Java 摆动 java.awt.Shape
Java Swing moving java.awt.Shape
我们有一个显示一种地图的应用程序。这个Swing应用主要画了一组java.awt.Shape
s with java.awt.Graphics2D#draw(Shape)
一切正常
现在,我必须扩展这个应用程序以允许我们的用户编辑(移动形状)地图。但是 java.awt.Shape
上没有 translate
或 move
方法。所以我无法更改形状的位置 (java.awt.Point
)。
我试图覆盖 java.awt.Shape#getPathIterator
以应用与形状位置匹配的翻译。但这很棘手,因为这种方法已经接受了我必须合并的转换,而且为了正确起见,PathIterator
应该从 (0, 0)
开始,因为它是相对于形状位置的。
无论如何,这不会起作用,因为 Graphics2D
似乎并不总是使用这种方法来绘制 java.awt.Shape
。
所以现在,我感觉有点失落。另一种解决方案可能是让形状自己绘制,但我必须重写应用程序的一部分。这不是问题,但我必须知道哪个可能是最好的解决方案:
找个技巧移动一个java.awt.Shape
似乎是最好的解决方案,但我不知道该怎么做。
更改应用程序以具有自绘形状
可能很好,但我必须计算 myslef contains
和其他更复杂的方法。
您可以根据您的 Shape 创建 Area 并调用 transform() 传递带有转换后坐标的 AffineTransform。
Area a=new Area(sourceShape);
AffineTransform at=new AffineTransform();
at.translate(horizontalShift, verticalShift);
Shape transformedShape=a.transform(at);
还没有测试过
But there is no translate
or move
methods on java.awt.Shape
s.
参见 AffineTransform.createTransformedShape(Shape)
,其中:
Returns a new Shape
object defined by the geometry of the specified Shape
after it has been transformed by this transform.
当然还有Graphics2D.translate(x,y)
..
Translates the origin of the Graphics2D
context to the point (x, y) in the current coordinate system. ..
@AndrewThompson 的解决方案似乎是最好的。我要试一试。
对于那些感兴趣的人(因为我必须进步)。我创建了一个扩展 java.awt.Shape
的接口,并为他的 位置 属性 设置了一个 setter。
每个特定形状都必须实现更新其位置所需的代码,但这比移动任何形状要容易得多。
我们有一个显示一种地图的应用程序。这个Swing应用主要画了一组java.awt.Shape
s with java.awt.Graphics2D#draw(Shape)
一切正常
现在,我必须扩展这个应用程序以允许我们的用户编辑(移动形状)地图。但是 java.awt.Shape
上没有 translate
或 move
方法。所以我无法更改形状的位置 (java.awt.Point
)。
我试图覆盖 java.awt.Shape#getPathIterator
以应用与形状位置匹配的翻译。但这很棘手,因为这种方法已经接受了我必须合并的转换,而且为了正确起见,PathIterator
应该从 (0, 0)
开始,因为它是相对于形状位置的。
无论如何,这不会起作用,因为 Graphics2D
似乎并不总是使用这种方法来绘制 java.awt.Shape
。
所以现在,我感觉有点失落。另一种解决方案可能是让形状自己绘制,但我必须重写应用程序的一部分。这不是问题,但我必须知道哪个可能是最好的解决方案:
找个技巧移动一个
java.awt.Shape
似乎是最好的解决方案,但我不知道该怎么做。更改应用程序以具有自绘形状 可能很好,但我必须计算 myslef
contains
和其他更复杂的方法。
您可以根据您的 Shape 创建 Area 并调用 transform() 传递带有转换后坐标的 AffineTransform。
Area a=new Area(sourceShape);
AffineTransform at=new AffineTransform();
at.translate(horizontalShift, verticalShift);
Shape transformedShape=a.transform(at);
还没有测试过
But there is no
translate
ormove
methods onjava.awt.Shape
s.
参见 AffineTransform.createTransformedShape(Shape)
,其中:
Returns a new
Shape
object defined by the geometry of the specifiedShape
after it has been transformed by this transform.
当然还有Graphics2D.translate(x,y)
..
Translates the origin of the
Graphics2D
context to the point (x, y) in the current coordinate system. ..
@AndrewThompson 的解决方案似乎是最好的。我要试一试。
对于那些感兴趣的人(因为我必须进步)。我创建了一个扩展 java.awt.Shape
的接口,并为他的 位置 属性 设置了一个 setter。
每个特定形状都必须实现更新其位置所需的代码,但这比移动任何形状要容易得多。