title: How to move methods to a second file to call from first - Java Eclipse
body: <p>I need some help with moving methods so that I can have less code on the "client" (First file). I want to be able to call the methods so that the program works as it does now. I have tried do it myself, but I can not get it to work. Any help is appreciated!</p>
  
  <p>The methods I wish to move are the paintComponent, PanelListener, and PanelMotionListener</p>
  
  <p>Here is the first file. </p>
  
  <pre><code>/******************/
  /* IMPORT SECTION */
  /******************/
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  /**************/
  /* MAIN CLASS */
  /**************/
  public class Prog2 extends JPanel
  {
      private Prog2_Server c1, c2;
      private Prog2_Server selectedRect;
      private int x,y;
  
      public static void main(String[] args)
      {
          JFrame theGUI = new JFrame();
          theGUI.setTitle("GUI Example");
          theGUI.setSize(500,500);
          theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Prog2 panel = new Prog2(Color.white);
          Container pane = theGUI.getContentPane();
          pane.add(panel);
          theGUI.setVisible(true);
      }
  
      public Prog2(Color backColor)
      {
          setBackground(backColor);
          c1 = new Prog2_Server(300,50,100,200, Color.red);
          c2 = new Prog2_Server(50,50,100,200, Color.blue);
          selectedRect = null;
          addMouseListener(new PanelListener());
          addMouseMotionListener(new PanelMotionListener());
      }
  
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
          c1.fill(g);
          c2.draw(g);
      }
  
      private class PanelListener extends MouseAdapter
      {
          public void mousePressed(MouseEvent e)
          {
              // Select a circle if it contains the mouse coords
              x= e.getX();
              y= e.getY();
  
              if(c1.containsPoint(x,y))
              {
                  selectedRect = c1;
              }
              else if (c2.containsPoint(x,y))
              {
                  selectedRect = c2;
              }
          }
  
          public void mouseReleased(MouseEvent e)
          {
              // deselect the selected rectangle
              x= e.getX();
              y= e.getY();
              selectedRect = null;
          }
      }
  
      private class PanelMotionListener extends MouseMotionAdapter
      {
          public void mouseDragged (MouseEvent e)
          {
              // compute the distance and move the selected circle
              int newX = e.getX();
              int newY = e.getY();
              int dx = newX - x;
              int dy = newY -y;
  
              if(selectedRect != null)
              {
                  selectedRect.move(dx,dy);
              }
  
              x=newX;
              y=newY;
              repaint();
          }
      }
  }
  </code></pre>
  
  <p>Here is the second file.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class Prog2_Server extends JPanel
  {
      private int width, length, xCoord, yCoord;
      private Color color;
      private Prog2_Server c1, c2;
      private Prog2_Server selectedRect;
      private int x,y;
  
  
      public Prog2_Server(int xC, int yC, int x, int y, Color c)
      {
          width = x;
          length = y;
          xCoord = xC;
          yCoord = yC;
          color = c;
      }
  
      public void draw(Graphics g)
      {
          Color oldColor = g.getColor();
          g.setColor(color);
          g.drawRect(xCoord,yCoord,width,length);
          g.setColor(oldColor);
      }
  
      public void fill(Graphics g)
      {
          Color oldColor = g.getColor();
          g.setColor(color);
          g.drawRect(xCoord,yCoord,width,length);
          g.setColor(oldColor);
      }
  
      public boolean containsPoint(int x, int y)
      {
          return ((xCoord &lt;= x &amp;&amp; x &lt;= (xCoord + width)) &amp;&amp; (yCoord &lt;= y &amp;&amp; y &lt;= (yCoord + length)));
      }
  
      public void move(int xAmount, int yAmount)
      {
          xCoord = xCoord + xAmount;
          yCoord = yCoord + yAmount;
      }
  
  }
  </code></pre>