3 Replies Latest reply: Jan 6, 2005 2:11 PM by 807596 RSS

    Disapearing circle

    807596
      When I run this code a circle is drawn but if I resize the window or move another window on top of it the circle disapears. Am I correct in assuming that this is because I only tell it to draw once, instread of in any kind of reguarlly run loop?
      import java.awt.*;
      import javax.swing.*;
       
      public class Main 
      {
          public static void main(String[] args) 
          {    
              JFrame k = new JFrame();
              k.setSize(500, 500);
              k.setLocation(10,10);   
              k.setVisible(true);
              k.setDefaultCloseOperation(k.EXIT_ON_CLOSE);
              Graphics g = k.getGraphics();
              g.fillOval(100 , 100 , 50, 50);       
          }    
      }
        • 1. Re: Disapearing circle
          807596
          when you move the frame, the frame is invalidated..and performed a repaint()...which i believe invoke the paint() method..which contains no code to paint the circle.

          what you need to do is no overide the paint method...actually..use SuperPaint()..or some method like that (forgot the exact method signature)

          or do the same as above, butuse a JLabel and then add it to the JFrame
          • 2. Re: Disapearing circle
            796447
            When I run this code a circle is drawn but if I
            resize the window or move another window on top of it
            the circle disapears. Am I correct in assuming that
            this is because I only tell it to draw once, instread
            of in any kind of reguarlly run loop?
            It's not that you should draw it in some loop - it's that the frame (JFrame k)'s paint() method should do the painting when called upon to do so, such as when you move windows around like you described.
            • 3. Re: Disapearing circle
              807596
              try this
              public class Main extends JFrame{
                  public static void main(String args[]){
                      Main m = new Main();
                      m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  }
              
                  public Main(){
                      setSize(500,500);
                      setLocation(10,10);
                      SetVisible(true);
                  }
               
                  // should overide this method instead of the paint() method
                  public void paintComponents(Graphics g){
                      g.fillOval(100,100,50,60);
                  }
              
              }