title: How would I separate the MotionListener to a different class?
body: <p>I'm trying to teach myself java and I've gotten stumped. I've either been practicing too much today or I'm just having a dumb moment. </p>
  
  <p>I'm playing around with paint and MouseMotionListener to be able to drag graphics across the screen and I wanted to make the MouseMotionListener as a seperate class.</p>
  
  <p>On to the question:</p>
  
  <p>How would I separate the listener in my code into a separate class? When I tried to put it in another class I just ended up making a circular reference. </p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Point;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionListener;
  
  import javax.swing.JFrame;
  
  
  @SuppressWarnings("serial")
  class Class1 extends JFrame implements MouseMotionListener{
  
  Point p,p2;
  
  
  public Class1(){
      p = new Point(0,0);
      this.setSize(500,500);
      //this.setUndecorated(true);
      //this.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.01f));
      this.addMouseMotionListener(this);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  
  public static void main(String[]args){
      new Class1();
  }
  
  public void paint(Graphics g){
      g.setColor(Color.gray);
      g.fillRect(0, 0, 500, 500);
  }
  
  public void paintSquare(Graphics g){
      g.fillRect(p.x, p.y, 50, 50);
  }
  
  public void paintCover(Graphics g){
      g.setColor(Color.gray);
      g.fillRect(p2.x, p2.y, 50, 50);
  }
  
  @Override
  public void mouseDragged(MouseEvent e) {
      p2=p;
      p=e.getPoint();
      p.translate(-25, -25);
      paintCover(this.getGraphics());
      paintSquare(this.getGraphics());
  }
  
  @Override
  public void mouseMoved(MouseEvent e) {
      //do nothing
  }
  
  }
  </code></pre>