create Arrow Path - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

create Arrow Path

Demo Code


//package com.java2s;

import java.awt.Point;

import java.awt.geom.GeneralPath;

public class Main {
    public static final int ARROW_LENGTH = 8;
    public static final double ARROW_ANGLE = Math.PI / 6;

    private static GeneralPath createArrowPath(Point position,
            Point controlPoint) {
        // The the angle of the line segment
        double alpha = Math.atan((double) (position.y - controlPoint.y)
                / (position.x - controlPoint.x));
        if (controlPoint.x > position.x)
            alpha += Math.PI;/*from  w ww .  j  a v  a2s . c o  m*/
        double angle = ARROW_ANGLE - alpha;
        GeneralPath path = new GeneralPath();
        float x1 = (float) (position.x - ARROW_LENGTH * Math.cos(angle));
        float y1 = (float) (position.y + ARROW_LENGTH * Math.sin(angle));
        path.moveTo(x1, y1);
        path.lineTo(position.x, position.y);
        angle = ARROW_ANGLE + alpha;
        float x2 = (float) (position.x - ARROW_LENGTH * Math.cos(angle));
        float y2 = (float) (position.y - ARROW_LENGTH * Math.sin(angle));
        path.lineTo(x2, y2);
        path.closePath();
        return path;
    }
}

Related Tutorials