File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes drawing a curve from points Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawing a curve from points" Watch "drawing a curve from points" New topic

drawing a curve from points

Raschin Ghanad

Joined: Jul 28, 2004
Posts: 10
I have a little question about how to make curves out of points.

My function has many points and I have to get them to one curve.
At the moment, it is just necessary to put a line(/path) from point to point.

I thought about using the GeneralPath - class, but I don't know how to use it.

Maybe someone can tell me which class to use is the best for this problem?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

This question would get a lot more attention in the Swing/AWT forum, so I'm going to move it there.

[Jess in Action][AskingGoodQuestions]
Ryan Smith
Ranch Hand

Joined: Jun 29, 2004
Posts: 40

I am a relative newcomer to this sort of stuff, but here's what I'm thinking:

First, I don't know of any class that would do what you want to do.

However, here is what I would start with: You have a collection of points, probably in two arrays, int xPoints[] and int yPoints[], or maybe just in one array, points coords[]. If you want to make a smooth looking curve with this set of points, you need to focus on what determines the slope of the curve. That leaves you with two options: (1) you could try to use the drawArc method in Graphics class, fitting it to your points, or (2) you could draw your own curve based on the slope of the curve at the last endpoint and then the slope at the next endpoint.

I think the second strategy would be more precise, yet slower and more tedious to code, whereas the first may be easier to implement, but you won't have as much control.

If you could be more specific, that would be helpful.

Hope I haven't been too rambling.
Ryan Smith
Ranch Hand

Joined: Jun 29, 2004
Posts: 40
Oh, I just re-read your post, and all you need is lines connecting the points! Ignore the above post in that case!

What I would do is use Graphics's drawLine method, implemented in a for loop.

For example:

[ July 31, 2004: Message edited by: Ryan Smith ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's one way to connect the points with a curve. There may be many other or better ways. This takes five clicks on the drawing surface to get started.

The challenge is to figure out where/how to locate the control point for the quadTo method. With curveTo you have two control points to figure out.

[ August 01, 2004: Message edited by: Craig Wood ]
Raschin Ghanad

Joined: Jul 28, 2004
Posts: 10
thanks, but I think this is not exactly what I need...

To the first answer from Ryan Smith :

The second post from you could maybe work, if I alter it.

I have already a curve that connects the Points together with
lines. So far is everything fine.

The problem is that you have to switch in the program how to display the curve.
So I have also to display the curve with rounded lines, and also as stairs.

To the answer from Craig Wood :

This seems to be a better idea (to work with a Path and the quadTo/curveTo- method) , but your code does not round the lines in the way I need it.
As I said, the problem is to connect the points in a way, that there is a bended line, that displays the increase/decrease of the data.

I think your curves are something like the Bezier-curves....

If I would know how to get the right control-points for my curve, the problem would be solved, I think...
[ August 02, 2004: Message edited by: Raschin Ghanad ]
I agree. Here's the link:
subject: drawing a curve from points
It's not a secret anymore!