Java Draw Arrow drawArrow(Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke)

Here you can find the source of drawArrow(Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke)

Description

It defines functionality to draw arrows, by using colors and coordinates.

License

Open Source License

Parameter

Parameter Description
g2d An object of type Graphics2D. It is the graph on which arrows are pictured.
xCenter It is the x coordinate of the first point from the center.
yCenter It is the y coordinate of the second point from the center.
x It is the x coordinate of the first point.
y It is the y coordinate of the second point.
stroke It is the stroke of the arrow.

Declaration

public static void drawArrow(Graphics2D g2d, int xCenter, int yCenter,
        int x, int y, float stroke) 

Method Source Code

//package com.java2s;
/*/*from ww  w  .j ava 2  s  .c o  m*/
 * Rainbow - A simulator of processes and resources in a multitasking computer.
 * Copyright (C) 2006. E-mail: piero.dallepezze@gmail.com
 *
 * This program 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.
 *
 * This program 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *
 * File: gpfxUtilsRainbow.java
 * Package: gui.gpfxRepository
 * Author: Sarto Carlo, Dalle Pezze Piero
 * Date: 10/02/2006
 * Version: 1.2
 *
 * Modifies:
 *  - v.1.2 (16/04/2007): English translation. Java6 compatible. (Piero Dalle Pezze)
 *  - v.1.1 (16/02/2006): Documentation. Sarto Carlo.
 *  - v.1.0 (10/02/2006): Codify. Sarto Carlo.
 * */

import java.awt.*;

public class Main {
    /**
     * It defines functionality to draw arrows, by using colors and coordinates.
     * 
     * @param g2d
     *            An object of type Graphics2D. It is the graph on which arrows
     *            are pictured.
     * @param xCenter
     *            It is the x coordinate of the first point from the center.
     * @param yCenter
     *            It is the y coordinate of the second point from the center.
     * @param x
     *            It is the x coordinate of the first point.
     * @param y
     *            It is the y coordinate of the second point.
     * @param stroke
     *            It is the stroke of the arrow.
     */
    public static void drawArrow(Graphics2D g2d, int xCenter, int yCenter,
            int x, int y, float stroke) {
        double aDir = Math.atan2(xCenter - x, yCenter - y);
        g2d.drawLine(x, y, xCenter, yCenter);
        g2d.setStroke(new BasicStroke(1f));

        // it makes the arrow head solid even if dash pattern has been specified
        Polygon tmpPoly = new Polygon();
        int i1 = 12 + (int) (stroke * 2);
        int i2 = 6 + (int) stroke;

        // it makes the arrow head the same size regardless of the length length
        // arrow tip
        tmpPoly.addPoint(x, y);
        tmpPoly.addPoint(x + xCor(i1, aDir + .3), y + yCor(i1, aDir + .3));
        tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
        tmpPoly.addPoint(x + xCor(i1, aDir - .3), y + yCor(i1, aDir - .3));
        // arrow tip
        tmpPoly.addPoint(x, y);

        g2d.drawPolygon(tmpPoly);
        // it removes this line to leave arrow head unpainted
        g2d.fillPolygon(tmpPoly);

    }

    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));
    }
}

Related

  1. drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  2. drawArrow(Graphics g, Point from, Point to)
  3. drawArrow(Graphics g1, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawArrow(Graphics2D g, Point2D point, double angle, int len)
  5. drawArrow(Graphics2D g2d, double x1, double y1, double x2, double y2)
  6. drawArrowHead(final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor, final int aArrowWidth, final int aArrowHeight)
  7. drawArrowHead(Graphics g, double c1, double c2, double phi, int l, int grad, boolean fill)
  8. drawArrowLine(Graphics g, int x1, int y1, int x2, int y2, int d, int h, boolean capOnly)
  9. drawArrowLine(Graphics g, int x1, int y1, int x2, int y2, int dist, boolean arrowLeft, boolean arrowRight)