Graphics.drawString () Draws on top of my old line

I am working on a simple counter. My problem is that the drawString () method creates a new line on top of the old one. How to clean old? Code...

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

PS I'm new to Java, so please don't be afraid to tell me what else I need to fix in my code ...

+4
source share
4 answers

A few problems in your code:

  • You do not need to have a while (true) loop or Thread.sleep in the Swing GUI. Use a Swing timer instead.
  • Undo the JPanel paintComponent, not its drawing method.
  • The first call to paintComponent (Graphics g) should be super.paintComponent (g), so your JPanel can save its home and get rid of old graphics.

edit:

  • My bad, your time (true) and Thread.sleep (...) will work as they are in the background thread, but ...
  • Thread.sleep is a static method and should be called by the class, Thread and
  • I still think Swing Timer will be an easier way to do this.
  • It’s even easier not to use the paint or paintComponent method, but simply set the JLabel text for your display.

eg.

 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(); } }); } } 
+7
source

I think Graphics.clearRect is what you are looking for.

+1
source

I would do it like this:

 public void paintComponent(Graphics g) { super.paintComponent(g); //draw all the other stuff } 
+1
source

Ahh! This is normal. Imagine your panel as a blackboard . Every time you want to redraw what you wrote, you need to erase the board first .

In Java, as in Graphics in general, everything goes the same way. In the drawing method, do the following:

 Graphics.clearRect(0,0, getWidth(),getHeight()); //CLEAR the entire component first. Graphics.drawString(...); //now that the panel is blank, draw the string. 

If you are better at super.paint(Graphics) theme, do super.paint(Graphics) instead of clearRect() .

+1
source

All Articles