所以基本上我想在 JFrame 面板上有一个数字系统。使用此方法将数字显示在框架上。
public static void text(int x, int y, String text)
{
if (instance == null)
throw new RuntimeException("Must call window method first");
Graphics g = image.getGraphics();
g.setColor(color);
Font font = new Font(null, Font.PLAIN, fontSize);
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
int actualX = x - metrics.stringWidth(text) / 2;
int actualY = y + metrics.getAscent() / 2;
g.drawString(text, actualX, actualY);
instance.repaint();
}
当数字的值增加时,我希望它用新数字替换上面的数字。我知道必须有办法做到这一点,但有人可以帮助我找到它吗?非常感谢。
最佳答案
您需要清除绘制的区域,然后重新绘制字符串。
public static void image(int x, int y, int width, int height, String file)
{
if (instance == null)
throw new RuntimeException("Must call window method first");
Graphics g = image.getGraphics();
////
g.setColor(bgColor);//background color
g.fillRect(0,0,image.getWidth(),image.getHeigth());//clearing the are by filling a rectangle
////
g.setColor(color);
Font font = new Font(null, Font.PLAIN, fontSize);
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
int actualX = x - metrics.stringWidth(text) / 2;
int actualY = y + metrics.getAscent() / 2;
g.drawString(text, actualX, actualY);
instance.repaint();
}
关于java - 如何删除JFrame中绘制的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553657/