user69514
user69514

Reputation: 27629

Java Graphics2D DrawString

Hey guys I have a little issue here. I have a panel where I am drawing a string. This is a game so I keep redrawing the score in order to update it. However when I draw it again it is drawn on top of the previous score so it looked all garbled up. Any ideas how to fix this?

comp2d.drawString(GetScore(Score),ScoreX,ScoreY);

Upvotes: 0

Views: 2048

Answers (3)

Dmitry
Dmitry

Reputation: 85

when you call this comand: comp2d.drawString(GetScore(Score),ScoreX,ScoreY);

You should call this : comp2d.dispose()

because all operations with comp2d will be applied after .dispose()

Upvotes: 0

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

You need to redraw the background, before you paint the string. If this is an ordinary panel, you can redraw the background by a call to super.paintComponent(g) in your own paintComponent; however, since this is a game, I am going to guess that you have some other background you need to draw. Also, I would suggest that you use a JLabel, in lieu of using thedrawString command, if possible.

Upvotes: 5

zs2020
zs2020

Reputation: 54514

You can try to use repaint() after comp2d.drawString().

Upvotes: 1

Related Questions