/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import org.freehep.graphics2d.VectorGraphics;

/** A zoom on the canvas. */
public class JaxoZoom {
    private Graphics g;
    private Image background;
    private Image tmpImage;
    private int imageWidth;
    private int imageHeight;
    private int canvasWidth;
    private int canvasHeight;
    private int zoomWindowX = 0;
    private int zoomWindowY = 0;
    private int zoomWindowWidth;
    private int zoomWindowHeight;
    private int zoomFactor;

    /** Constructor. */
    public JaxoZoom() {
    }

    /** Sets the zoom factor.
     * @param zf The zoom factor.
     */
    public final void setZoomFactor(int zf) {
        this.zoomFactor = zf;
    }

    /** Returns the zoom factor.
     * @return The zoom factor.
     */
    public final int getZoomFactor() {
        return this.zoomFactor;
    }

    /** Sets the width of the zoom window.
     * @param zwWidth The width of the zoom window.
     */
    public final void setZoomWindowWidth(int zwWidth) {
        this.zoomWindowWidth = zwWidth;
    }

    /** Returns the width of the zoom window.
     * @return The width of the zoom window.
     */
    public final int getZoomWindowWidth() {
        return this.zoomWindowWidth;
    }

    /** Sets the height of the zoom window.
     * @param zwHeight The width of the zoom window.
     */
    public final void setZoomWindowHeight(int zwHeight) {
        this.zoomWindowHeight = zwHeight;
    }

    /** Returns the height of the zoom window.
     * @return The height of the zoom window.
     */
    public final int getZoomWindowHeight() {
        return this.zoomWindowHeight;
    }

    /** Sets the width of the image.
     * @param iWidth The width of the image.
     */
    public final void setImageWidth(int iWidth) {
        this.imageWidth = iWidth;
    }

    /** Returns the width of the image.
     * @return The width of the image.
     */
    public final int getImageWidth() {
        return this.imageWidth;
    }

    /** Sets the height of the image.
     * @param iHeight The width of the image.
     */
    public final void setImageHeight(int iHeight) {
        this.imageHeight = iHeight;
    }

    /** Returns the height of the image.
     * @return The height of the image.
     */
    public final int getImageHeight() {
        return this.imageHeight;
    }

    /** Sets the width of the canvas
     * (the portion of the image that is visible).
     * @param cWidth The width of the canvas.
     */
    public final void setCanvasWidth(int cWidth) {
        this.canvasWidth = cWidth;
    }

    /** Returns the width of the canvas
     * (the portion of the image that is visible).
     * @return The width of the canvas.
     */
    public final int getCanvasWidth() {
        return this.canvasWidth;
    }

    /** Sets the height of the canvas
     * (the portion of the image that is visible).
     * @param cHeight The height of the canvas.
     */
    public final void setCanvasHeight(int cHeight) {
        this.canvasHeight = cHeight;
    }

    /** Returns the height of the canvas
     * (the portion of the image that is visible).
     * @return The height of the canvas.
     */
    public final int getCanvasHeight() {
        return this.canvasHeight;
    }

    /** Sets the x-coordinate of the ZoomWindow's origin.
     * @param x The x-coordinate of the ZoomWindow's origin.
     */
    public final void setZoomWindowX(int x) {
        this.zoomWindowX = x;
    }

    /** Returns the x-coordinate of the ZoomWindow's origin.
     * @return The x-coordinate of the ZoomWindow's origin.
     */
    public final int getZoomWindowX() {
        return this.zoomWindowX;
    }

    /** Sets the y-coordinate of the ZoomWindow's origin.
     * @param y The y-coordinate of the ZoomWindow's origin.
     */
    public final void setZoomWindowY(int y) {
        this.zoomWindowY = y;
    }

    /** Returns the y-coordinate of the ZoomWindow's origin.
     * @return The y-coordinate of the ZoomWindow's origin.
     */
    public final int getZoomWindowY() {
        return this.zoomWindowY;
    }

    /** Sets the background image for the zoom.
     * @param bg The background image.
     */
    public final void setBackground(Image bg) {
        this.background = bg;
    }

    /** Sets the temporary image that is used for double-buffering the zoom. */
    public final void setTmpImage() {
        if (tmpImage == null) {
            createTmpImage();
        } else {
            Graphics gTmp = tmpImage.getGraphics();
            gTmp.drawImage(background, 0, 0, null);
        }
    }

    /** Returns the background image of the zoom.
     * @return The background image.
     */
    public final Image getBackground() {
        return this.background;
    }

    /** Sets the graphics context for the zoom.
     * @param gr The graphics context.
     */
    public final void setGraphics(Graphics gr) {
        this.g = gr;
    }

    /** Returns the graphics context of the zoom.
     * @return The graphics context.
     */
    public final Graphics getGraphics() {
        return this.g;
    }

    /** Draws the zoom by clipping to the zoom window and scaling the
     * background image.
     */
    public final void drawZoomWindow() {
        Graphics g2 = g.create();

        if (tmpImage == null) {
            createTmpImage();
        }

        Graphics gTmp = tmpImage.getGraphics();

        gTmp.clipRect(zoomWindowX, zoomWindowY, zoomWindowWidth,
            zoomWindowHeight);
        gTmp.drawImage(background,
            (1 - zoomFactor) * (zoomWindowX + (zoomWindowWidth / 2)),
            (1 - zoomFactor) * (zoomWindowY + (zoomWindowHeight / 2)),
            imageWidth * zoomFactor, imageHeight * zoomFactor, null);

        gTmp.setColor(JaxoColor.BLACK);
        gTmp.drawRect(zoomWindowX, zoomWindowY, zoomWindowWidth - 1,
            zoomWindowHeight - 1);
        gTmp.setClip(null);

        g2.drawImage(tmpImage, 0, 0, null);
    }

    /** Erases the zoom window. */
    public final void eraseZoomWindow() {
        VectorGraphics g2 = VectorGraphics.create(g); 
        Graphics gTmp = tmpImage.getGraphics();

        gTmp.clipRect(zoomWindowX, zoomWindowY, zoomWindowWidth,
            zoomWindowHeight);
        gTmp.drawImage(background, 0, 0, null);
        gTmp.setClip(null);
        g2.drawImage(tmpImage, 0, 0, null);
    }

    /** Adjusts the zoom when the zoom window gets dragged.
     * @param oldX The old x-coordinate of the zoom window.
     * @param oldY The old y-coordinate of the zoom window.
     * @param newX The new x-coordinate of the zoom window.
     * @param newY The new y-coordinate of the zoom window.
     */
    public final void adjustZoomWindow(int oldX, int oldY, int newX, int newY) {
        Graphics gTmp = tmpImage.getGraphics();

        if ((newX <= oldX) && (newY <= oldY)) {
            gTmp.clipRect(newX + zoomWindowWidth, oldY, oldX - newX,
                (newY + zoomWindowHeight) - oldY);
            gTmp.drawImage(background, 0, 0, null);
            gTmp.setClip(null);
            gTmp.clipRect(oldX, newY + zoomWindowHeight, zoomWindowWidth,
                oldY - newY);
            gTmp.drawImage(background, 0, 0, null);
        } else if ((newX > oldX) && (newY <= oldY)) {
            gTmp.clipRect(oldX, oldY, newX - oldX,
                (newY + zoomWindowHeight) - oldY);
            gTmp.drawImage(background, 0, 0, null);
            gTmp.setClip(null);
            gTmp.clipRect(oldX, newY + zoomWindowHeight, zoomWindowWidth,
                oldY - newY);
            gTmp.drawImage(background, 0, 0, null);
        } else if ((newX > oldX) && (newY > oldY)) {
            gTmp.clipRect(oldX, oldY, zoomWindowWidth, newY - oldY);
            gTmp.drawImage(background, 0, 0, null);
            gTmp.setClip(null);
            gTmp.clipRect(oldX, newY, newX - oldX,
                (oldY + zoomWindowHeight) - newY);
            gTmp.drawImage(background, 0, 0, null);
        } else { // if (newX <= oldX && newY > oldY)
            gTmp.clipRect(oldX, oldY, zoomWindowWidth, newY - oldY);
            gTmp.drawImage(background, 0, 0, null);
            gTmp.setClip(null);
            gTmp.clipRect(newX + zoomWindowWidth, newY, oldX - newX,
                (oldY + zoomWindowHeight) - newY);
            gTmp.drawImage(background, 0, 0, null);
        }
    }

    /** Drags the zoom when the zoom window gets dragged.
     * @param eX The x-coordinate of the center of the zoom window
     * (where the mouse event occurred).
     * @param eY The y-coordinate of the center of the zoom window.
     */
    public final void dragZoomWindow(int eX, int eY) {
        int oldX = zoomWindowX;
        int oldY = zoomWindowY;
        int newX = eX - (zoomWindowWidth / 2);
        int newY = eY - (zoomWindowHeight / 2);

        adjustZoomWindow(oldX, oldY, newX, newY);

        zoomWindowX = newX;
        zoomWindowY = newY;

        if (zoomWindowX > (canvasWidth - (zoomWindowWidth / 2))) {
            zoomWindowX = canvasWidth - (zoomWindowWidth / 2);
        } else if (zoomWindowX < (-zoomWindowWidth / 2)) {
            zoomWindowX = -zoomWindowWidth / 2;
        }

        if (zoomWindowY > (canvasHeight - (zoomWindowHeight / 2))) {
            zoomWindowY = canvasHeight - (zoomWindowHeight / 2);
        } else if (zoomWindowY < (-zoomWindowHeight / 2)) {
            zoomWindowY = -zoomWindowHeight / 2;
        }

        drawZoomWindow();
    }

    private void createTmpImage() {
        VectorGraphics g2D = VectorGraphics.create(g);
        BufferedImage bimage =
            g2D.getDeviceConfiguration().createCompatibleImage(imageWidth,
                imageHeight, Transparency.OPAQUE);
        tmpImage = (Image) bimage;

        Graphics gTmp = tmpImage.getGraphics();
        gTmp.drawImage(background, 0, 0, null);
    }
}

 

Ads help maintain this website.