repaint重绘不消除之前图像问题
来源:互联网 发布:怎么看淘宝关注的直播 编辑:程序博客网 时间:2024/04/29 14:09
好久没写过Java界面的程序,一写就搞不定:
如下,就是一个小球在在几面里来回移动的问题
public class ReboundPanel extends JPanel { private final int WIDTH = 300, HEIGHT = 100; private final int DELAY = 20; private Timer timer; private int x,y ,moveX,moveY; public ReboundPanel() { timer = new Timer(DELAY,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { x += moveX; y += moveY; if(x<=0 ||x >= WIDTH - 30) { moveX = moveX * -1; } if(y<=0 ||y >= HEIGHT - 30) { moveY = moveY * -1; } repaint(); } }); x = 0; y = 40; moveX = moveY = 3; setPreferredSize(new Dimension(WIDTH, HEIGHT)); setBackground(Color.black); timer.start(); } @Override public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, 30, 30); g.setColor(c); }}
结果却是这样:
对,之前的图像没有消除,查了资料,在继承了JPanel类后使用paint()方法应该调用一下父类的paint()方法,继承JFrame类的类使用paint()方法测试不调用父类方法并不会出现这样的情况:
public void paint(Graphics g) { super.paint(g);//调用父类方法 Color c = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, 30, 30); g.setColor(c); }
结果:
0 0
- repaint重绘不消除之前图像问题
- 重构(reflow)与重绘(repaint)
- JPanel repaint不清除之前图案的问题
- Qt重绘之update,repaint详解
- Swing 重绘 repaint,updateUI区别
- Qt重绘之update,repaint详解
- Qt重绘之update,repaint详解
- Qt重绘之update,repaint详解
- Qt重绘之update,repaint详解
- Qt重绘之update,repaint详解
- Qt重绘之update,repaint详解
- 回流reflow和重绘repaint
- Reflow(回流)和Repaint(重绘)
- 回流reflow与重绘repaint
- 回流(reflow)与重绘(repaint)
- Swing 重绘 repaint,updateUI区别
- reflow(回流)和repaint(重绘)
- 重绘和回流repaint/reflow
- 【Java】设计模式:深入理解单例模式
- Android string.xml 及通配符 %$ 取值用法
- Cf div2 338 D [费马小定理]
- 为什么要用补码
- Caffe、TensorFlow、MXnet三库对比
- repaint重绘不消除之前图像问题
- 开机启动程序
- mysql不区分à与a或大小写的解决方法
- Servlet的生命周期
- Volley的使用
- 第二周项目0-宣告主权
- 整除2011
- NOIP2006(4)数列
- 网友整理的Android面试题集-附网友提供的参考答案