diff --git a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java index 10a471404d30bec77bb95141698f7c65e751427b..37d374fed62f3a9d73433f51fc4fe99cc37efa73 100644 --- a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java +++ b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java @@ -19,6 +19,8 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; +import java.util.ArrayList; +import java.util.List; import javax.swing.BoxLayout; import javax.swing.JFrame; @@ -26,13 +28,12 @@ import javax.swing.JPanel; import com.xeiam.xchart.Chart; - /** * @author timmolter */ public class SwingWrapper { - private Chart[] charts; + private List<Chart> charts = new ArrayList<Chart>(); private int numRows; private int numColumns; @@ -43,17 +44,31 @@ public class SwingWrapper { */ public SwingWrapper(Chart chart) { - this.charts = new Chart[1]; - charts[0] = chart; + this.charts.add(chart); } /** - * Constructor + * Deprecated Constructor - use the one that takes a Collection! This will be removed in next version. * * @param charts */ + @Deprecated public SwingWrapper(Chart[] charts, int numRows, int numColumns) { + for (int i = 0; i < charts.length; i++) { + this.charts.add(charts[i]); + } + this.numRows = numRows; + this.numColumns = numColumns; + } + + /** + * Constructor + * + * @param charts + */ + public SwingWrapper(List<Chart> charts, int numRows, int numColumns) { + this.charts = charts; this.numRows = numRows; this.numColumns = numColumns; @@ -76,7 +91,7 @@ public class SwingWrapper { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); - JPanel chartPanel = new ChartJPanel(charts[0]); + JPanel chartPanel = new ChartJPanel(charts.get(0)); frame.getContentPane().add(chartPanel); // Display the window. @@ -103,10 +118,9 @@ public class SwingWrapper { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridLayout(numRows, numColumns)); - for (int i = 0; i < charts.length; i++) { - - if (charts[i] != null) { - JPanel chartPanel = new ChartJPanel(charts[i]); + for (Chart chart : charts) { + if (chart != null) { + JPanel chartPanel = new ChartJPanel(chart); frame.getContentPane().add(chartPanel); } else { JPanel chartPanel = new JPanel(); diff --git a/src/test/java/com/xeiam/xchart/example/Example4.java b/src/test/java/com/xeiam/xchart/example/Example4.java index 56fbeee651bb7381c99dea1c105641b42581e1af..1978556ae0b964585f0eb32d4a9e1a8f7f015163 100644 --- a/src/test/java/com/xeiam/xchart/example/Example4.java +++ b/src/test/java/com/xeiam/xchart/example/Example4.java @@ -15,6 +15,9 @@ */ package com.xeiam.xchart.example; +import java.util.ArrayList; +import java.util.List; + import com.xeiam.xchart.Chart; import com.xeiam.xchart.QuickChart; import com.xeiam.xchart.swing.SwingWrapper; @@ -30,13 +33,12 @@ public class Example4 { int numRows = 2; int numCols = 2; - Chart[] charts = new Chart[numRows * numCols]; - int chartCount = 0; + List<Chart> charts = new ArrayList<Chart>(); + for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { - - charts[chartCount++] = QuickChart.getChart(i + "," + j, "X", "Y", null, null, getRandomWalk(1000)); + charts.add(QuickChart.getChart(i + "," + j, "X", "Y", null, null, getRandomWalk(1000))); } } new SwingWrapper(charts, numRows, numCols).displayChartMatrix();