在java中应用paint和repaint方法的一段小程序如下! 问:为什么运行起来红色圆重画了之后之前的圆还显示着

importjava.awt.*;importjavax.swing.*;publicclassTankWarClinetextendsJFrame{publicstat... import java.awt.*;
import javax.swing.*;

public class TankWarClinet extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50,y = 50;

Image offScreenImage = null;

public void updata(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage,GAME_WIDTH,GAME_HEIGTH,null);
}
}

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x,y,30,30);
g.setColor(c);

y+=50;

}

public void LaunchFrame() {
this.setBounds(100,50,GAME_WIDTH,GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}
展开
 我来答
yujingtaojing
2011-03-12 · TA获得超过467个赞
知道小有建树答主
回答量:1108
采纳率:0%
帮助的人:489万
展开全部
import java.awt.*;
import javax.swing.*;

public class TankWarClinet extends JFrame {

public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50, y = 50;
Image offScreenImage = null;

public void updata(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage, GAME_WIDTH, GAME_HEIGTH, null);
}
}

@Override
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);

y += 50;

}

public void LaunchFrame() {
this.setBounds(100, 50, GAME_WIDTH, GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private class PaintThread implements Runnable {

@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
}

加一句super.paint(g);就可以了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式