Java examples for 2D Graphics:Shape
Draw an arrow
/*//from w w w. ja va2 s . c o m * A Collection of Miscellaneous Utilities * * Copyright 2010 * The President and Fellows of Harvard College. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ //package com.java2s; import java.awt.*; public class Main { /** * Draw an arrow * * @param g the graphics object * @param x1 the source X coordinate * @param y1 the source Y coordinate * @param x2 the target X coordinate * @param y2 the target Y coordinate * @param stroke the stroke size * @param filled whether to fill the arrowhead */ public static void drawArrow(Graphics g, int x1, int y1, int x2, int y2, double stroke, boolean filled) { g.drawLine(x2, y2, x1, y1); drawArrowHead(g, x1, y1, x2, y2, stroke, filled); } /** * Draw an arrow head * * @param g the graphics object * @param x1 the source X coordinate * @param y1 the source Y coordinate * @param x2 the target X coordinate * @param y2 the target Y coordinate * @param size the head size * @param filled whether to fill the arrowhead */ public static void drawArrowHead(Graphics g, int x1, int y1, int x2, int y2, double size, boolean filled) { // Original source code: http://forums.sun.com/thread.jspa?threadID=378460 Polygon p = new Polygon(); double aDir = Math.atan2(x1 - x2, y1 - y2); int i1 = (int) size; //int i2 = 6 + (int) stroke; p.addPoint(x2, y2); p.addPoint(x2 + xCor(i1, aDir + .5), y2 + yCor(i1, aDir + .5)); //p.addPoint(x2 + xCor(i2, aDir ), y2 + yCor(i2, aDir )); p.addPoint(x2 + xCor(i1, aDir - .5), y2 + yCor(i1, aDir - .5)); p.addPoint(x2, y2); if (filled) g.fillPolygon(p); else g.drawPolygon(p); } private static int xCor(int len, double dir) { return (int) (len * Math.sin(dir)); } private static int yCor(int len, double dir) { return (int) (len * Math.cos(dir)); } }