Java Draw Arrow drawArrow(Graphics2D g, Point2D point, double angle, int len)

Here you can find the source of drawArrow(Graphics2D g, Point2D point, double angle, int len)

Description

draw Arrow

License

Open Source License

Declaration

public static void drawArrow(Graphics2D g, Point2D point, double angle, int len) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

public class Main {
    public static void drawArrow(Graphics2D g, int x1, int y1, int x2, int y2) {
        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));
        AffineTransform restoreTransform = g.getTransform();
        g.transform(at);/*w w  w.ja  v a  2 s. c o  m*/

        final int ARR_SIZE = 12;

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

        g.setTransform(restoreTransform);
    }

    public static void drawArrow(Graphics2D g, Point2D point, double angle, int len) {
        AffineTransform at = AffineTransform.getTranslateInstance(point.getX(), point.getY());
        at.concatenate(AffineTransform.getRotateInstance(angle));
        AffineTransform restoreTransform = g.getTransform();
        g.transform(at);

        int ARR_SIZE = 42;

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

        g.setTransform(restoreTransform);
    }
}

Related

  1. drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight)
  2. drawArrow(Graphics g, int x0, int y0, int x1, int y1, int headLength, int headAngle)
  3. drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawArrow(Graphics g, Point from, Point to)
  5. drawArrow(Graphics g1, int x1, int y1, int x2, int y2, int lineWidth)
  6. drawArrow(Graphics2D g2d, double x1, double y1, double x2, double y2)
  7. drawArrow(Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke)
  8. drawArrowHead(final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor, final int aArrowWidth, final int aArrowHeight)
  9. drawArrowHead(Graphics g, double c1, double c2, double phi, int l, int grad, boolean fill)