Java Draw Arrow drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)

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

Description

draw Arrow

License

Open Source License

Declaration

public static void drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) 

Method Source Code

//package com.java2s;

import java.awt.*;

public class Main {
    static int al = 16;
    static int aw = 10;
    static int haw = aw / 2;
    static int xValues[] = new int[3];
    static int yValues[] = new int[3];

    public static void drawArrow(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
        // Draw line using user defined drawline() which can specify line width
        drawLine(g, x1, y1, x2, y2, lineWidth);
        // Calculate x-y values for arrow head
        calcValues(x1, y1, x2, y2);//from  w ww .j  ava  2  s  . c  o  m
        g.fillPolygon(xValues, yValues, 3);
    }

    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
        if (lineWidth == 1)
            g.drawLine(x1, y1, x2, y2);
        else {
            double angle;
            double halfWidth = ((double) lineWidth) / 2.0;
            double deltaX = (double) (x2 - x1);
            double deltaY = (double) (y2 - y1);
            if (x1 == x2)
                angle = Math.PI;
            else
                angle = Math.atan(deltaY / deltaX) + Math.PI / 2;
            int xOffset = (int) (halfWidth * Math.cos(angle));
            int yOffset = (int) (halfWidth * Math.sin(angle));
            int[] xCorners = { x1 - xOffset, x2 - xOffset + 1, x2 + xOffset + 1, x1 + xOffset };
            int[] yCorners = { y1 - yOffset, y2 - yOffset, y2 + yOffset + 1, y1 + yOffset + 1 };
            g.fillPolygon(xCorners, yCorners, 4);
        }
    }

    public static void calcValues(int x1, int y1, int x2, int y2) {

        // North or south   
        if (x1 == x2) {
            // North
            if (y2 < y1)
                arrowCoords(x2, y2, x2 - haw, y2 + al, x2 + haw, y2 + al);
            // South
            else
                arrowCoords(x2, y2, x2 - haw, y2 - al, x2 + haw, y2 - al);
            return;
        }
        // East or West   
        if (y1 == y2) {
            // East
            if (x2 > x1)
                arrowCoords(x2, y2, x2 - al, y2 - haw, x2 - al, y2 + haw);
            // West
            else
                arrowCoords(x2, y2, x2 + al, y2 - haw, x2 + al, y2 + haw);
            return;
        }
        // Calculate quadrant

        calcValuesQuad(x1, y1, x2, y2);
    }

    public static void arrowCoords(int x1, int y1, int x2, int y2, int x3, int y3) {
        xValues[0] = x1;
        yValues[0] = y1;
        xValues[1] = x2;
        yValues[1] = y2;
        xValues[2] = x3;
        yValues[2] = y3;
    }

    public static void calcValuesQuad(int x1, int y1, int x2, int y2) {
        double arrowAng = Math.toDegrees(Math.atan((double) haw / (double) al));
        double dist = Math.sqrt(al * al + aw);
        double lineAng = Math.toDegrees(Math.atan(((double) Math.abs(x1 - x2)) / ((double) Math.abs(y1 - y2))));

        // Adjust line angle for quadrant
        if (x1 > x2) {
            // South East
            if (y1 > y2)
                lineAng = 180.0 - lineAng;
        } else {
            // South West
            if (y1 > y2)
                lineAng = 180.0 + lineAng;
            // North West
            else
                lineAng = 360.0 - lineAng;
        }

        // Calculate coords   
        xValues[0] = x2;
        yValues[0] = y2;
        calcCoords(1, x2, y2, dist, lineAng - arrowAng);
        calcCoords(2, x2, y2, dist, lineAng + arrowAng);
    }

    public static void calcCoords(int index, int x, int y, double dist, double dirn) {
        while (dirn < 0.0)
            dirn = 360.0 + dirn;
        while (dirn > 360.0)
            dirn = dirn - 360.0;
        // System.out.println("dirn = " + dirn);

        // North-East
        if (dirn <= 90.0) {
            xValues[index] = x + (int) (Math.sin(Math.toRadians(dirn)) * dist);
            yValues[index] = y - (int) (Math.cos(Math.toRadians(dirn)) * dist);
            return;
        }
        // South-East
        if (dirn <= 180.0) {
            xValues[index] = x + (int) (Math.cos(Math.toRadians(dirn - 90)) * dist);
            yValues[index] = y + (int) (Math.sin(Math.toRadians(dirn - 90)) * dist);
            return;
        }
        // South-West
        if (dirn <= 90.0) {
            xValues[index] = x - (int) (Math.sin(Math.toRadians(dirn - 180)) * dist);
            yValues[index] = y + (int) (Math.cos(Math.toRadians(dirn - 180)) * dist);
        }
        // Nort-West    
        else {
            xValues[index] = x - (int) (Math.cos(Math.toRadians(dirn - 270)) * dist);
            yValues[index] = y - (int) (Math.sin(Math.toRadians(dirn - 270)) * dist);
        }
    }
}

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(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)
  4. drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight)
  5. drawArrow(Graphics g, int x0, int y0, int x1, int y1, int headLength, int headAngle)
  6. drawArrow(Graphics g, Point from, Point to)
  7. drawArrow(Graphics g1, int x1, int y1, int x2, int y2, int lineWidth)
  8. drawArrow(Graphics2D g, Point2D point, double angle, int len)
  9. drawArrow(Graphics2D g2d, double x1, double y1, double x2, double y2)