JPanel画图
我想要实现一个动画功能,一个矩形从上落到下;但下面的代码不知道怎么回事,结果把整个运动过程中的所以矩形都画出来了,谁能帮忙解决下。import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Gamef extends JPanel{
static Zuhe z;
int x = 70;
static int y = 40;
int j = 0;
public static void main(String[] args) {
final Gamef gf = new Gamef();
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setBackground(Color.DARK_GRAY);
jf.setBounds(400,130,300,400);
jf.setResizable(false);
gf.setBackground(Color.DARK_GRAY);
jf.add(gf);
gf.stat();
}
public void stat() {
new Thread(new abcd()).start();
}
public void paint(Graphics g) {
Color c = g.getColor();
g.fillRect(x, y, 30, 40);
if(j == 1){
g.setColor(Color.RED);
g.fillRect(x, y, 30, 40);
}else if(j == 2){
g.setColor(Color.BLUE);
g.fillRect(x, y, 30, 40);
}
g.setColor(c);
}
private class abcd implements Runnable {
public void run() {
while(true) {
y += 10;
j ++;
if(j >3) j = 1;
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}