开发者

Java Graphics 2D UI Arrow Head Direction

开发者 https://www.devze.com 2023-01-20 20:15 出处:网络
I want to draw an arrow using Graphic fillpolygon. But my arrow head is in a re开发者_如何学Goverse side. Any idea?

I want to draw an arrow using Graphic fillpolygon. But my arrow head is in a re开发者_如何学Goverse side. Any idea?

int xpoints[] = { 20, 30, 30, 35, 25, 15, 20 };
int ypoints[] = { 10, 10, 30, 30, 45, 30, 30 };
int npoints = 7;
g2D.fillPolygon(xpoints, ypoints, npoints);


Java 2D coordinates are given in user space, in which the top-left is (0, 0). See Coordinates:

When the default transformation from user space to device space is used, the origin of user space is the upper-left corner of the component’s drawing area. The x coordinate increases to the right, and the y coordinate increases downward, as shown in the following figure. The top-left corner of a window is 0,0. All coordinates are specified using integers, which is usually sufficient. However, some cases require floating point or even double precision which are also supported.

Java Graphics 2D UI Arrow Head Direction

I found Java 2D - Affine Transform to invert y-axis, so I modified it to translate the origin to bottom-left, and combined it with your arrow:

protected  void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    Insets insets = getInsets();
    // int w = getWidth() - insets.left - insets.right;
    int h = getHeight() - insets.top - insets.bottom;

    AffineTransform oldAT = g2.getTransform();
    try {
        //Move the origin to bottom-left, flip y axis
        g2.scale(1.0, -1.0);
        g2.translate(0, -h - insets.top);

        int xpoints[] = { 20, 30, 30, 35, 25, 15, 20 };
        int ypoints[] = { 10, 10, 30, 30, 45, 30, 30 };
        int npoints = 7;
        g2.fillPolygon(xpoints, ypoints, npoints);
    }
    finally {
      //restore
      g2.setTransform(oldAT);
    }
}

full source

Java Graphics 2D UI Arrow Head Direction

0

精彩评论

暂无评论...
验证码 换一张
取 消