Source code for SimpleInteractionDemo.java

What follows is the highlighted source code with comments. You can also download this file directly: SimpleInteractionDemo.java.

package simplermouseinteraction;

/*
 * This work by W. Patrick Hooper is free of known copyright restrictions.
 * The work is in the public domain.
 * 
 * Author's website: <a href="http://wphooper.com">http://wphooper.com</a>. 
 */

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** This class was created to demonstrate how a graphical program can interact
 * with the user's mouse. 
 * 
 * The program allows the user to manipulate a disk in the plane. When the mouse
 * is clicked it recenters the disk. The center can also be dragged around.
 * A point is plotted on the boundary of the disk. Dragging this point allows the
 * user to modify the radius of the disk.
 * 
 * @author pat
 */
public class SimpleInteractionDemo extends JPanel implements MouseListener, MouseMotionListener {

    // Data for out circle:
    Point2D center=new Point2D.Double(200,200);
    double radius=150;
    
    // The radius of the draggable points:
    int pt_radius=5;
    
    public SimpleInteractionDemo() {
        setBackground(Color.WHITE); // Make the background color white

        // Necessary for mouse interaction:
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    ///// DRAWING
    /**
     * Draw the disks.
     */
    @Override
    public void paintComponent(Graphics gfx) {
        // This stuff is standard, and should be in any paintComponent method. 
        super.paintComponent(gfx);
        Graphics2D g = (Graphics2D) gfx;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.BLACK);
        g.drawString("Click somewhere to recenter the disk. You can also drag the red points.", 10, 20);
        
        // This is the circle with our center and radius:
        Ellipse2D e = new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, 
                2*radius, 2*radius);
        g.setColor(Color.BLUE);
        g.fill(e);
        g.setStroke(new BasicStroke(2));
        g.setColor(Color.BLACK);
        g.draw(e);
        
        // Plot the two movable points:
        // This disk allows the user to drag the center.
        Ellipse2D center_disk=new Ellipse2D.Double(
                center.getX() - pt_radius, center.getY() - pt_radius, 
                2*pt_radius, 2*pt_radius);
        g.setColor(Color.RED);
        g.fill(center_disk);
        g.setStroke(new BasicStroke(0.5f));
        g.setColor(Color.BLACK);
        g.draw(center_disk);
        
        // This disk allows the user to change the radius.
        Ellipse2D radial_disk=new Ellipse2D.Double(
                center.getX() + radius  - pt_radius, center.getY() - pt_radius, 
                2*pt_radius, 2*pt_radius);
        g.setColor(Color.RED);
        g.fill(radial_disk);
        g.setStroke(new BasicStroke(0.5f));
        g.setColor(Color.BLACK);
        g.draw(radial_disk);
    }

    ///// MOUSE INTERACTION
    // Implementation of MouseListener
    @Override
    public void mouseClicked(MouseEvent me) {
        center=me.getPoint(); // Update move the center to the point clicked.
        repaint(); // Redraw the image.
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    boolean center_dragging=false;
    boolean radius_dragging=false;
    
    @Override
    public void mousePressed(MouseEvent me) {
        Point2D radial_pt=new Point2D.Double(center.getX()+radius, center.getY());
        if (me.getPoint().distance(radial_pt) < pt_radius) {
            radius_dragging=true;
            return;
        }
        if (me.getPoint().distance(center) < pt_radius) {
            center_dragging=true;
            return;
        }
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        center_dragging=false;
        radius_dragging=false;
    }

    // Implementation of MouseMotionListener
    @Override
    public void mouseDragged(MouseEvent me) {
        if (center_dragging) {
            center=me.getPoint();
            repaint();
            return;
        }
        if (radius_dragging) {
            radius=Math.abs(me.getPoint().getX()- center.getX());
            repaint();
            return;
        }
    }

    @Override
    public void mouseMoved(MouseEvent me) {        
    }

    public static void main(String[] args) {
        // Construct a new window:
        JFrame frame = new JFrame("Simple Interaction Demo");
        // Dimensions of the window in pixels:
        frame.setSize(640, 480);
        // Quit the program when the window is closed:
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // The window will contain only our panel:
        frame.add(new SimpleInteractionDemo());
        // Make the window visible:
        frame.setVisible(true);
    }
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 25, 2021.
[check html] [check css]