我一直在做一个hang子手游戏来教自己Java。我已经进入了框架的主体。Java hang子手游戏重画()不工作
this.add(new PaintSurface(), BorderLayout.CENTER);
我有:
private class PaintSurface extends JComponent {
Shape found = null;
public PaintSurface(){
JOptionPane.showMessageDialog(null, "Repainting");
Shape s;
msgbox("LL: " + intLivesLost);
switch(intLivesLost){
//draw the Hanged man
case 10:
//Face + KILL
case 9:
//2nd Arm
case 8:
//1st Arm
case 7:
//2nd Leg
case 6:
//1st Leg
case 5:
//Body
case 4:
//Head
shapes.add(s);
case 3:
//Horizontal Bar
s = new Line2D.Float(100, 450, 250, 450);
shapes.add(s);
//Rope
s = new Line2D.Float(250, 450, 250, 500);
shapes.add(s);
case 2:
//Vertical Bar
s = new Line2D.Float(100, 450, 100, 670);
shapes.add(s);
case 1:
//Stand
s = new Line2D.Float(40, 670, 460, 670);
shapes.add(s);
break;
default:
break;
}
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(4));
g2.setColor(Color.BLACK);
for (Shape count : shapes){
g2.draw(count);
}
}
}
而且我使用:
repaint();
...整个项目中的每个帧被更新时,新的信猜测,不正确猜,新游戏。
当应用程序第一次运行JOptionPane.showMessageDialog(null,“重绘”);弹出,所以我知道它被称为那么。之后,“重新油漆”弹出不再出现,所以我知道repaint();电话无所事事。我知道代码正在进行repaint();调用,因为我在它们之前和之后放置了一个JOptionPane.showMessageDialog。
我已经试过,没有运气:
的removeAll();
revalidate();
getContentPane()。repaint();
这个任何提示和技巧,将不胜感激。
编辑:我试过了,因为你推荐,把代码放在“paint”中,认为这是我以前的样子,而且还没有工作。不过谢谢。
“public PaintSurface(){...}”是PaintSurface类的构造函数。它只在使用“new PaintSurface()”创建PaintSurface时被调用。你创建形状对象的逻辑应该放在其他地方(可能是绘制方法,但我不确定,因此这不是答案;)) –