简体   繁体   English

Java Graphics2D DrawString

[英]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); 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 ; 如果这是普通面板,则可以通过在自己的paintComponent调用super.paintComponent(g)来重绘背景; 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. 另外,如果可能的话,我建议您使用JLabel来代替使用drawString命令。

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

when you call this comand: comp2d.drawString(GetScore(Score),ScoreX,ScoreY); 当您调用此命令时: comp2d.drawString(GetScore(Score),ScoreX,ScoreY);

You should call this : comp2d.dispose() 您应该这样称呼: comp2d.dispose()

because all operations with comp2d will be applied after .dispose() 因为所有comp2d操作都将在.dispose()之后应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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