Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: Drawing curved lines

Tags:

java

swing

I'm trying to draw curved lines in Java. A simple bezier curve with an (X,Y) start, an (X,Y) end, and a curve amount would suffice.

I can't find a way to do this in Swing. If it's not in Swing, is there some simple math I can use to do it? And how would I implement it in Swing?

Edit: I'm aware of how to draw shapes and lines by overriding the paint(Graphics g) method.

like image 396
Derek Avatar asked Mar 05 '12 07:03

Derek


2 Answers

You can draw a Bézier curve using the Java 2D Object Path2D.Double. Just call the method curveTo(float x1, float y1, float x2, float y2, float x3, float y3) and define the 3 coordinate.

  • first Bézier control point
  • second Bézier control point
  • final end point
like image 135
Seffel Avatar answered Sep 27 '22 17:09

Seffel


Better would be read and following basic tutorial about Graphics2D, tons examples are here,

  • some of examples are outdated by using wrong method paint() instead of correct method for painting for Swing JComponent paintComponent(),

  • simple change wrong method paint() to the paintComponent(),

like image 30
mKorbel Avatar answered Sep 27 '22 15:09

mKorbel