java2d 对象的非破坏性转换

发布于 2024-09-09 09:33:51 字数 309 浏览 7 评论 0原文

我希望能够在由 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 技术交流群。

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

发布评论

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

评论(2

我不是你的备胎 2024-09-16 09:33:51

我找到了解决方案:我根据 Graphic2D 中的比例因子更改线宽,这样我可以将变换应用于 Graphic2D 本身,并且不会破坏 Path2D 中包含的原始坐标。

tr = g.getTransform()
g.transform(AffineTransform.getScaleInstance(scaleFactor,scaleFactor))
g.setStroke(new java.awt.BasicStroke(1.0f/scaleFactor.toFloat))
/* draw lines */
g.setTransform(tr)

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.

tr = g.getTransform()
g.transform(AffineTransform.getScaleInstance(scaleFactor,scaleFactor))
g.setStroke(new java.awt.BasicStroke(1.0f/scaleFactor.toFloat))
/* draw lines */
g.setTransform(tr)
故人爱我别走 2024-09-16 09:33:51

正如您所发现的,更改 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 inside paintComponent(). In this example, the content is drawn in a scaled context, while a Rectangle.Double surrounding the target object is drawn unscaled.

Addendum: The example uses explicit transformations, but you can use AffineTransform, instead. Like Rectangle2D, Path2D implements the Shape interface, so you can use createInverse() and createTransformedShape(), accordingly. Here's a related example.

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