Search code examples
javaswingawtgraphics2ddrawstring

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


Solution

  • 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.