简体   繁体   English

Java AWT - 绘制由平滑曲线连接的多边形

[英]Java AWT - Draw a Polygon connected by smooth curved lines

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. 也就是说,我在画布上绘制了一个Polygon。 It can have an arbitrary number of points, but let's assume 12 for now. 它可以有任意数量的点,但我们假设现在是12。 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 谢天谢地,马特

To get started: 开始:
* create a GeneralPath *创建一个GeneralPath
* add curves to the path using GeneralPath.curveTo(float x1, float y1, float x2, float y2, float x3, float y3) *使用GeneralPath.curveTo将路径添加到路径中(float x1,float y1,float x2,float y2,float x3,float y3)
* get a Graphics2D object *获取Graphics2D对象
* use Graphics2D.draw(Shape s) to draw the GeneralPath (which is a shape) *使用Graphics2D.draw(Shape s)绘制GeneralPath(这是一个形状)

Optionally you can set the line cap and join style: 您可以选择设置线帽和连接样式:
* Create a BasicStroke (width=1, cap=CAP_ROUND , join=JOIN_ROUND ) *创建一个BasicStroke (宽度= 1,上限= CAP_ROUND,join = JOIN_ROUND)
* use Graphics2D.setStroke to set the stroke *使用Graphics2D.setStroke设置笔画

The only hard part is that you have to figure out the x3,y3 of the method curveTo 唯一困难的是你必须弄清楚方法curveTo的x3,y3

With a J2SE JVM you can cast any Graphics object to a Graphics2D object and then use this to draw Bezier lines. 使用J2SE JVM,您可以将任何Graphics对象转换为Graphics2D对象,然后使用它来绘制Bezier线。

See here for an example 请看这里的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM