Java Draw Arrow drawArrow(Graphics g, Point from, Point to)

Here you can find the source of drawArrow(Graphics g, Point from, Point to)

Description

draw Arrow

License

Apache License

Declaration

public static void drawArrow(Graphics g, Point from, Point to) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Graphics;
import java.awt.Point;

public class Main {
    public static void drawArrow(Graphics g, Point from, Point to) {
        int arrowLength = 10;

        int deltaX = to.x - from.x;
        int deltaY = to.y - from.y;

        float length = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);

        Point toRotate = new Point();
        toRotate.x = Math.round(0 - (arrowLength * deltaX) / length);
        toRotate.y = Math.round(0 - (arrowLength * deltaY) / length);

        Point one = rotate(toRotate, (float) Math.toRadians(20));
        Point two = rotate(toRotate, (float) Math.toRadians(-20));
        one.x += to.x;//  w w  w .j  a  v a 2  s  .  c  om
        one.y += to.y;

        two.x += to.x;
        two.y += to.y;
        // draw arrow
        g.drawLine(one.x, one.y, to.x, to.y);
        g.drawLine(two.x, two.y, to.x, to.y);
    }

    public static Point rotate(Point toRotate, float tetta) {
        float sin = (float) Math.sin(tetta);
        float cos = (float) Math.cos(tetta);

        int x = (int) (toRotate.x * cos - toRotate.y * sin);
        int y = (int) (toRotate.x * sin + toRotate.y * cos);

        return new Point(x, y);
    }
}

Related

  1. draw_nav2_backward_arrow(Graphics2D g2, int x, int y, int length, int base_width)
  2. drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)
  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 g1, int x1, int y1, int x2, int y2, int lineWidth)
  7. drawArrow(Graphics2D g, Point2D point, double angle, int len)
  8. drawArrow(Graphics2D g2d, double x1, double y1, double x2, double y2)
  9. drawArrow(Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke)