您的位置:首页 > 其它

16.10.19 Swing 动画是如何被制作出来的

2016-10-19 22:27 197 查看
   

首先 你要会画一张静态的图

1.画板     JPanel    Canvas 

   2.画笔是什么  ?   怎么画 ?

      Griphics  g  

         有这么3个方法   repaint paint update

         2.1   作画的代码扔在哪?  

                  paint里

         2.2    外部调用的是什么方法

                  repaiint

3.连续的作画是动画吗?   是

 

4.连续作画  的方式  ?    ---------------要不就是 擦完重画   要不就是  画好覆盖

    举个栗子:

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

public class Gun extends Canvas implements Runnable {

    public Gun() {

        // 在创建对象时 创建并运行线程

        Thread t = new Thread(this);

        t.start();

    }

    public void run() {

        while (true) {

            // 设置帧数

            try {

                Thread.sleep(5);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            // 控制子弹向下移动

            y++;

            if (y == 400)

                y = 0;

            this.repaint();

        }

    }

    int y = 0;

    /**

     * g 是画笔 在paint 方法中作画 外部调用是repaint方法

     */

    public void paint(Graphics g) {

        g.setColor(Color.red);

        g.fillOval(30, y, 20, 20);

        g.setColor(Color.black);

        g.fillRect(20, 0, 40, 80);

    }

    public static void main(String[] args) {

        // 定义并配置顶层窗口

        JFrame jf = new JFrame();

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.setBounds(0, 0, 400, 400);

        jf.setVisible(true);

        // 配置画板 顺便 运行线程

        Gun c = new Gun();

        c.setBounds(0, 0, 400, 400);

        jf.add(c);

        c.setVisible(true);

    }

}

     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swing