views:

389

answers:

2

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

A: 

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

sza
I tried before, but no luck
+3  A: 

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.

Michael Aaron Safyan
+1, I don't know why people keep trying to do custom painting like drawing strings and images on a panel when you can just use a JLabel. Use a proper layout manager and you don't have to worry about all these little painting details.
camickr
yeah I tried a JLabel, but for some reason when I add it everything else disappears