Class A has a MouseMotionListener which updates cursorLocation every time its called;
Class B extends A. the idea is to make B's lastSelected update once cursorLocation is updated and println the new coords. i thought paintComponent could be used for that but im sure theres something obvious im missing:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class A extends JPanel{
Point cursorLocation;
A() {
setPreferredSize(new Dimension(200,200));
addMouseMotionListener(new MouseMotionHandler());
}
private class MouseMotionHandler extends MouseMotionAdapter {
public void mouseMoved(MouseEvent e) { cursorLocation = new Point(e.getX(), e.getY());}
}
}
import java.awt.*;
import javax.swing.*;
public class B extends A{
Point lastSelected;
static public void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
B b = new B();
frame.getContentPane().add(BorderLayout.CENTER,b);
frame.setVisible(true);
}
B() {
super();
}
/*
public void paintComponent(Graphics g) {
if (lastSelected != cursorLocation) { lastSelected = new Point(cursorLocation); repaint(); }
System.out.println(lastSelected);
*/
}
}