/** * ClickableWindow starts with the simple JFrame subclass * found in CircleWindow, and shows how to implement a * standard interface--in this case, the MouseListener * interface. Any class that implements the MouseListener * interface must provide implementations of the five * MouseListener methods: mouseEnter, mouseExit, mousePressed, * mouseReleased, and mouseClicked. In exchange for this * modest effort, those methods will get called whenever the * corresponding mouse-related event occurs in your * ClickableWindow object. * * Note one pitfall: it's easy to forget to call addListener. * You can implement all the right methods, but they won't * ever get called unless you first tell the Java Virtual * Machine that you want them to get called. That's what the * addListener(this) call is for in the constructor below. * * @author Jeff Ondich, 1/12/04 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ClickableWindow extends JFrame implements MouseListener { private Point center; public static void main( String[] args ) { ClickableWindow cw = new ClickableWindow(); } public ClickableWindow() { setSize( 1000, 800 ); setLocation( 50, 50 ); setVisible( true ); center = new Point( 500, 400 ); addMouseListener( this ); } public void paint( Graphics g ) { Rectangle r = getBounds(); g.clearRect( 0, 0, r.width, r.height ); g.setColor( Color.green ); g.fillOval( center.x - 150, center.y - 150, 300, 300 ); } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { center = e.getPoint(); repaint(); } public void mouseReleased( MouseEvent e ) { } }