Java Graphics 2D UI 箭头方向

发布于 2024-09-28 03:58:20 字数 236 浏览 9 评论 0 原文

我想使用 Graphic fillpolygon 绘制箭头。但我的箭头在反面。有什么想法吗?

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

I want to draw an arrow using Graphic fillpolygon. But my arrow head is in a reverse 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-10-05 03:58:20

Java 2D 坐标在用户空间中给出,其中左上角为 (0, 0)。请参阅坐标

当使用从用户空间到设备空间的默认转换时,用户空间的原点是组件绘图区域的左上角。 x坐标向右增大,y坐标向下增大,如下图所示。窗口的左上角是 0,0。所有坐标均使用整数指定,这通常就足够了。但是,某些情况下需要浮点甚至双精度,这也是支持的。

alt text

我发现 Java 2D - 仿射变换以反转 y 轴,因此我修改它以将原点平移到左下角,并将其与您的箭头组合:

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

完整源代码

替代文字

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.

alt text

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

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文