Java – How do I draw X, Y coordinates in the order in which they are added?

How do I draw X, Y coordinates in the order in which they are added?… here is a solution to the problem.

How do I draw X, Y coordinates in the order in which they are added?

I

want to connect the X and Y coordinates in the order I entered to form a circle.

I found a program that plots X,Y coordinates in Java.
Then I added my circle data, but the program concatenated the nearest X,Y coordinates instead of the ordered X,Y.

public Graph(final String title) {

super(title);
    final XYSeries series = new XYSeries("Random Data");

series.add(2.000 , 0);
    series.add(0 , 2.000);
    series.add(-2.000 , 0);
    series.add(0 , -2.000);
    final XYSeriesCollection data = new XYSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Series Demo",
            "X",
            "Y",
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
    );

I expect the result to be a square diagram. result_ss

Solution

XYSeries API specify,

By default, items in the series will be sorted into ascending order by x-value, and duplicate x-values are permitted.

You can use the appropriate constructor to solve this problem, suggesting here :

final XYSeries series = new XYSeries("Random Data", false);

In the image below, I added an extra data point to close the graph:

image

For arbitrary shapes, also consider XYShapeAnnotation, see here

image

Related Problems and Solutions