Mike
Mike

Reputation: 87

How to make a paintComponent follow your mouse?

The following is what I have, I have a paintComponent method in one class,

public void paintComponent(Graphics g) {
        g2.setPaint(Color.red);
        g2.fillRect(100, 100, 50, 50);
}

I want to make that graphics object(above) follow my mouse in a second class, but I do not know how to call it in my second class(below), I wrote an constructor of the first class, but I don't know how to make it show up on my frame. P.S. I added mouseMotionListener to my frame

public void mouseMoved(MouseEvent e) {
    GOLDraw g1 = new GOLDraw();//default constructor from the first class
    repaint();
}

Please explain in simple terms on how to call the paintComponent method, and why(I'll try to understand it, I don't know much about inheritances and such). Probably because I'm a beginner and I'm doing this wrong, I found nothing after hours of reading the api and google.

public class GolPresets extends JComponent implements MouseMotionListener{  


    public GolPresets() {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    Point point;
    @Override
    public void mouseMoved(MouseEvent e){
       point = e.getPoint();
    }


    public void paintComponent(Graphics g) {
            g.drawRect(point.x, point.y, 100, 100);
    }

    public void GUI() {
        JFrame frame = new JFrame("");
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.add(new GolPresets());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new GolPresets());
        frame.addMouseMotionListener(this);

    }

    public static void main(String[] args) {
        GolPresets g = new GolPresets();
        g.GUI();
    }

}

Upvotes: 0

Views: 1221

Answers (3)

FredK
FredK

Reputation: 4084

Here's an example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseFollower extends JPanel {

   protected Point spot;

   public MouseFollower() {
      final MouseFollower me = this;
      addMouseMotionListener( new MouseMotionAdapter() {
         @Override
         public void mouseMoved( MouseEvent e ) {
            spot = e.getPoint();
            repaint();
         }
      } );

      addMouseListener( new MouseAdapter() {
         @Override
         public void mouseExited( MouseEvent e ) {
            spot = null;
            repaint();
         }
      } );

      setPreferredSize( new Dimension( 300, 300 ) );
   }

   @Override
   public void paintComponent( Graphics g ) {
      super.paintComponent( g );
      if ( spot != null ) {
         Graphics2D g2 = (Graphics2D)g;
         g2.setColor( Color.red );
         g2.fillRect( spot.x, spot.y, 50, 50 );
      }
   }

   public static void main( String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         public void run() {
            JFrame win = new JFrame( "MouseFollower" );
            final MouseFollower mf = new MouseFollower();
            win.add( mf );
            win.pack();
            win.addWindowListener( new WindowAdapter() {
               @Override
               public void windowClosing( WindowEvent arg0 ) {
                  System.exit( 0 );
               }
            } );

            win.setVisible( true );
         }
      } );
   }
}

Upvotes: 0

martinez314
martinez314

Reputation: 12332

For example:

Point lastCursorPoint;

public void mouseMoved(MouseEvent e) {
    lastCursorPoint = e.getPoint();
    repaint();
}

public void paintComponent(Graphics g) {
    if (lastCursorPoint != null) {
        g2.setPaint(Color.red);
        g2.fillRect(lastCursorPoint.x, lastCursorPoint.y, 50, 50);
    }
}

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

Well, where does the shared state live? You will need to keep track of the cursor position in your mouseMoved method; you should just reuse the already-created component (not a new one every time) and invoke the repaint() method on that.

Upvotes: 0

Related Questions