INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Zoom.java.


/*************************************************************************
 *  Compilation:  javac Zoom.java
 *  Execution:    java Zoom
 *  Dependencies: InteractiveDraw.java DrawListener.java
 * 
 *  User clicks and drags to draw filled circles of different 
 *  diameters and colors. Uses XOR mode to draw and erase
 *  the circle as it is being drawn.
 *
 *  Remarks
 *  -------
 *   - the dragging boolean variable is needed since mouseDragged
 *     can be true without a mouseClicked in the current window region
 *
 *************************************************************************/

import java.awt.Color;

public class Zoom implements DrawListener {
    private Point p;                    // center of circle
    private double diameter;            // diameter of circle to draw
    private boolean dragging = false;   // is the user dragging the mouse?
    private InteractiveDraw d;          // the drawing object

    public Zoom() {
        int SIZE = 512;
        d = new InteractiveDraw(SIZE, SIZE);
        d.setScale(0, 0, SIZE, SIZE);
        d.clear(Color.lightGray);
        d.addListener(this);
        d.show();
    }

    // user clicks the mouse at (x, y)
    public void mousePressed(double x, double y) {
System.out.println(dragging);
        p = new Point(x, y);
        d.go(x, y);
        d.setColorRandom();
        diameter = 0;
        d.spot(diameter);
        d.show();
        d.XOROn();
        dragging = true;
    }

    // user is dragging the mouse, currently at (x, y)
    public void mouseDragged(double x, double y) {
System.out.println(dragging);
        if (!dragging) return;
        d.spot(diameter);
        Point q = new Point(x, y);
        diameter = 2 * p.distanceTo(q);
        d.spot(diameter);
        d.show();
    }

    // user releases the mouse at location (x, y)
    public void mouseReleased(double x, double y) {
System.out.println(dragging);
        if (!dragging) return;
        d.XOROff();
        d.spot(diameter);
        d.show();
        dragging = false;
    }

    // save image to file
    public void keyTyped(char c) { d.save("zoom" + c + ".png"); }

    // test client
    public static void main(String args[]) {
        Zoom zoom = new Zoom();
    }
 
   
}


Last updated: Sat May 22 00:23:45 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.