5

I have been making a hangman game to teach myself 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"); 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(); calls, as I put a JOptionPane.showMessageDialog before and after them.

I've tried with no luck:

removeAll();
revalidate();
getContentPane().repaint();

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.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Geoff
  • 583
  • 2
  • 7
  • 25
  • "public PaintSurface(){...}" is the constructor of the PaintSurface class. it only gets called when PaintSurface is created with "new PaintSurface()". Your logic to create the shapes object should be places somewhere else (probably the paint method, but i'm not sure hence this isn't an answer ;)) – OH GOD SPIDERS Apr 29 '16 at 11:58

2 Answers2

1
  1. Do not override paint, override paintComponent or update instead according to your needs.
  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.
bns
  • 392
  • 2
  • 9
0

I've solved it, put the drawing on a separate panel, and that's all working fine. Thanks for the help.

Geoff
  • 583
  • 2
  • 7
  • 25