import uwcse.graphics.*; import java.awt.Color; // Display some graphics at the location of the mouse click // (here a filled circle of radius 30. The color of the circle is random) public class MouseHandler extends GWindowEventAdapter { // Graphics window private GWindow window; public MouseHandler() { // Create a graphics window window = new GWindow("Using the mouse"); // Terminate the application when closing the window window.setExitOnClose(); // Send all of the events to this MouseHandler window.addEventHandler(this); } // Methods to manage the mouse clicks /** * Invoked when a mouse button has been pressed on the window. */ public void mousePressed(GWindowEvent e) { // mouse coordinates int xMouse = e.getX(); int yMouse = e.getY(); // Print the coordinates of the click System.out.println("(x,y)=(" + xMouse + "," + yMouse + ")"); // Display a circle of radius 30 centered at the click location. // The color of the circle is random. Color color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); int radius = 30; Oval circle = new Oval(xMouse - radius, yMouse - radius, 2 * radius, 2 * radius, color, true); window.add(circle); // Show the circle window.doRepaint(); } /** * Starts the application */ public static void main(String[] args) { new MouseHandler(); } }