I have wrote a code that creates two scatters and two JPanels placed on a JFrame. However I could not make them fit automatically to the size of the window when it is being resized. Here is the code for one of the drawing classes:
class ALTCanvas extends JPanel{
private static final long serialVersionUID = 1L;
private XYSeries alts = new XYSeries("Altitude");
public ALTCanvas() {
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
}
private void update(double xnew, double ynew) {
alts.add(new XYDataItem(xnew, ynew));
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
"", "Time [epoch]", "Altitude [m]", createSampleData(),
PlotOrientation.VERTICAL, false, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
XYItemRenderer renderer = xyPlot.getRenderer();
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setTickUnit(new NumberTickUnit(0.5));
return new ChartPanel(jfreechart){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(alts);
return xySeriesCollection;
}
private String stdCalc(){
double sum = 0, avg = 0;
double var = 0;
for(int i = 0; i < alts.getItemCount(); i++){
sum += (double) alts.getY(i);
}
avg = sum/alts.getItemCount();
for(int i = 0; i < alts.getItemCount(); i++){
var += ((double)alts.getY(i) - avg)*((double)alts.getY(i) - avg);
}
String str = "";
str += Math.sqrt(var);
return str;
}
}
Here is the code for the initialization of the graphics:
public void initGrafics() {
// create and set up the window
JFrame frame = new JFrame("Position / Altitude");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
Container C = frame.getContentPane();
// create canvas objects
PC = new POSCanvas();
PC.setBorder(BorderFactory.createRaisedBevelBorder());
AC = new ALTCanvas();
AC.setBorder(BorderFactory.createRaisedBevelBorder());
JPanel left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
JLabel prcP = new JLabel("Precision [m]: "); JLabel acrP = new JLabel("Accuracy [m]: ");
prcTp = new JTextField(" "); acrTp = new JTextField(" ");
JPanel labelsP = new JPanel();
labelsP.setLayout(new FlowLayout());
labelsP.add(prcP); labelsP.add(prcTp); labelsP.add(acrP); labelsP.add(acrTp);
left.add(PC); left.add(labelsP);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
JLabel stdA = new JLabel("Standard deviation [m]: "); stdTa = new JTextField(" ");
JPanel labelsA = new JPanel();
labelsA.setLayout(new FlowLayout());
labelsA.add(stdA); labelsA.add(stdTa);
right.add(AC); right.add(labelsA);
// add objects to the pane
C.setLayout(new BoxLayout(C, BoxLayout.X_AXIS));
C.add(left); C.add(right);
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setLocation(500, 500);
}
Here is what it looks like after resizing:
As one can see, the charts does not fit the size of the window. Can anybody help me how to fix it? Any help would be appreciated, thank you.