Lukas
Lukas

Reputation: 23

JavaFX - draw line with arrow (Canvas)

I have problem with rewrite this code to JavaFX:

private final int ARR_SIZE = 8;

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

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

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

my solution is not working correctly because the result of code:

drawArrow(gc, 200, 50, 50, 50);

is

arrow.

Expected result is

arrow2.

private final int ARR_SIZE = 8;

void drawArrow(GraphicsContext gc, int x1, int y1, int x2, int y2) {

    gc.setFill(Color.BLACK);

    double dx = x2 - x1, dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);

    Affine affine = new Affine(Affine.translate(x1, y1));
    affine.createConcatenation(Affine.rotate(angle, 0, 0));
    gc.setTransform(affine);

    gc.strokeLine(0, 0, len, 0);
    gc.fillPolygon(new double[]{len, len - ARR_SIZE, len - ARR_SIZE, len}, new double[]{0, -ARR_SIZE, ARR_SIZE, 0},
            4);

}

can you help me pls ?

Upvotes: 2

Views: 4604

Answers (1)

fabian
fabian

Reputation: 82461

There are 2 reasons for this:

  1. Rotating by radians instead of degrees: Transform.rotate expects the angle in degrees (see javadoc for Rotate constructor; javadoc for Transform.rotate "redirects" there), but Math.atan2 returns radians.
  2. Transform.createConcatenation creates a new Transform instead of modifying the existing Transform.

This code should work:

void drawArrow(GraphicsContext gc, int x1, int y1, int x2, int y2) {
    gc.setFill(Color.BLACK);

    double dx = x2 - x1, dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);

    Transform transform = Transform.translate(x1, y1);
    transform = transform.createConcatenation(Transform.rotate(Math.toDegrees(angle), 0, 0));
    gc.setTransform(new Affine(transform));

    gc.strokeLine(0, 0, len, 0);
    gc.fillPolygon(new double[]{len, len - ARR_SIZE, len - ARR_SIZE, len}, new double[]{0, -ARR_SIZE, ARR_SIZE, 0},
            4);
}

Upvotes: 5

Related Questions