Clear Screen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    #1

    Clear Screen

    Hello guys. I was wondering how you can clear the screen in a java program. Here is the code for my program:
    Code:
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    public class GUI {
       
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    
        private static void createAndShowGUI() {
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new MyPanel());
            f.pack();
            f.setVisible(true);
        }
    }
    
    
    class MyPanel extends JPanel {
    
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
    
            // Draw Text
            g.drawRect(100, 100, 100, 100);
            //CLEAR SCREEN CODE GOES HERE
        }  
    }
    If it worked than the rectangle would disapear.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    What do you mean by "clear screen"?

    1. clear console window
    2. repaint MyPanel so that it is blank: filled with its background colour.

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by BigDaddyLH
      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.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Kid Programmer
        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?

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Originally posted by BigDaddyLH
          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:
          Code:
          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.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Kid Programmer
            I had a theory for how to animate things. I thought that if you drew everything you wanted, cleared the screen, and typed:
            Code:
            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

            Comment

            Working...