2

I have a JPanel here that I want to have keep track of a number of guesses that I am giving the player.

The guesses are displayed every time the paintcomponenent is called. This is the code:

@Override
public void paintComponent(Graphics g){
    if(stop) {
        g.drawString("YOU RAN OUT OF GUESSES. YOU LOSE!", getWidth() / 2 - 150, getHeight() / 2 - 5);
        return;
    }
    for(Block block : blocks){
        block.draw(g);
    }
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(650, 650, 100, 100);
    g.setColor(Color.BLACK);
    g.drawRect(650, 650, 100, 100);

    g.drawString("CHECK", 680, 705);
    g.drawString("Guesses Left: " + guesses, 100, 100);
}

What happens is the strings that are drawn from the previous calling of repaint() to cause this method to be called do not disappear.

This means that the number after "Guesses left: " gets to be unreadable after numbers start to pile on each other (starting from ten and going down once before the method is called).

I don't see a reason why this should be an issue. A similar issue I am having is that when the stop Boolean is true, it should be quitting the method and not draw the rest of the shapes. This is not the case though, all those shapes are still drawn.

Can someone help me figure out what I am doing wrong? Here is a screenshot of my two problems:

enter image description here

Also, through a bit of accidental testing, I found that if the window is resized, all the other shapes disappear and only the text is left.

Do I just have a misconception of how the repaint method works? My high school java teacher told me it essentially just recalls the paintComponent() method, but I wouldn't be surprised if that was incorrect.

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
  • You might want to take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) to get a better understanding of how painting works in Swing – MadProgrammer Mar 03 '16 at 04:15
  • @NuffsaidM8 You can take a look at [What does super.paintComponent(g) do?](http://stackoverflow.com/questions/28724609/what-does-super-paintcomponentg-do) – user3437460 Mar 03 '16 at 06:25
  • @NuffsaidM8 `but I wouldn't be surprised if that was incorrect.` you seems to have little confidence in your teacher.. – user3437460 Mar 03 '16 at 06:33

1 Answers1

4

What happens is the strings that are drawn from the previous calling of repaint() ...

@Override
public void paintComponent(Graphics g)
{
    ...
}

You need to make sure the panels background is painted before you do your custom painting. The code should be:

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    ...
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Ah yes, that would do it. I included that in my other swing programs, but Intellij didn't require it so I glossed over that line. Thanks! –  Mar 03 '16 at 11:58