Drawing a line with arrow in Java
Asked Answered
H

2

12

alt textCan anyone guide me how to code the arrow line in different direction. wa and wl is positive the rectangle will be on top of the x-axis. Below example shown if wl is negative and wa is positive. The code below shown how i code the rectangle shape. x1 is the varaible to state where to start at x axis. e1 is the length of the shape, wa1 and wl1 is the height. wsign to determine the height wa1 or wl1 should display at negative side or positive side.

        if (Math.abs(wl1) > Math.abs(wa1)) {
            y_scale = (load_y0 - 40) / (double) Math.abs(wl1);
        } else {
            y_scale = (load_y0 - 40) / (double) Math.abs(wa1);
        }
        g.drawLine((int) ((double) x0 + x1 * x_scale), (int) (load_y),
                    (int) ((double) x0 + x1 * x_scale),
                    (int) (load_y + (wa1 * y_scale) * -1));
            g.drawLine((int) ((double) x0 + (x1 + e1) * x_scale),
                    (int) (load_y), (int) ((double) x0 + (x1 + e1)
                            * x_scale), (int) (load_y + (wl1 * y_scale)
                            * -1));

            g.drawLine((int) ((double) x0 + x1 * x_scale),
                    (int) (load_y + (wa1 * y_scale * -1)),
                    (int) ((double) x0 + (x1 + e1) * x_scale),
                    (int) (load_y + (wl1 * y_scale) * -1)); 
Hernardo answered 6/11, 2010 at 10:18 Comment(4)
That's too fast for me ))). What do you mean by "draw the line between the shape and the arrow direction"?Gelasius
Can you annotate your diagram with a circle to show which line you mean?Treaty
Hi, thanks for reply, I want to draw the line with arrow inside the triangle shape, one is shown at positive area another is shown at negative areaHernardo
Hi maybe can you guide me how to get x when the point y = 0 like shown in diagram?Hernardo
B
30

Here is a simple routine (adopted from here) for drawing arbitrary arrows:

import static java.awt.geom.AffineTransform.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

public class Main {
    public static void main(String args[]) {
        JFrame t = new JFrame();
        t.add(new JComponent() {

            private final int ARR_SIZE = 4;

            void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
                Graphics2D g = (Graphics2D) g1.create();

                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));
                g.transform(at);

                // 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);
            }

            public void paintComponent(Graphics g) {
                for (int x = 15; x < 200; x += 16)
                    drawArrow(g, x, x, x, 150);
                drawArrow(g, 30, 300, 300, 190);
            }
        });

        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setSize(400, 400);
        t.setVisible(true);
    }
}

Result:

enter image description here

Bis answered 6/11, 2010 at 11:20 Comment(5)
Hi, thanks for your example, but how can i set the height of the line according wa1 and wl1Hernardo
Hi maybe can you guide me how to get x when the point y = 0 like shown in diagram?Hernardo
great answer, but I can't get my head around what you would to to give the arrow an offset, for example, me second coordinate is the center of a circle, how would I change it so the arrow points to the circumference of the circle as opposed to the center?Whippoorwill
The arrow-drawing code is trivial. It's the two lines below the // Draw horizontal arrow starting in (0, 0) comment. I'm sure you can figure out how to adjust those lines according to your needs.Bis
Happy to see a solution using two pairs of x and y coordinates.Libertine
S
2

Simple arrow sample

/**
 * @param fromPoint end of the arrow
 * @param rotationDeg rotation angle of line
 * @param length arrow length
 * @param wingsAngleDeg wingspan of arrow
 * @return Path2D arrow shape
 */
public static Path2D createArrowForLine(
                 Point2D fromPoint, 
                 double rotationDeg, 
                 double length, 
                 double wingsAngleDeg) {

        double ax = fromPoint.getX();
        double ay = fromPoint.getY();

        double radB = Math.toRadians(-rotationDeg + wingsAngleDeg);
        double radC = Math.toRadians(-rotationDeg - wingsAngleDeg);

        Path2D resultPath = new Path2D.Double();
        resultPath.moveTo(length * Math.cos(radB) + ax, length * Math.sin(radB) + ay);
        resultPath.lineTo(ax, ay);
        resultPath.lineTo(length * Math.cos(radC) + ax, length * Math.sin(radC) + ay);
        return resultPath;
    }
Synesthesia answered 4/10, 2014 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.