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);
}
}
相关文章推荐
- 如何制作具有声音效果的动画桌面精灵
- 麦当劳制作过程揭秘,麦当劳是如何制作出来的!
- 【安卓开机动画制作】如何将视频转…
- 【安卓开机动画制作】如何将视频转…
- 【安卓开机动画制作】如何将视频转…
- 如何制作鼠标感应动画菜单
- 【安卓开机动画制作】如何将视频转…
- 【安卓开机动画制作】如何将视频转…
- 如何制作gif动画,丰富自己的博客?
- 【安卓开机动画制作】如何将视频转…
- 如何制作鼠标感应动画菜单
- 如何利用地图API制作汽车沿道路行驶的动画?——如何获得道路层数据
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画?——如何获得道路层数据
- 【安卓开机动画制作】如何将视频转…
- 如何用 Keynote 制作动画演示(转)
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画?——如何获得道路层数据
- VC如何制作启动动画
- 如何在cocos2d里面用TexturePacker制作可移动的炮炮兵动画
- 【安卓开机动画制作】如何将视频转…
- 【安卓开机动画制作】如何将视频转…