Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw a curve that varies in thickness along its path?

Tags:

java

graphics

I'm capturing data from a tablet using Java (JPen library rocks) and would like to be able to paint a penstroke in a more natural way.

Currently I'm drawing the pen stroke as straight line segments each with a different Stroke thickness.

There has to be something in Java's Graphics Library that lets me to this more efficiently.

Right?

like image 304
Allain Lalonde Avatar asked Nov 06 '22 23:11

Allain Lalonde


2 Answers

I've never done this, but here are a couple things you could try. First, you could implement a custom Stroke that creates skinny trapezoids. The width of the end caps would be a function of the pressure at the end points. If that works, you could try to make the line segments look more natural by using Bezier curves to form "curvy trapezoids". You might be able to use QuadCurve2D to help.

like image 167
Will Avatar answered Nov 12 '22 13:11

Will


There's a more general solution available at least. The feature was added to Inkscape based on a recent algorithm. You can see it applied directly to your problem in some screenshots. It can extrude any shape brush along the path to mimic a paintbrush for example, but you'd have to port it to Java from the algorithm in the first link or from the Inkscape sources. Also, it's covered by patents so you'd have to release your code under the GPL (the author gives explicit permission) or buy a patent license.

like image 41
jjrv Avatar answered Nov 12 '22 13:11

jjrv