import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;public class Test { public static void main(String[] args) {
TankWar tankWar = new TankWar(); }
}class TankWar extends Frame {//
//主要问题为:把这里的Frame改成JFrame运行时不会擦除背景
//请运行一下看看区别,然后帮忙解释一下吧~~
//别人问我的问题,搞不清了,看API也看的眼花缭乱的~
//
//另外感觉这段代码~~有点~~那个,在repaint()里调用paint()会产生递归吧
//我觉的这不是很好的设计方式,请大家评价一下
//
//又:谁有关于画图或paint()方法的详细点的资料,分享一下吧~谢谢啦
int x = 50, y = 50; @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 5;
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(TankWar.class.getName()).log(Level.SEVERE, null, ex);
}
repaint();
} public TankWar() {
super("测试");
this.setLocation(100, 100);
this.setSize(800, 600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
}
} 问题在注释里~~
import java.awt.Frame;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;public class Test { public static void main(String[] args) {
TankWar tankWar = new TankWar(); }
}class TankWar extends Frame {//
//主要问题为:把这里的Frame改成JFrame运行时不会擦除背景
//请运行一下看看区别,然后帮忙解释一下吧~~
//别人问我的问题,搞不清了,看API也看的眼花缭乱的~
//
//另外感觉这段代码~~有点~~那个,在repaint()里调用paint()会产生递归吧
//我觉的这不是很好的设计方式,请大家评价一下
//
//又:谁有关于画图或paint()方法的详细点的资料,分享一下吧~谢谢啦
int x = 50, y = 50; @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 5;
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(TankWar.class.getName()).log(Level.SEVERE, null, ex);
}
repaint();
} public TankWar() {
super("测试");
this.setLocation(100, 100);
this.setSize(800, 600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
}
} 问题在注释里~~
详细出处参考:http://bbs.firnow.com/dview16t125538.html
不过在paint里调用repaint是不好,同样效果可以这样做。 public void paint(Graphics g) {
//--by blazingfire 用JFrame不能重绘,要加上如下代码
//你还要重绘背景
super.paint(g);
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
} public TankWar() {
super("测试");
this.setLocation(100, 100);
this.setSize(800, 600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
//--by blazingfire 用定时器重绘, paint方法只要绘图就好了
new Timer().schedule(new TimerTask() {
@Override
public void run() {
y += 5;
TankWar.this.repaint();
}
}, 500, 500);
}