By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

Clear Screen

100+
P: 176
Hello guys. I was wondering how you can clear the screen in a java program. Here is the code for my program:
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.SwingUtilities;
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4. import javax.swing.BorderFactory;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8.  
  9. public class GUI {
  10.  
  11.     public static void main(String[] args) {
  12.         SwingUtilities.invokeLater(new Runnable() {
  13.             public void run() {
  14.                 createAndShowGUI(); 
  15.             }
  16.         });
  17.     }
  18.  
  19.     private static void createAndShowGUI() {
  20.         JFrame f = new JFrame("Swing Paint Demo");
  21.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  22.         f.add(new MyPanel());
  23.         f.pack();
  24.         f.setVisible(true);
  25.     }
  26. }
  27.  
  28.  
  29. class MyPanel extends JPanel {
  30.  
  31.     /**
  32.      * 
  33.      */
  34.     private static final long serialVersionUID = 1L;
  35.  
  36.     public MyPanel() {
  37.         setBorder(BorderFactory.createLineBorder(Color.black));
  38.     }
  39.  
  40.     public Dimension getPreferredSize() {
  41.         return new Dimension(250,200);
  42.     }
  43.  
  44.     public void paintComponent(Graphics g) {
  45.         super.paintComponent(g);       
  46.  
  47.         // Draw Text
  48.         g.drawRect(100, 100, 100, 100);
  49.         //CLEAR SCREEN CODE GOES HERE
  50.     }  
  51. }
  52.  
If it worked than the rectangle would disapear.
May 7 '08 #1
Share this Question
Share on Google+
5 Replies


BigDaddyLH
Expert 100+
P: 1,216
What do you mean by "clear screen"?

1. clear console window
2. repaint MyPanel so that it is blank: filled with its background colour.
May 7 '08 #2

100+
P: 176
What do you mean by "clear screen"?

1. clear console window
2. repaint MyPanel so that it is blank: filled with its background colour.
I mean number 2. So that there is nothing drawn on the screen.
May 7 '08 #3

BigDaddyLH
Expert 100+
P: 1,216
I mean number 2. So that there is nothing drawn on the screen.
But if you put that into your paintComponent, you would never see what was painted in the previous lines.

Take a few steps back: what are you trying to do? What is your goal?
May 7 '08 #4

100+
P: 176
But if you put that into your paintComponent, you would never see what was painted in the previous lines.

Take a few steps back: what are you trying to do? What is your goal?
I had a theory for how to animate things. I thought that if you drew everything you wanted, cleared the screen, and typed:
Expand|Select|Wrap|Line Numbers
  1. Thread.sleep()
and had it sleep for a very very short time and you drew the shapes again in a different place close to the last one it would look like it moved. To do this I need to be able to clear the screen.
May 7 '08 #5

BigDaddyLH
Expert 100+
P: 1,216
I had a theory for how to animate things. I thought that if you drew everything you wanted, cleared the screen, and typed:
Expand|Select|Wrap|Line Numbers
  1. Thread.sleep()
and had it sleep for a very very short time and you drew the shapes again in a different place close to the last one it would look like it moved. To do this I need to be able to clear the screen.
Showing a blank panel in between showing a shape at position A, then position B will only make it seem like it's blinking in and out of existence.

You should not use sleep to animate. Use a timer instead: http://java.sun.com/docs/books/tutor...isc/timer.html
May 7 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.