0

I have a Game Tutorial that I followed, that paints String's on the Screen using Graphics2D. It works like a star, but the only problem is that I don't undertstand why.

I call draw(graphics) in a game loop and it works fine. I use an int, named currentChoice to keep track of which letter should be Red and which should be Black.

Well, I call the method Draw in a loop. I just don't understand how does the graphics clear the previous string it drew. I mean, I call the method constantly, and it keeps on Drawing string's on the window, and its 'clearing' the other ones (if you get what i'm saying).

Basicly, I just don't undertstant how it's clearing the screen (NOTE: I am super new to this sort of thing)

CODE (I call this in a loop and it works):

    public void draw(Graphics2D graphics) {

    bg.draw(graphics);

    graphics.setColor(titleColor);
    graphics.setFont(titleFont);
    graphics.drawString("Peache's Revenge", 50, 70);

    graphics.setFont(font);
    for (int i = 0; i < options.length; i++) {
        if (i == currentChoice) {
            graphics.setColor(Color.RED);
        } else {
            graphics.setColor(Color.BLACK);
        }
        graphics.drawString(options[i], 145, 140 + i * 15);
    }

}
Charles
  • 50,943
  • 13
  • 104
  • 142
SirTrashyton
  • 173
  • 2
  • 3
  • 12

1 Answers1

0

Assuming the Graphics context does not change (ie is the same for each call), then, unless the background is cleared, content will continue to be painted ontop of it.

From you comments, bg.draw is drawing the background, over the top of whatever was previously painted, meaning that anything that was previously painted will now be covered by the background, thus requiring the text to be re-generated.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366