3

I have a list of x,y points which printed,display an uneven peak curve line.

enter image description here

The above image was generated by just painting the points on a java paint component. I used the following way to paint them on a paint component.

g.drawline(pointX,pointY,pointX,pointY)

Are there better ways to paint such wave line? I checked some of the similar questions,often they need to print a curve or peak,but my line is not always a peak as some times its flats out and other times they are bizarre.

Balaram26
  • 1,349
  • 3
  • 15
  • 32
  • 2
    Maybe something like [this](http://stackoverflow.com/questions/15864441/how-to-make-a-line-curve-through-points)? Or something like [this](https://docs.oracle.com/javase/tutorial/2d/geometry/primitives.html)? – MadProgrammer Jul 20 '15 at 02:19
  • It was helpful,but when using lines and points to draw curve lines,they tend to pixalate when we zoom in using 'g.scale' function. Are there ways to smooth the curves or should i be looking for some curve lib for these kind of things? – Balaram26 Jul 20 '15 at 03:21
  • 1
    Take a look at [`RenderingHints`](https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html), but, remember, scaling up is hard then scaling and down and instead of scaling the pixels, I'd scale the points and simply repaint them – MadProgrammer Jul 20 '15 at 03:28

1 Answers1

2

The simplest way to draw polylines with java.awt.Graphics is to use the drawPolyline method. It requires you to have your x and y coordinates stored in separate int[] arrays, but it is much faster and clearer than to draw each line segment individually.

If you need floating-point coordinates, the best way would be to use a Shape object with Graphics2D. Unfortunately, Java does not seem to provide a polyline Shape implementation, but you can easily use Path2D:

Graphics2D graphics = /* your graphics object */;
double[] x = /* x coordinates of polyline */;
double[] y = /* y coordinates of polyline */;

Path2D polyline = new Path2D.Double();
polyline.moveTo(x[0], y[0]);
for (int i = 1; i < x.length; i++) {
    polyline.lineTo(x[i], y[i]);
}

graphics.draw(polyline);

This way allows you to easily transform you coordinates, too -- however, it may be more efficient to transform the view, of course.

Franz D.
  • 1,061
  • 10
  • 23