Java2D负位置无法显示,将原点移至左下

发布于 2024-12-06 05:59:14 字数 238 浏览 0 评论 0原文

我正在尝试在 Graphics2D 坐标系上画线。但是,我发现负数区域的线上部分无法显示。无论如何,我可以使负区域中的线条可见吗?

另外,我是否可以将 y 轴的直接从向下转换为向上?

Graphics2D g2 = (Graphics2D) g;
g2.scale(1, -1);
g2.translate(0, -HEIGHT);

无法工作。物体消失。

谢谢!

I am trying to draw lines on coordinates system in Graphics2D. However, I find out that the part on line in negative area can not be shown. Is there anyway I can make the lines in negative area be seen?

Also, is there anyway I can convert direct of y-axis from downward to upward?

Graphics2D g2 = (Graphics2D) g;
g2.scale(1, -1);
g2.translate(0, -HEIGHT);

Can't work. Object disappears.

Thanks!

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

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

发布评论

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

评论(2

清风不识月 2024-12-13 05:59:14

啊,您正在使用 HEIGHT 属性。您应该使用 getHeight()


下面的代码生成此屏幕截图 (g2.drawLine(0, 0, 100, 100)):

截图

代码:

public static void main(String[] args) throws Exception {

    JFrame frame = new JFrame("Test");

    frame.add(new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g.create();
            {
                g2.translate(0, getHeight() - 1);
                g2.scale(1, -1);

                g2.drawLine(0, 0, 100, 100);
            }
            g2.dispose();
        }
    });

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

Ah, you are using the HEIGHT attribute. You should be using getHeight().


The code below produces this screenshot (g2.drawLine(0, 0, 100, 100)):

screenshot

Code:

public static void main(String[] args) throws Exception {

    JFrame frame = new JFrame("Test");

    frame.add(new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g.create();
            {
                g2.translate(0, getHeight() - 1);
                g2.scale(1, -1);

                g2.drawLine(0, 0, 100, 100);
            }
            g2.dispose();
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}
贱人配狗天长地久 2024-12-13 05:59:14

据我了解 Java2D 你不能使用负坐标。你总是在Java2D中所谓的“用户空间”中进行操作。您在“设备空间”中的位置的平移坐标可能为负,但这在 Java 中对您来说是不可见的。另请参阅 Java2D 教程 - 坐标Graphics2D API

您也许可以通过子类化 Graphics2D 并自己进行这些转换来实现您想要的目标。

As far as I understand Java2D you can't use negative coordinates. You always operate in the so-called "User Space" in Java2D. The translates coordinates of your position in "Device Space" might be negative, but this is invisible to you in Java. See also Java2D Tutorial - Coordinates and Graphics2D API.

You might be able to achieve what you want by subclassing Graphics2D and doing the those translation yourself.

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