Java Draw Arrow drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight)

Here you can find the source of drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight)

Description

Draws an arrow between two points (specified by their coordinates) using the specified weight.
The arrow is leading from point from to point to.

License

Open Source License

Parameter

Parameter Description
g Graphics context
x0 X coordinate of arrow starting point
y0 Y coordinate of arrow starting point
x1 X coordinate of arrow ending point
y1 Y coordinate of arrow ending point
weight Arrow weight

Declaration

public static void drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight) 

Method Source Code


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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

public class Main {
    /**/*from   w w w. ja  va  2 s .  c  o m*/
     * Draws an arrow between two given points using the specified weight.<br>
     * The arrow is leading from point <code>from</code> to point <code>to</code>.
     * @param g Graphics context
     * @param from Arrow starting point
     * @param to Arrow ending point
     * @param weight Arrow weight
     */
    public static void drawArrow(Graphics g, Point from, Point to, double weight) {
        drawArrow(g, from.getX(), from.getY(), to.getX(), to.getY(), weight);
    }

    /**
     * Draws an arrow between two points (specified by their coordinates) using the specified weight.<br>
     * The arrow is leading from point <code>from</code> to point <code>to</code>.
     * @param g Graphics context
     * @param x0 X coordinate of arrow starting point
     * @param y0 Y coordinate of arrow starting point
     * @param x1 X coordinate of arrow ending point
     * @param y1 Y coordinate of arrow ending point
     * @param weight Arrow weight
     */
    public static void drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight) {
        int ix2, iy2, ix3, iy3;
        double sinPhi, cosPhi, dx, dy, xk1, yk1, xk2, yk2, s;
        dx = x1 - x0;
        dy = y1 - y0;
        int maxArrowWidth = 10;
        //arrow length
        s = Math.sqrt(dy * dy + dx * dx);
        //arrow head length
        int headLength = (int) Math.round(s * 0.5);

        double arrowAngle = Math.atan((double) (weight * maxArrowWidth) / headLength);
        double arrowAngle2 = Math.atan((double) maxArrowWidth / headLength);

        sinPhi = dy / s; // Winkel des Pfeils 
        cosPhi = dx / s; // mit der X-Achse
        if (s < headLength) { // Der Pfeil 
            x0 = x1 - headLength * cosPhi; // .. darf nicht kuerzer 
            y0 = y1 - headLength * sinPhi; // .. als die Spitze sein
        }
        xk1 = -headLength * Math.cos(arrowAngle); // Koordinaten
        yk1 = headLength * Math.sin(arrowAngle); // .. der Pfeilspitze

        xk2 = -headLength * Math.cos(arrowAngle2); // Koordinaten
        yk2 = headLength * Math.sin(arrowAngle2);
        ix2 = (int) (x1 + xk1 * cosPhi - yk1 * sinPhi); // Pfeilspitze 
        iy2 = (int) (y1 + xk1 * sinPhi + yk1 * cosPhi); // .. um Winkel Phi 
        ix3 = (int) (x1 + xk1 * cosPhi + yk1 * sinPhi); // .. rotieren 
        iy3 = (int) (y1 + xk1 * sinPhi - yk1 * cosPhi); // .. und translatieren 
        Color c = g.getColor();
        g.setColor(Color.black);
        g.drawLine((int) x0, (int) y0, (int) x1, (int) y1); // Pfeilachse 
        Polygon p = new Polygon();
        p.addPoint((int) x1, (int) y1);
        p.addPoint((int) ix2, (int) iy2);
        p.addPoint((int) ix3, (int) iy3);
        g.setColor(c);
        g.fillPolygon(p);
        g.setColor(Color.black);
        g.drawPolygon(p);
        g.drawLine((int) (x1 + xk2 * cosPhi - yk2 * sinPhi), (int) (y1 + xk2 * sinPhi + yk2 * cosPhi),
                (int) (x1 + xk2 * cosPhi + yk2 * sinPhi), iy3 = (int) (y1 + xk2 * sinPhi - yk2 * cosPhi));
    }
}

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, 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)
  7. drawArrow(Graphics g1, int x1, int y1, int x2, int y2, int lineWidth)