Source code for CircleLayer.java

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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layers;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author pat
 */
public class CircleLayer implements Layer {

    private static final int R = 20; // Radii of disks
    // This list records the places the user clicks.
    private LinkedList<Point2D> click_locations = new LinkedList<Point2D>();
    private LayeredPanel layered_panel;

    public CircleLayer(LayeredPanel lp) {
        layered_panel = lp;
    }

    @Override
    public void render(Graphics2D g) {
        g.setStroke(new BasicStroke(2));

        double hue = 0; // for picking different colors. 
        for (Point2D pt : click_locations) {
            Ellipse2D e = new Ellipse2D.Double(pt.getX() - R, pt.getY() - R, 2 * R, 2 * R);
            g.setColor(Color.getHSBColor((float) hue, 1f, 1f));
            g.fill(e);
            g.setColor(Color.BLACK);
            g.draw(e);

            hue = hue + (Math.sqrt(5) - 1) / 2;
            hue = hue - Math.floor(hue);
        }
    }

    @Override
    public void renderActive(Graphics2D g) {
        g.setColor(Color.BLACK);
        g.drawString("Click somewhere to add a disk. You can also drag disks.", 10, 20);
        
        if ( (mouse_location != null) && (! disk_selected) ) {
            g.setColor(Color.GRAY);
            g.setStroke(new BasicStroke(5));
            Ellipse2D e = new Ellipse2D.Double(mouse_location.getX() - R, mouse_location.getY() - R, 2 * R, 2 * R);
            g.draw(e);
        }
    }
    Point2D mouse_location = null;

    @Override
    public void mouseClicked(MouseEvent me) {
        mouse_location = me.getPoint();
        click_locations.addLast(me.getPoint());
        layered_panel.repaintAllLayers();
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        mouse_location = me.getPoint();
        layered_panel.repaint();
    }

    @Override
    public void mouseExited(MouseEvent me) {
        mouse_location = null;
        layered_panel.repaint();
    }


    boolean disk_selected=false;
    int selected_disk_index=0;
    Point2D last_mouse_location;
    
    @Override
    public void mousePressed(MouseEvent me) {
        mouse_location = me.getPoint();
        
        Iterator<Point2D> it=click_locations.descendingIterator();
        int index=click_locations.size()-1;
        while (it.hasNext()) {
            if (me.getPoint().distance(it.next())<R) {
                disk_selected=true;
                selected_disk_index=index;
                last_mouse_location=me.getPoint();
                break; // quit the while loop.
            }
            index = index-1;
        }
    }
    
    @Override
    public void mouseDragged(MouseEvent me) {
        mouse_location = me.getPoint();
        
        if (disk_selected) {
            Point2D current_center=click_locations.get(selected_disk_index);
            Point2D new_center=new Point2D.Double(
                    current_center.getX()+me.getX()-last_mouse_location.getX(),
                    current_center.getY()+me.getY()-last_mouse_location.getY());
            click_locations.set(selected_disk_index, new_center);
            last_mouse_location=me.getPoint();
            layered_panel.repaintAllLayers();            
        }        
    }
    
    @Override
    public void mouseReleased(MouseEvent me) {
        mouse_location = me.getPoint();
        disk_selected=false;        
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        mouse_location = me.getPoint();
        layered_panel.repaint();
    }
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 17, 2018.
[check html] [check css]