www.pudn.com > Caliph_v0.9.13+Emirv0.8.5-src.zip > Arrow.java


/* 
 * This file is part of Caliph & Emir. 
 * 
 * Caliph & Emir 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. 
 * 
 * Caliph & Emir 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 Caliph & Emir; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 * 
 * Copyright statement: 
 * -------------------- 
 * (c) 2002-2004 by Mathias Lux (mathias@juggle.at) and the Know-Center Graz 
 * Inffeldgasse 21a, 8010 Graz, Austria 
 * http://www.know-center.at 
 */ 
 
/* 
 * Date: 31.07.2002 
 * Time: 09:49:20 
 */ 
package at.know.center.wv_wr.imb.objectcatalog.graphics; 
 
import java.awt.*; 
import java.awt.geom.*; 
 
/** 
 * Class derived from Shape to create an arrow. 
 * @author Mathias Lux, mathias@juggle.at 
 */ 
public class Arrow implements Shape { 
    private double ath = 4.0; 
    private Line2D line; 
    private double thickness; 
    private GeneralPath gp; 
 
    /** 
     * Main Constructor 
     * @param line is the line the arrow is painted along, Point 1 of the Line is the source, Point 2 of the line the target of the arrow. 
     * @param thickness gives a hint how bold the arrow is. 
     */ 
    public Arrow(Line2D line, double thickness) { 
        this.line = line; 
        this.thickness = thickness; 
        GeneralPath generalPath = new GeneralPath(); 
 
        Point2D sp = line.getP1(); 
        Point2D tp = line.getP2(); 
 
        double vx = (sp.getX() - tp.getX()); 
        double vy = (sp.getY() - tp.getY()); 
 
        double length = Math.sqrt((vx * vx) + (vy * vy)); 
        vx = vx / length; 
        vy = vy / length; 
 
        double nx = vy * thickness / 2; 
        double ny = -vx * thickness / 2; 
 
        double athVal = ath; 
        double bx = (double) tp.getX() + vx * athVal * 3.0; 
        double by = (double) tp.getY() + vy * athVal * 3.0; 
 
        Point2D.Double p1 = new Point2D.Double(sp.getX() + nx, sp.getY() + ny); 
        Point2D.Double p2 = new Point2D.Double(bx + nx, by + ny); 
        Point2D.Double p3 = new Point2D.Double(bx + athVal * nx, by + athVal * ny); 
        Point2D.Double p4 = new Point2D.Double(tp.getX(), tp.getY()); 
        Point2D.Double p5 = new Point2D.Double(bx - athVal * nx, by - athVal * ny); 
        Point2D.Double p6 = new Point2D.Double(bx - nx, by - ny); 
        Point2D.Double p7 = new Point2D.Double(sp.getX() - nx, sp.getY() - ny); 
 
        generalPath.moveTo((float) p1.getX(), (float) p1.getY()); 
        generalPath.lineTo((float) p2.getX(), (float) p2.getY()); 
        generalPath.lineTo((float) p3.getX(), (float) p3.getY()); 
        generalPath.lineTo((float) p4.getX(), (float) p4.getY()); 
        generalPath.lineTo((float) p5.getX(), (float) p5.getY()); 
        generalPath.lineTo((float) p6.getX(), (float) p6.getY()); 
        generalPath.lineTo((float) p7.getX(), (float) p7.getY()); 
        generalPath.closePath(); 
 
        gp = generalPath; 
         
    } 
 
    /** 
     * Tests if the specified Point2D is inside the boundary 
     * of this Shape. 
     * @param p the specified Point2D 
     * @return true if this Shape contains the 
     * specified Point2D, false otherwise. 
     */ 
    public boolean contains(Point2D p) { 
        return gp.contains(p); 
    } 
 
 
    /** 
     * Tests if the specified Rectangle2D 
     * is inside the boundary of this Shape. 
     * @param r a specified Rectangle2D 
     * @return true if this Shape bounds the 
     * specified Rectangle2D; false otherwise. 
     */ 
    public boolean contains(Rectangle2D r) { 
        return gp.contains(r); 
    } 
 
    /** 
     * Tests if the specified coordinates are inside the boundary of 
     * this Shape. 
     * @param x the specified x coordinates 
     * @param y the specified y coordinates 
     * @return true if the specified coordinates are inside this 
     * Shape; false otherwise 
     */ 
    public boolean contains(double x, double y) { 
        return gp.contains(x, y); 
    } 
 
    /** 
     * Tests if the specified rectangular area is inside the boundary of 
     * this Shape. 
     * @param x, y the specified coordinates 
     * @param w the width of the specified rectangular area 
     * @param h the height of the specified rectangular area 
     * @return true if this Shape contains 
     * the specified rectangluar area; false otherwise. 
     */ 
    public boolean contains(double x, double y, double w, double h) { 
        return gp.contains(x, y, w, h); 
    } 
 
    /** Return the bounding box of the path. 
     * @return a {@link java.awt.Rectangle} object that 
     * bounds the current path. 
     */ 
    public Rectangle getBounds() { 
        return gp.getBounds(); 
    } 
 
    /** 
     * Returns the bounding box of the path. 
     * @return a {@link Rectangle2D} object that 
     *          bounds the current path. 
     */ 
    public Rectangle2D getBounds2D() { 
        return gp.getBounds2D(); 
    } 
 
    /** 
     * Returns a PathIterator object that iterates along the 
     * boundary of this Shape and provides access to the 
     * geometry of the outline of this Shape. 
     * The iterator for this class is not multi-threaded safe, 
     * which means that this GeneralPath class does not 
     * guarantee that modifications to the geometry of this 
     * GeneralPath object do not affect any iterations of 
     * that geometry that are already in process. 
     * @param at an AffineTransform 
     * @return a new PathIterator that iterates along the 
     * boundary of this Shape and provides access to the 
     * geometry of this Shape's outline 
     */ 
    public PathIterator getPathIterator(AffineTransform at) { 
        return gp.getPathIterator(at); 
    } 
 
    /** 
     * Returns a PathIterator object that iterates along the 
     * boundary of the flattened Shape and provides access to the 
     * geometry of the outline of the Shape. 
     * The iterator for this class is not multi-threaded safe, 
     * which means that this GeneralPath class does not 
     * guarantee that modifications to the geometry of this 
     * GeneralPath object do not affect any iterations of 
     * that geometry that are already in process. 
     * @param at an AffineTransform 
     * @param flatness the maximum distance that the line segments used to 
     *		approximate the curved segments are allowed to deviate 
     *		from any point on the original curve 
     * @return a new PathIterator that iterates along the flattened 
     * Shape boundary. 
     */ 
    public PathIterator getPathIterator(AffineTransform at, double flatness) { 
        return gp.getPathIterator(at, flatness); 
    } 
 
    /** 
     * Tests if the interior of this Shape intersects the 
     * interior of a specified Rectangle2D. 
     * @param r the specified Rectangle2D 
     * @return true if this Shape and the interior 
     * 		of the specified Rectangle2D intersect each 
     * 		other; false otherwise. 
     */ 
    public boolean intersects(Rectangle2D r) { 
        return gp.intersects(r); 
    } 
 
    /** 
     * Tests if the interior of this Shape intersects the 
     * interior of a specified set of rectangular coordinates. 
     * @param x, y the specified coordinates 
     * @param w the width of the specified rectangular coordinates 
     * @param h the height of the specified rectangular coordinates 
     * @return true if this Shape and the 
     * interior of the specified set of rectangular coordinates intersect 
     * each other; false otherwise. 
     */ 
    public boolean intersects(double x, double y, double w, double h) { 
        return gp.intersects(x, y, w, h); 
    } 
 
 
}