java Graphics.drawString() 绘制我的旧字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5842360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Graphics.drawString() Draws Over My Old String
提问by daGrevis
I'm working on simple counter. My problem is that drawString() method draws new string over the old one. How to clear the old one before? Code...
我正在研究简单的计数器。我的问题是 drawString() 方法在旧字符串上绘制新字符串。如何清除之前的旧的?代码...
package foobar;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class board extends JPanel implements Runnable {
Thread animator;
int count;
public board() {
this.setBackground( Color.WHITE );
count = 0;
animator = new Thread( this );
animator.start();
}
@Override
public void run() {
while( true ) {
++count;
repaint();
try {
animator.sleep( 1000 );
} catch ( InterruptedException e ) {}
}
}
@Override
public void paint( Graphics Graphics ) {
Graphics.drawString( Integer.toString( count ), 10, 10 );
}
}
P.S. I'm new to Java, so please don't be afraid to tell me what other things I should fix in my code...
PS我是Java新手,所以请不要害怕告诉我我应该在我的代码中修复哪些其他问题......
回答by Hovercraft Full Of Eels
Several problems in your code:
您的代码中有几个问题:
- Don't have a while (true) loop or Thread.sleep in a Swing GUI. Use a Swing Timer instead.
- Override JPanel's paintComponent, not its paint method.
- The first call in paintComponent(Graphics g) should be super.paintComponent(g), so your JPanel can do its house keeping and get rid of old graphics.
- 在 Swing GUI 中不要有 while(真)循环或 Thread.sleep。改用摇摆计时器。
- 覆盖 JPanel 的paintComponent,而不是它的paint 方法。
- 在paintComponent(Graphics g) 中的第一个调用应该是super.paintComponent(g),这样您的JPanel 就可以进行内部管理并摆脱旧图形。
edit:
编辑:
- My bad, your while (true) and Thread.sleep(...) willwork since they're in a background thread, but,...
- Thread.sleep is a static method and should be called on the class, Thread, and
- I still think a Swing Timer would be an easier way to do this.
- Even easier still is to not even use a paint or paintComponent method, but rather simply set the text of a JLabel for your display.
- 我的不好,你的 while (true) 和 Thread.sleep(...)会工作,因为它们在后台线程中,但是,...
- Thread.sleep 是一个静态方法,应该在类、Thread 和
- 我仍然认为 Swing Timer 是一种更简单的方法。
- 更简单的方法是甚至不使用paint 或paintComponent 方法,而是简单地为您的显示设置JLabel 的文本。
e.g.,
例如,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Board2 extends JPanel {
private static final int TIMER_DELAY = 1000;
private int counter = 0;
private JLabel timerLabel = new JLabel("000");
public Board2() {
add(timerLabel);
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
timerLabel.setText(String.format("%03d", counter));
}
}).start();
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Board2");
frame.getContentPane().add(new Board2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
回答by corlettk
I think Graphics.clearRectis what you're looking for.
我认为Graphics.clearRect就是你要找的。
回答by james_bond
I would do it like this:
我会这样做:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//draw all the other stuff
}
回答by Alba Mendez
Aaah! This is normal. Imagine your panel as a blackboard. Each time you want to repaint what you have writed in, you will have to erase the blackboard first.
啊啊!这是正常的。将您的面板想象成一块黑板。每次你想重新粉刷你写过的东西时,你必须先把黑板擦掉。
In Java, as well as in Graphics in general, things go in a similar way. In your paint method, do this:
在 Java 以及一般的 Graphics 中,事情以类似的方式进行。在您的绘制方法中,执行以下操作:
Graphics.clearRect(0,0, getWidth(),getHeight());
//CLEAR the entire component first.
Graphics.drawString(...); //now that the panel is blank, draw the string.
When you can handle the topic better, do super.paint(Graphics)
instead of clearRect()
.
当您可以更好地处理该主题时,请执行super.paint(Graphics)
而不是clearRect()
.