title: How can i reference a class that extends JPanel from another class
body: <p>I have a Circle class, every circle element has a thread for moving the circle in the jpanel. The CirclePanel class is separate from the actual GUI and has a vector of circle elements . I need a way to repaint the jpanel without creating a new panel every time. The CirclePanel is instantiated in the GUI class which creates a frame that contains the CirclePanel. How can redraw the panel each time the circle moves ? Thank you !</p>
  
  <p>EDIT : put in some code </p>
  
  <p>Circle class:</p>
  
  <pre><code>    public class Circle extends Thread {
  
      public Ellipse2D.Double thisCircle;
      public int size=30,xPoz,yPoz;
      Random r=new Random();
      int red=r.nextInt(255),green=r.nextInt(255),blue=r.nextInt(255);
      int deltax,deltay;
      public boolean ballStarted;
  
  
      public Circle()
      {xPoz=(int)Math.random()*300;
       yPoz=(int)Math.random()*300;
       ballStarted = true;
       deltax=-10+(int)(Math.random()*21);
       deltay=-10+(int)(Math.random()*21);
       if ((deltax == 0) &amp;&amp; (deltay == 0)) { deltax = 1; }
       thisCircle=new Ellipse2D.Double(xPoz,yPoz,size,size);
  
      }
  
  
      public void draw(Graphics2D g2d){
      if(thisCircle!=null)
      {g2d.setColor(new Color(red,green,blue,80));
       g2d.fill(thisCircle);
  
      }  
      }
  
      public int PozX(){return xPoz;}
      public int PozY(){return yPoz;}
      public int radius(){return size*2;}
  
      public void grow(){
      size++;
      thisCircle.setFrame(xPoz,yPoz,size,size);
      }
  
      public void move(){
          int oldx=xPoz;
          int oldy=yPoz;
  
          int newx=oldx+deltax;
          int newy=oldy+deltay;
  
          if(newx+size&gt;800 || newx&lt;0)
              deltax=-deltax;
          if(newy+size&gt;600 || newy&lt;0)
              deltay=-deltay;
  
          thisCircle.setFrame(newx,newy,size,size);
      }
  
      public void run(){
  
          try {
                 Thread.sleep(100); 
              }
              catch (InterruptedException e)
              {  System.out.println("Thread Halted");}
          while(ballStarted)
          {move(); }
  
      }
  
  }
  </code></pre>
  
  <p>This is the Circle panel class :</p>
  
  <pre><code>public class CirclePanel extends JPanel {
  
      private int prefwid, prefht;
      public ArrayList&lt;Circle&gt; Circles = new ArrayList&lt;Circle&gt;();
  
      public ShapePanel(int pwid, int pht) {
          prefwid = pwid;
          prefht = pht;
          createCircles();
      }
  
      public void createCircles() {
          for (int i = 1; i &lt;= 20; i++) {
              Circle nextCircle = new Circle();
              Circles.add(nextCircle);
              nextCircle.start();
  
          }
      }
  
      public Dimension getPreferredSize() {
          return new Dimension(prefwid, prefht);
      }
  
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          for (int i = 0; i &lt; Circles.size(); i++) {
              (Circles.get(i)).draw(g2d);
          }
  
      }
  }
  </code></pre>