简体   繁体   English

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

[英]Java hangman game repaint() not working

I have been making a hangman game to teach myself Java. 我一直在做一个刽子手游戏来自学Java。 I've got in the main body of the frame. 我已进入框架的主体。

this.add(new PaintSurface(), BorderLayout.CENTER);

I've got: 我有:

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);
        }
    }
}

And I'm using: 我正在使用:

repaint();

...throughout the project each time the frame is updated, new letter guessed, incorrect guess, new game. ...每次更新框架时,整个项目,新信猜测,错误猜测,新游戏。

When the application first runs JOptionPane.showMessageDialog(null, "Repainting"); 当应用程序首次运行JOptionPane.showMessageDialog(null,“重绘”)时; pops up, so I know it's been called then. 弹出,所以我知道它被称为。 Following that, the "Repainting" pop up doesn't appear any more, so I know that the repaint(); 在那之后,“重新绘制”弹出不再出现,所以我知道重绘(); calls are doing nothing. 电话什么也没做。 I know the code is getting to the repaint(); 我知道代码正在进入repaint(); calls, as I put a JOptionPane.showMessageDialog before and after them. 调用,因为我在它们之前和之后放置了一个JOptionPane.showMessageDialog。

I've tried with no luck: 我试过没有运气:

removeAll(); 移除所有();
revalidate(); 重新验证();
getContentPane().repaint(); 的getContentPane()重绘();

Any hints and tips for this would be much appreciated. 任何提示和技巧都将非常感激。

Edit: I've tried it as you recommend, putting the code in "paint", think this is how I had it before, and it's still not working. 编辑:我按照你的推荐尝试了它,把代码放在“绘画”中,认为这是我以前的方式,它仍然无法正常工作。 Thanks though. 谢谢。

  1. Do not override paint, override paintComponent or update instead according to your needs. 不要覆盖paint,覆盖paintComponent或根据需要更新。
  2. Seems like you have a confusion between the paint, repaint and update methods. 好像你在绘画,重绘和更新方法之间存在混淆。 Read this: https://www.guiguan.net/repaint-paint-and-update/ if you are doing a game, repaint() will cause the repaint of the entire component, so you will have some performance issues. 阅读本文: https//www.guiguan.net/repaint-paint-and-update/如果您正在进行游戏,则repaint()将导致整个组件的重绘,因此您将遇到一些性能问题。

I've solved it, put the drawing on a separate panel, and that's all working fine. 我已经解决了它,把图纸放在一个单独的面板上,这一切都很好。 Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM