Java Draw Arrow drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)

Here you can find the source of drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)

Description

Draw arrow of a message in communication diagram

License

Open Source License

Parameter

Parameter Description
g Graphics2D
x1 position x where arrow starts
y1 position y where arrow starts
x2 position x where arrow ends
y2 position y where arrow ends

Declaration

public static void drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2) 

Method Source Code

//package com.java2s;
/*//from  w w  w  . j  a v a2 s. c o  m
 * USE - UML based specification environment
 * Copyright (C) 1999-2010 Mark Richters, University of Bremen
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

public class Main {
    /**
     * Draw arrow of a message in communication diagram
     * 
     * @param g
     *            Graphics2D
     * @param x1
     *            position x where arrow starts
     * @param y1
     *            position y where arrow starts
     * @param x2
     *            position x where arrow ends
     * @param y2
     *            position y where arrow ends
     */
    public static void drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2) {
        final int ARR_SIZE = 5;

        // Get the current transform
        AffineTransform saveAT = g.getTransform();

        double dx = x2 - x1, dy = y2 - y1;
        double angle = Math.atan2(dy, dx);
        int len = (int) Math.sqrt(dx * dx + dy * dy);
        AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        // Perform transformation
        g.transform(at);

        // Draw horizontal arrow starting in (0, 0)
        g.drawLine(0, 0, (int) len, 0);
        g.fillPolygon(new int[] { len, len - ARR_SIZE, len - ARR_SIZE, len },
                new int[] { 0, -ARR_SIZE, ARR_SIZE, 0 }, 4);

        // Restore original transform
        g.setTransform(saveAT);
    }
}

Related

  1. draw_nav1_backward_arrow(Graphics2D g2, int x, int y, int length, int base_width)
  2. draw_nav2_backward_arrow(Graphics2D g2, int x, int y, int length, int base_width)
  3. drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight)
  4. drawArrow(Graphics g, int x0, int y0, int x1, int y1, int headLength, int headAngle)
  5. drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  6. drawArrow(Graphics g, Point from, Point to)