Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 821
Matt Avatar asked Aug 15 '10 23:08

Matt


2 Answers

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

like image 69
Davy Meers Avatar answered Sep 24 '22 02:09

Davy Meers


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

like image 38
x4u Avatar answered Sep 22 '22 02:09

x4u