1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class JPanelTest extends JPanel {
private JFreeChart chart;
private ChartPanel panelChart;
private XYSeries _series;
public JPanelTest() {
this.setLayout(new BorderLayout());
_series = new XYSeries("Bidon");
_series.add(new XYDataItem(5,5));
chart = ChartFactory.createScatterPlot(null, "X", "Y",
new XYSeriesCollection(_series),
PlotOrientation.VERTICAL, false, true, false);
panelChart = new ChartPanel(chart);
this.add(panelChart);
}
@Override
public void paint(Graphics g2d) {
super.paint(g2d);
Rectangle2D dataArea = panelChart.getChartRenderingInfo().getPlotInfo().getDataArea();
while (dataArea.getWidth() == 0) {
try {
System.out.println("thread sleep 100ms");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
dataArea = panelChart.getChartRenderingInfo().getPlotInfo().getDataArea();
}
System.out.println("frame bounds: "+this.getBounds());
System.out.println("dataArea(green): "+dataArea);
g2d.setColor(Color.GREEN);
g2d.drawRect((int)dataArea.getX(), (int)dataArea.getY(), (int)dataArea.getWidth(), (int)dataArea.getHeight());
Rectangle2D plotArea = panelChart.getChartRenderingInfo().getPlotInfo().getPlotArea();
System.out.println("plotArea(red): "+plotArea);
// g2d.setColor(Color.RED);
// g2d.drawRect((int)plotArea.getX(), (int)plotArea.getY(), (int)plotArea.getWidth(), (int)plotArea.getHeight());
Rectangle2D chartArea = panelChart.getChartRenderingInfo().getChartArea();
System.out.println("chartArea(blue): "+chartArea);
// g2d.setColor(Color.BLUE);
// g2d.drawRect((int)chartArea.getX(), (int)chartArea.getY(), (int)chartArea.getWidth(), (int)chartArea.getHeight());
}
} |
Partager