/*

 * 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 jhplot.jadraw;


import japlot.jaxodraw.JaxoBlobOptionsPanel;
import japlot.jaxodraw.JaxoColor;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Composite;
import java.awt.Dimension;
import org.freehep.graphics2d.VectorGraphics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;


/** A blob object: a general ellypse that can be rotated. */
public class JaBlob extends JaFillObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** The rotation angle of the ellipse */
    private int rotAngle;
    private float transp;
    private boolean fill;

    /** Constructor: sets the width, height, getRelw() and getRelh() to a
     * default value of 25.
     */
    public JaBlob() {
        setRelw(INIT_SIZE);
        setRelh(INIT_SIZE);
        rotAngle=0;
        transp=1.0f;
        fill=true;
    }

    // Bean getter and setter methods

    /** Returns the rotation angle of this blob.
     * @return The rotation angle of this blob
     */
    public final int getRotAngle() {
        return rotAngle;
    }

    /** Sets the rotation angle of this blob.
     * @param newangle The rotation angle of this blob
     */
    public final void setRotAngle(int newangle) {
        this.rotAngle = newangle;
    }

    
    /**
     * Set transparency of this object
     * @param transp transparency from 0 to 1
     */
    public void setTransparency(float transp) {
		this.transp = transp;
	}
    
    
    
    /**
     * Fill or not the object
     * @param fill true is filled
     */
    public void setFilled(boolean fill) {
		this.fill = fill;
	}
    
    
   
    /**
     * Get the filled style
     * @return
     */
    
    public boolean isFilled() {
		return fill;
	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * Get the transparency of it
     * @return transparency
     */
    public float getTransparency() {
		return transp;
	}
    
    
    
    
    /** Returns an exact copy of this JaBlob.
     * @return A copy of this JaBlob.
     */
    public final JaObject copy() {
        JaBlob temp = new JaBlob();

        temp.setX(getX());
        temp.setY(getY());
        temp.setColor(getColor());
        temp.setFilled(isFilled());
        temp.setTransparency(getTransparency());
        temp.setFillColor(getFillColor());
        temp.setStroke(getStroke());
        temp.setRotAngle(getRotAngle());
        temp.setSize(getWidth(), getHeight(), getRelw(), getRelh());
        temp.setBoundingBox(getBoundingBox());

        return temp;
    }

    /** Returns true if all serializable variables of this JaObject
     *  and those of the specified one are equal.
     * @param comp A JaObject to compare with.
     * @return True if the objects are equal, false otherwise.
     */
    public final boolean isCopy(JaObject comp) {
        boolean isCopy = false;

        if (comp instanceof JaBlob) {
            JaBlob temp = (JaBlob) comp;
            if ((temp.getX() == getX()) && (temp.getY() == getY())
                    && (temp.getColor().equals(getColor()))
                    && (temp.getFillColor().equals(getFillColor()))
                    && (temp.getStroke() == getStroke())
                    && (temp.getRotAngle() == getRotAngle())
                    && (temp.isFilled() == isFilled())
                    && (temp.getRelw() == getRelw())
                    && (temp.getRelh() == getRelh())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** Draws a visual aid during the dragging of a blob object,
     *  which is a red cross at the center.
     * @param g2 The graphics context to draw to.
     */
    public final void drawVisualAid(VectorGraphics g2) {
        g2.setColor(JaxoColor.RED);
        g2.setStroke(new BasicStroke(1.3f));

        if ((getRelw() > 0) && (getRelh() >= 0)) {
            g2.drawLine(getX() - (LENGTH + 4), getY(), getX() + LENGTH + 4,
                getY());
            g2.drawLine(getX(), getY() - (LENGTH + 4), getX(),
                getY() + LENGTH + 4);
        }

        if ((getRelw() <= 0) && (getRelh() > 0)) {
            g2.drawLine((getX() + getWidth()) - (LENGTH + 4), getY(),
                getX() + getWidth() + (LENGTH + 4), getY());
            g2.drawLine(getX() + getWidth(), getY() - (LENGTH + 4),
                getX() + getWidth(), getY() + (LENGTH + 4));
        }

        if ((getRelw() < 0) && (getRelh() <= 0)) {
            g2.drawLine(getX() + getWidth(),
                (getY() + getHeight()) - (LENGTH + 4), getX() + getWidth(),
                getY() + getHeight() + (LENGTH + 4));
            g2.drawLine((getX() + getWidth()) - (LENGTH + 4),
                getY() + getHeight(), getX() + getWidth() + (LENGTH + 4),
                getY() + getHeight());
        }

        if ((getRelw() >= 0) && (getRelh() < 0)) {
            g2.drawLine(getX() - (LENGTH + 4), getY() + getHeight(),
                getX() + (LENGTH + 4), getY() + getHeight());
            g2.drawLine(getX(), (getY() + getHeight()) - (LENGTH + 4), getX(),
                getY() + getHeight() + (LENGTH + 4));
        }
    }

    /** Draws a visual aid during the dragging of a blob object,
     *  which is a red cross at the center.
     * @param g2 The graphics context to draw to
     */
    public final void drawHandles(VectorGraphics g2) {
        g2.setColor(JaxoColor.RED);
        g2.setStroke(new BasicStroke(1.3f));

        if (((getRelw() > 0) && (getRelh() >= 0))
                || ((getRelw() < 0) && (getRelh() <= 0))) {
            g2.drawRect(getX() - (LENGTH / 2), getY() - (LENGTH / 2), LENGTH,
                LENGTH);
            g2.drawRect((getX() + getWidth()) - (LENGTH / 2),
                (getY() + getHeight()) - (LENGTH / 2), LENGTH, LENGTH);

            if (isMarked()) {
                g2.setColor(JaxoColor.GRAYSCALE150);
                g2.fillRect(getX() - (LENGTH / 2) + 1,
                    getY() - (LENGTH / 2) + 1, LENGTH - 1, LENGTH - 1);
                g2.fillRect((getX() + getWidth()) - (LENGTH / 2) + 1,
                    (getY() + getHeight()) - (LENGTH / 2) + 1, LENGTH - 1,
                    LENGTH - 1);
            }
        }

        if (((getRelw() <= 0) && (getRelh() > 0))
                || ((getRelw() >= 0) && (getRelh() < 0))) {
            g2.drawRect(getX() - (LENGTH / 2),
                (getY() + getHeight()) - (LENGTH / 2), LENGTH, LENGTH);
            g2.drawRect((getX() + getWidth()) - (LENGTH / 2),
                getY() - (LENGTH / 2), LENGTH, LENGTH);

            if (isMarked()) {
                g2.setColor(JaxoColor.GRAYSCALE150);
                g2.fillRect(getX() - (LENGTH / 2) + 1,
                    (getY() + getHeight()) - (LENGTH / 2) + 1, LENGTH - 1,
                    LENGTH - 1);
                g2.fillRect((getX() + getWidth()) - (LENGTH / 2) + 1,
                    getY() - (LENGTH / 2) + 1, LENGTH - 1, LENGTH - 1);
            }
        }
    }

    /** Determines where on this JaObject a mouse click has ocurred.
     * @param clickX The x position of the point where the mouse click ocurred.
     * @param clickY The y position of the point where the mouse click ocurred.
     * @param editmode The current edit mode as defined in JaxoMainPanel.
     * @return An integer specifying whether the click ocurred on one of
     * the handles and if yes, on which.
     */
    public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
        if ((editmode == MOVE) || (editmode == COPY)) {
            if ((getRelw() > 0) && (getRelh() >= 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    //center = true;
                    return SELECT_BODY;
                }

                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    //center = false;
                    return SELECT_BODY;
                }
            }

            if ((getRelw() < 0) && (getRelh() <= 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    //center = false;
                    return SELECT_BODY;
                }

                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    //center = true;
                    return SELECT_BODY;
                }
            }

            if ((getRelw() >= 0) && (getRelh() < 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    //center = true;
                    return SELECT_BODY;
                }

                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    //center = false;
                    return SELECT_BODY;
                }
            }

            if ((getRelw() <= 0) && (getRelh() > 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    //center = false;
                    return SELECT_BODY;
                }

                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    //center = true;
                    return SELECT_BODY;
                }
            }

            return SELECT_NONE;
        } else if (editmode == RESIZE) {
            //center = false;
            if ((getRelw() > 0) && (getRelh() >= 0)) {
                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    return SELECT_LR;
                }
            } else if ((getRelw() < 0) && (getRelh() <= 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    return SELECT_UL;
                }
            } else if ((getRelw() >= 0) && (getRelh() < 0)) {
                if ((clickX >= ((getX() + getWidth()) - (LENGTH / 2)))
                        && (clickX <= (getX() + getWidth() + (LENGTH / 2)))
                        && (clickY >= (getY() - (LENGTH / 2)))
                        && (clickY <= (getY() + (LENGTH / 2)))) {
                    return SELECT_UR;
                }
            } else if ((getRelw() <= 0) && (getRelh() > 0)) {
                if ((clickX >= (getX() - (LENGTH / 2)))
                        && (clickX <= (getX() + (LENGTH / 2)))
                        && (
                            clickY >= ((getY() + getHeight()) - (LENGTH / 2))
                        ) && (
                            clickY <= (getY() + getHeight() + (LENGTH / 2))
                        )) {
                    return SELECT_LL;
                }
            }

            return SELECT_NONE;
        }

        return SELECT_NONE;
    }

    /** The method that draws this JaBlob.
     * @param g2 The graphics context where the JaBlob has to be drawn.
     * @param drawToScreen A boolean variable that indicates whether the
     * drawing is done on the screen or somewhere else. This is used for
     * exporting/printing, where the object handles should not be painted,
     * even if they are visible on the screen.
     */
    public final void jaxoDraw(VectorGraphics g2, boolean drawToScreen) {
        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(rotAngle), 0.f, 0.f);

        GeneralPath gp = getGeneralPath();
        gp.reset();

        Ellipse2D blob = new Ellipse2D.Float();
        blob.setFrame(-getWidth(), -getHeight(), 2 * getWidth(),
            2 * getHeight());
        gp.append(blob, false);
        gp.transform(at);
        gp = transc(gp, 1.f);

        Composite cold = g2.getComposite();
        Composite c = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, transp);
                g2.setComposite(c);
                
                
        g2.setColor(getFillColor());
        g2.setStroke(new BasicStroke(1.0f));
        if (fill) g2.fill(gp);
        gp.reset();
        gp.append(blob, false);
        gp.transform(at);
        gp = transc(gp, 1.f);
        g2.setColor(getColor());
        g2.setStroke(new BasicStroke(getStroke()));
        g2.draw(gp);

        Rectangle2D bb = gp.getBounds2D();
        double[] bbox =
        {bb.getMinX(), bb.getMinY(), bb.getMaxX(), bb.getMaxY()};
        setBoundingBox(bbox);
        
        g2.setComposite(cold);
        
    }

    /** The LaTeX command that is necessary to draw the given JaBlob
     * using the axodraw.sty package.
     * @param scale A scale factor to translate Java coordinates to
     * LaTeX coordinates.
     * @param canvasDim The current dimension of the canvas.
     * @return The corresponding axodraw LaTeX command.
     */
    public final String latexCommand(float scale, Dimension canvasDim) {
        int canvasHeight = canvasDim.height;

        Point2D center = getLaTexCenter(scale, canvasHeight);
        Point2D axes = getLaTexAxes(scale);
        int rotate = getLaTexRotation();
        String command = "";

        if (((int) axes.getX() == 0) && ((int) axes.getY() == 0)) {
            command = "%";
        } else {
            if (JaxoColor.isGrayScale(getFillColor())) {
                String grayScale = JaxoColor.getGreyScale(getFillColor());
                command =
                    "\\GOval" + "(" + D_FORMAT.format(center.getX()) + ","
                    + D_FORMAT.format(center.getY()) + ")" + "("
                    + D_FORMAT.format(axes.getY()) + ","
                    + D_FORMAT.format(axes.getX()) + ")" + "("
                    + D_FORMAT.format(rotate) + ")" + "{" + grayScale + "}";
            } else {
                String tlc = JaxoColor.getColorName(getColor());
                String tfc = JaxoColor.getColorName(getFillColor());
                command =
                    "\\COval" + "(" + D_FORMAT.format(center.getX()) + ","
                    + D_FORMAT.format(center.getY()) + ")" + "("
                    + D_FORMAT.format(axes.getY()) + ","
                    + D_FORMAT.format(axes.getX()) + ")" + "("
                    + D_FORMAT.format(rotate) + ")" + "{" + tlc + "}" + "{"
                    + tfc + "}";
            }
        }

        return command;
    }

    /** Rescales this JaBlob by the scale factor scale,
     * keeping the point (orx, ory) fixed.
     * @param orx The x - coordinate of the fixed point
     * @param ory The y - coordinate of the fixed point
     * @param scale The scale parameter
     */
    public final void rescaleObject(int orx, int ory, float scale) {
        //int oldWidth = this.getSize().width;
        //int oldHeight = this.getSize().height;
        //int newWidth = (int) Math.round(oldWidth * scale);
        //int newHeight = (int) Math.round(oldHeight * scale);
        int newRelWidth = (int) Math.round(this.getRelSize().width * scale);
        int newRelHeight = (int) Math.round(this.getRelSize().height * scale);

        Point2D newP =
            this.scalePoint(orx, ory, scale, this.getX(), this.getY());

        this.setX((int) Math.round(newP.getX()));
        this.setY((int) Math.round(newP.getY()));

        //this.setWAndH(newWidth, newHeight);
        this.setRelWAndH(newRelWidth, newRelHeight);
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   private auxiliary methods  //
    //
    /////////////////////////////////////////////////////////////////////////
    private GeneralPath transc(GeneralPath gpp, double scale) {
        AffineTransform at = new AffineTransform();

        if ((getRelw() >= 0) && (getRelh() >= 0)) {
            at.translate(getX(), getY());
            at.scale(scale, scale);
            gpp.transform(at);
        }

        if ((getRelw() >= 0) && (getRelh() < 0)) {
            at.translate(getX(), getY() + getHeight());
            at.scale(scale, scale);
            gpp.transform(at);
        }

        if ((getRelw() < 0) && (getRelh() <= 0)) {
            at.translate(getX() + getWidth(), getY() + getHeight());
            at.scale(scale, scale);
            gpp.transform(at);
        }

        if ((getRelw() < 0) && (getRelh() > 0)) {
            at.translate(getX() + getWidth(), getY());
            at.scale(scale, scale);
            gpp.transform(at);
        }

        return gpp;
    }

    private Point2D getLaTexCenter(float scaleFactor, int canvasHeight) {
        Point2D centerPoint = new Point2D.Float();
        float cx;
        float cy;

        if ((getRelw() > 0) && (getRelh() >= 0)) {
            cx = getX() / scaleFactor;
            cy = (canvasHeight - getY()) / scaleFactor;
            centerPoint.setLocation(cx, cy);
        } else if ((getRelw() >= 0) && (getRelh() < 0)) {
            cx = getX() / scaleFactor;
            cy = (canvasHeight - (getY() + getHeight())) / scaleFactor;
            centerPoint.setLocation(cx, cy);
        } else if ((getRelw() < 0) && (getRelh() <= 0)) {
            cx = (getX() + getWidth()) / scaleFactor;
            cy = (canvasHeight - (getY() + getHeight())) / scaleFactor;
            centerPoint.setLocation(cx, cy);
        } else if ((getRelw() <= 0) && (getRelh() > 0)) {
            cx = (getX() + getWidth()) / scaleFactor;
            cy = (canvasHeight - getY()) / scaleFactor;
            centerPoint.setLocation(cx, cy);
        }

        return centerPoint;
    }

    private Point2D getLaTexAxes(float scaleFactor) {
        Point2D axesVec = new Point2D.Float();
        float wdt;
        float hgt;

        wdt = getWidth() / scaleFactor;
        hgt = getHeight() / scaleFactor;
        axesVec.setLocation(wdt, hgt);

        return axesVec;
    }

    private int getLaTexRotation() {
        int rt = -rotAngle;

        return rt;
    }

    /** Brings up the edit panel that allows to change the parameters of
     * this object.
     * @return True if the editing actually changed the object, false if
     * the object has not been changed.
     */
    public final boolean editPanel() {
        JaxoBlobOptionsPanel blobop = new JaxoBlobOptionsPanel(this);

        return blobop.hasChanged();
    }
}

 

Ads help maintain this website.