java2d 对象的非破坏性转换
我希望能够在由 Path2D.Double 组成的 Java2D 场景上缩放和取消缩放,而无需 只需扩大距离即可加粗线条。
我尝试对 PaintComponent 方法接收的 Graphics2D 对象应用转换,但这会使线条更粗。我发现的唯一方法是对线条应用转换(例如 line.transform(AffineTransform.getScaleInstance(2d,2d))
),但每次我再次缩放和取消缩放时,我都会丢失信息,因为的浮点错误。
长话短说:这种转变是破坏性的。有没有办法说“我想在不修改线条内容的情况下应用该转换来绘制这条线”?
I would like to be able to zoom and unzoom on a Java2D scene made of Path2D.Double without
thickening the lines, just by dilating the distances.
I'v tried to apply a transformation to the Graphics2D object the paintComponent method receives, but this makes the lines thicker. The only way I found was to apply a transformation to the lines (line.transform(AffineTransform.getScaleInstance(2d,2d))
for instance) but every time I zoom and unzoom again, I lose information because of floating point errors.
To make a long story short: the transformations are destructive. is there a way to say "i want to draw this line with that transformation applied without modifying the content of the line"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案:我根据 Graphic2D 中的比例因子更改线宽,这样我可以将变换应用于 Graphic2D 本身,并且不会破坏 Path2D 中包含的原始坐标。
I found the solution: I change the line width according to the scale factor in Graphic2D, that way I can apply the transform to Graphic2D itself and it doesn't destruct the original coordinates contained in the Path2D.
正如您所发现的,更改
Graphics2D
变换会影响所有绘图,但不妨碍在paintComponent()
内保存、修改和恢复变换。在此示例中,内容是在缩放的上下文中绘制的,而目标对象周围的 Rectangle.Double
未缩放绘制。附录:示例使用显式转换,但您可以使用
AffineTransform,而不是。与 Rectangle2D 一样,Path2D 实现了 Shape 接口,因此您可以使用
createInverse()
和createTransformedShape()
,相应地。这是一个相关的示例。As you found, changing the
Graphics2D
transform affects all drawing, but nothing precludes saving, modifying and restoring the transform insidepaintComponent()
. In this example, the content is drawn in a scaled context, while aRectangle.Double
surrounding the target object is drawn unscaled.Addendum: The example uses explicit transformations, but you can use
AffineTransform
, instead. LikeRectangle2D
,Path2D
implements theShape
interface, so you can usecreateInverse()
andcreateTransformedShape()
, accordingly. Here's a related example.