简体   繁体   中英

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

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 the drawString command, if possible.

您可以尝试在comp2d.drawString()之后使用repaint()。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM