如何在 Java 中调整 AWT 多边形的大小
Java API 中是否有任何内置方法可以让我调整多边形的大小?
编写我自己的方法是一个非常简单的任务,还是已经有其他方法了?
Are there any in-built methods in the Java API which would allow me to resize a polygon?
Would it be quite a simple task to write my own method, or are there any others already made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有内置任何东西,尽管如此,当您绘制多边形时,您可以使用应用的变换矩阵来绘制多边形,该变换矩阵可以为您缩放多边形。 (或旋转、倾斜等)。
参见
Graphics2D.setTransform(transform);
假设您正在 JPanel 中绘制多边形,并且您已经重写了 JPanel 的 PaintComponent 方法。将 Graphics 对象转换为 Graphics2D 对象,并使用变换来适当缩放它:
您有可能可以在其他地方(一次)设置变换矩阵,而不是每次都在 PaintComponent 方法中设置,但我在这里这样做是为了展示如何做到这一点。
另请注意,这将移动多边形,您可能希望将其应用于变换:
,这样,物体不会移动,只会缩放。
No, nothing built in, altho, when you draw the polygon, you can draw the polygon with a transformation matrix applied which could scale the polygon for you. (or rotate, skew, etc, etc).
see
Graphics2D.setTransform(transform);
Lets assume you are drawing the polygon in a JPanel, and you have overridden the paintComponent method of JPanel. Cast the Graphics object to a Graphics2D object, and use transforms to scale it as appropriate:
Chances are you can set up the transformation matrix elsewhere (once) instead of each time in the paintComponent method, but i did here to show how to do it.
Also note, that this will move the polygon, you would probably want apply this to the transform:
in this way, the object doesn't move it just scales.
是的,
AffineTransform.createTransformedShape(Shape)
将创建任何Shape
的转换副本(可能是Polygon
或Path2D
。)Yes,
AffineTransform.createTransformedShape(Shape)
will create a transformed copy of anyShape
(which may be aPolygon
orPath2D
.)