如何使用另一个象限来绘制JPanel坐标? [英] How can I draw on JPanel using another quadrant for the coordinates?

查看:111
本文介绍了如何使用另一个象限来绘制JPanel坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过覆盖 paintComponent 在JPanel上绘制一些形状。我希望能够平移和缩放。使用 Graphics2D上的 AffineTransform setTransform 方法可以轻松地进行平移和缩放。 code>对象。完成此操作后,我可以使用 g2.draw(myShape)轻松绘制形状。这些形状是使用世界坐标定义的,因此在平移时效果很好,我必须翻译

I would like to draw some shapes on a JPanel by overriding paintComponent. I would like to be able to pan and zoom. Panning and zooming is easy to do with AffineTransform and the setTransform method on the Graphics2D object. After doing that I can easyli draw the shapes with g2.draw(myShape) The shapes are defined with the "world coordinates" so it works fine when panning and I have to translate them to the canvas/JPanel coordinates before drawing.

现在,我想更改象限。从JPanel和计算机经常使用的第四象限到用户最熟悉的第一象限。 X相同,但Y轴应向上而不是向下增加。通过 new Point(origo.x,-origo.y)重新定义origo很容易;

Now I would like to change the quadrant of the coordinates. From the 4th quadrant that JPanel and computer often uses to the 1st quadrant that the users are most familiar with. The X is the same but the Y-axe should increase upwards instead of downwards. It is easy to redefine origo by new Point(origo.x, -origo.y);

但是如何在该象限中绘制形状?我想保留形状的坐标(在世界坐标中定义),而不是在画布坐标中保留它们。因此,我需要以某种方式对其进行转换,或转换 Graphics2D 对象,而我想高效地做到 。我也可以使用 AffineTransform 来做到这一点吗?

But How can I draw the shapes in this quadrant? I would like to keep the coordinates of the shapes (defined in the world coordinates) rather than have them in the canvas coordinates. So I need to transform them in some way, or transform the Graphics2D object, and I would like to do it efficiently. Can I do this with AffineTransform too?

我的绘图代码:

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setColor(Color.blue);

            AffineTransform at = g2.getTransform();
            at.translate(-origo.x, -origo.y);
            at.translate(0, getHeight());
            at.scale(1, -1);
            g2.setTransform(at);
            g2.drawLine(30, 30, 140, 20);
            g2.draw(new CubicCurve2D.Double(30, 65, 23, 45, 23, 34, 67, 58));
        }


推荐答案

答案,因此未经测试,但我认为它可以工作。

This is an off the cuff answer, so it's untested, but I think it will work.

按(0,height)转换。可以将原点重新定位到左下角。

Translate by (0, height). That should reposition the origin to the lower left.

按(1,-1)缩放。

Scale by (1, -1). That should flip it about the x axis.

在这种情况下,我认为操作顺序并不重要。

I don't think the order of operations matters in this case.

这篇关于如何使用另一个象限来绘制JPanel坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆