Java AWT - 绘制由平滑曲线连接的多边形
在这里我问更多愚蠢的图形问题。希望很快我就能离开图形世界,再次将自己牢牢地置于无色的中间层。我对那些能够通过代码以令人愉悦的方式摆弄图像的人产生了新的敬意。
也就是说,我正在画布上绘制一个多边形。它可以有任意数量的点,但现在我们假设 12 个。所实现的多边形是通过直线从点到点连接的。我想应用某种类型的变换,以便更“自然”地绘制形状,就像有人用钢笔/铅笔连接点一样。
我不确定这样的描述是否太模糊。我认为我正在寻找的是贝塞尔曲线,但我是一个图形(和几何)松懈者。一般来说,我对新颖的解决方案感兴趣,只是让直边多边形看起来更像一滴墨水的东西。也许可以通过控制来实现或多或少“自然”的形状。
如果您需要任何其他信息,请随时询问。
衷心感谢, 马特
Here I am asking more silly graphics questions. Hopefully soon I will leave the world of graphics behind and plant myself firmly in the color-less middle-tier again. I have a newfound respect for people who are able to fiddle with images in pleasing ways through code.
That said, I am drawing a Polygon on a canvas. It can have an arbitrary number of points, but let's assume 12 for now. The polygon, as implemented, is connected via straight lines from point to point. I would like to apply some type of transformation so that the shape is drawn more 'naturally', as if someone had connected the points with a pen/pencil.
I'm not sure if this is too vague of a description. I think what I'm looking for is a bezier curve, but I'm a graphics (and geometry) slack-jaw. I'm interested in novel solutions in general, just something that makes a straight-sided polygon look more like a blob of ink. Maybe with controls to achieve a more or less 'natural' shape.
If you need any additional info, please don't hesitate to ask.
Kind thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开始使用:
* 创建一个 GeneralPath
* 使用 GeneralPath.curveTo(float x1, float y1, float x2, float y2, float x3, float y3) 将曲线添加到路径
* 获取 Graphics2D 对象
* 使用 Graphics2D.draw(Shape s) 绘制 GeneralPath(这是一个形状)
您可以选择设置线帽和连接样式:
* 创建一个 BasicStroke (width=1 , cap=CAP_ROUND , join=JOIN_ROUND )
* 使用Graphics2D.setStroke设置描边
唯一困难的是你必须计算出curveTo方法的x3,y3
To get started:
* create a GeneralPath
* add curves to the path using GeneralPath.curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
* get a Graphics2D object
* use Graphics2D.draw(Shape s) to draw the GeneralPath (which is a shape)
Optionally you can set the line cap and join style:
* Create a BasicStroke (width=1, cap=CAP_ROUND , join=JOIN_ROUND )
* use Graphics2D.setStroke to set the stroke
The only hard part is that you have to figure out the x3,y3 of the method curveTo
使用 J2SE JVM,您可以将任何 Graphics 对象转换为 Graphics2D 对象,然后使用它来绘制贝塞尔线。
请参阅此处的示例
With a J2SE JVM you can cast any Graphics object to a Graphics2D object and then use this to draw Bezier lines.
See here for an example