画图时不能直接显示只有最小化后才显示

sangqiesimajia 2012-09-09 09:40:07
代码下面放,百度说是重画的问题。可是移动窗口也不能显示
public class TankClient extends Frame{

public static int WEIDTH=800;
public static int HEIGHT=600;

public List<Wall> walls=new ArrayList<Wall>();
Graphics g=null;
Image offScreenImage = null;

class MouseMonitor extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("主页面的鼠标单击");
Point p=e.getPoint();
walls.add(new Wall(p.x,p.y,Wall.wallStyle.stone,null));

}
}

public void lanchFrame(){

this.setSize(WEIDTH, HEIGHT);
this.setLocation(200, 50);
setTitle("坦克大战");
this.setBackground(Color.GREEN);

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new Thread(new PaintThread()).start();
this.addMouseListener(new MouseMonitor());


this.setVisible(true); this.setResizable(false);
}

public void paint(Graphics g) {
System.out.println("重画!");
for(int i=0; i<walls.size(); i++) {
Wall w = walls.get(i);
w.draw(g);
}
}


public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(WIDTH, HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, WIDTH,HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}

private class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args){
new TankClient().lanchFrame();
}
}


wall类的draw()

public void draw(Graphics g){
System.out.println("draw");

if(!this.live){
tc.walls.remove(this);
return ;
}

if(!init){
System.out.println("initwall");
init=true;
for(int i=0;i<wallImage.length;i++){
System.out.println("drawImage"+i);
g.drawImage(wallImage[i], -100, -100, null);
}
}

if(style==wallStyle.grass){
// System.out.println("drawgrass"+locationX+locationY);
// g.drawImage(wallImage[0], locationX, locationY, null);
Color c = g.getColor();
g.setColor(Color.DARK_GRAY);
g.fillRect(locationX,locationY,50,50);
g.setColor(c);
}
else if(style==wallStyle.stone){
// System.out.println("drawstone"+locationX+locationY);
// g.drawImage(wallImage[1], locationX, locationY, null);
Color c = g.getColor();
g.setColor(Color.DARK_GRAY);
g.fillRect(locationX,locationY,50,50);
g.setColor(c);
}
}
...全文
153 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sangqiesimajia 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

如果出现需要最小化后才能出现内容的话都是“重画”那块的问题。有时候调用repaint()/getUI()/invalidate()/revalidate()的问题。你这点代码不好调试。
[/Quote]
现在把继承改为JFrame就可以了,这不应该是重画的问题了吧,丫的现在背景色又改不了
shenhua 2012-09-09
  • 打赏
  • 举报
回复
如果出现需要最小化后才能出现内容的话都是“重画”那块的问题。有时候调用repaint()/getUI()/invalidate()/revalidate()的问题。你这点代码不好调试。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧