Java刽子手游戏重绘()无法正常工作

时间:2016-04-29 11:45:58

标签: java eclipse repaint

我一直在制作一个刽子手游戏来自学Java。我进入了框架的主体。

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();
  重新验证();
  的getContentPane()重绘();

非常感谢任何提示和技巧。

编辑:我已经按照您的推荐尝试了它,将代码放入"绘制",认为这是我以前的方式,它仍然无法正常工作。谢谢。

2 个答案:

答案 0 :(得分:1)

  1. 不要覆盖paint,覆盖paintComponent或根据需要更新。
  2. 好像你在绘画,重绘和更新方法之间存在混淆。阅读本文:https://www.guiguan.net/repaint-paint-and-update/如果您正在进行游戏,则repaint()将导致整个组件的重绘,因此您将遇到一些性能问题。

答案 1 :(得分:0)

我已经解决了它,把图纸放在一个单独的面板上,这一切都正常。 谢谢你的帮助。

相关问题