2

I have drawn multiple ellipses using a loop as shown below, and the results are perfect using one color for all the ellipses, but my target is to color each ellipse with different color. Is there any way to let the property Color.BLUE change its value in each iteration?

for (int i = 0; i < 3; i++)
{
    XYShapeAnnotation unitCircle1 = new XYShapeAnnotation(
        new Ellipse2D.Double((FinalArayOfOptpar[s][i] - Math.abs(FinalArayOfOptpar[s][i + 2])),
            (FinalArayOfOptpar[s][i + 1] - Math.abs(FinalArayOfOptpar[s][i + 3])),
            Math.abs(FinalArayOfOptpar[s][i + 2] * 2.0), Math.abs(FinalArayOfOptpar[s][i + 3] * 2.0)),
        new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
            10.2f), Color.BLUE);
    xyPlot.addAnnotation(unitCircle1);
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
M.Kassim
  • 33
  • 5
  • 1
    Why not create multiple instances of `XYShapeAnnotation`, for [example](http://stackoverflow.com/a/6802375/230513)? – trashgod Feb 04 '16 at 21:33
  • Thank you .......The code I have illustrated above is just an example of my orginal code, in which tens of XYShapeAnnotations will be created, and the parameters of each ellipse are got from a particular calculation process. So creating multiple instances of XYShapeAnnotation will not work for my purpose. I need only a way to change the color using the above loop. – M.Kassim Feb 05 '16 at 14:42

1 Answers1

4

tens of XYShapeAnnotations will be created…so creating multiple instances of XYShapeAnnotation will not work for my purpose.

Happily, an instance XYShapeAnnotation is small—just 48 bytes each in the example below. You'll want to profile to be sure.

shape annotation image

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.geom.Ellipse2D;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/a/35236100/230513
 */
public class AnnotationTest {

    private static final BasicStroke stroke = new BasicStroke(2.0f);
    private static final int N = 16;
    private static final int S = 8;

    public static void main(String[] args) {
        EventQueue.invokeLater(new AnnotationTest()::display);
    }

    private void display() {
        XYDataset data = createDataset();
        JFreeChart chart = ChartFactory.createXYLineChart("ArcTest", "X", "Y",
            data, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer
            = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        for (int i = 0; i < N; i++) {
            double x = data.getXValue(0, i) - S / 2;
            double y = data.getYValue(0, i) - S / 2;
            Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, S, S);
            Color color = Color.getHSBColor((float) i / N, 1, 1);
            renderer.addAnnotation(new XYShapeAnnotation(ellipse, stroke, color));
        }
        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("ArcTest");
        for (int i = 0; i < N; i++) {
            series.add(i * S, i * S);
        }
        result.addSeries(series);
        return result;
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thank you for the valued help...... I have used the method getHSBColor() that is provided in the example and it works perfectly for my purpose.... Thank you very much – M.Kassim Feb 06 '16 at 20:58
  • 1
    Color.getHSBColor((float) i / N, 1, 1); line helped me – SatyaRajC Aug 27 '21 at 11:24
  • @SatyaRajC: More examples are seen [here](https://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20Color.getHSBColor). – trashgod Aug 27 '21 at 16:21