ncr7
July 23rd, 2009, 03:42 PM
I am trying to get this GUI to work to where after you choose a file it shows the graph automatically after the txt file is choose. I have it showing up but it only shows up after I resize it. I'm thinking I have a .setVisible in the wrong spot or I am missing something in the code.
// AvsI.java
//Creates GUI to eventually show Semimajor axis vs Inclination graph.
package avsi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
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.XYSeriesCollection;
public class AvsI{
JLabel label;
Graph graph;
JFrame Panel;
JButton Open;
JMenu List;
XYDataset Data;
ChartFrame Frame;
JFreeChart chart;
File Import;
ChartPanel chartPanel;
Boolean TF;
protected static FileDialog fileDialog;
public AvsI() {
Panel = new JFrame("Graph");
graph = new Graph();
label = new JLabel("GRAPH");
Open = new JButton("File");
List = new JMenu("List");
TF = new Boolean(false);
Panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Open.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
TF = true;
Data = null;
try {
ImportFileDeclared();
Data = graph.getData();
JFreeChart chart = createChart(Data);
chartPanel = new ChartPanel(chart);
System.out.println("Sofar so good");
Panel.getContentPane().add(chartPanel, BorderLayout.CENTER);
chartPanel.setPopupMenu(null);
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
chartPanel.setVisible(true);
}catch (Exception e) {
e.printStackTrace();
}
}
});
Panel.setPreferredSize(new Dimension(500,500));
Panel.getContentPane().add(label, BorderLayout.NORTH);
Panel.getContentPane().add(Open, BorderLayout.SOUTH);
Panel.getContentPane().add(List,BorderLayout.WEST);
Panel.repaint();
Panel.pack();
Panel.setVisible(true);
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot Demo 1",
"Inclination", "Semi-major Axis", dataset, PlotOrientation.VERTICAL, true, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setNoDataMessage("NO DATA");
plot.setDomainZeroBaselineVisible(true);
plot.setRangeZeroBaselineVisible(true);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesOutlinePaint(0, Color.black);
renderer.setUseOutlinePaint(true);
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setAutoRangeIncludesZero(false);
domainAxis.setTickMarkInsideLength(2.0f);
domainAxis.setTickMarkOutsideLength(0.0f);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickMarkInsideLength(2.0f);
rangeAxis.setTickMarkOutsideLength(0.0f);
return chart;
}
@SuppressWarnings("deprecation")
public void ImportFileDeclared() {
Frame frame = new Frame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
String file = fileDialog.getFile();
String directory = fileDialog.getDirectory();
if(file != null){
File f = new File(directory, file);
graph.main(f);
}
}
public static void main(String args[]) {
System.out.print("Its started");
new AvsI();
}
}
// AvsI.java
//Creates GUI to eventually show Semimajor axis vs Inclination graph.
package avsi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
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.XYSeriesCollection;
public class AvsI{
JLabel label;
Graph graph;
JFrame Panel;
JButton Open;
JMenu List;
XYDataset Data;
ChartFrame Frame;
JFreeChart chart;
File Import;
ChartPanel chartPanel;
Boolean TF;
protected static FileDialog fileDialog;
public AvsI() {
Panel = new JFrame("Graph");
graph = new Graph();
label = new JLabel("GRAPH");
Open = new JButton("File");
List = new JMenu("List");
TF = new Boolean(false);
Panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Open.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
TF = true;
Data = null;
try {
ImportFileDeclared();
Data = graph.getData();
JFreeChart chart = createChart(Data);
chartPanel = new ChartPanel(chart);
System.out.println("Sofar so good");
Panel.getContentPane().add(chartPanel, BorderLayout.CENTER);
chartPanel.setPopupMenu(null);
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
chartPanel.setVisible(true);
}catch (Exception e) {
e.printStackTrace();
}
}
});
Panel.setPreferredSize(new Dimension(500,500));
Panel.getContentPane().add(label, BorderLayout.NORTH);
Panel.getContentPane().add(Open, BorderLayout.SOUTH);
Panel.getContentPane().add(List,BorderLayout.WEST);
Panel.repaint();
Panel.pack();
Panel.setVisible(true);
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot Demo 1",
"Inclination", "Semi-major Axis", dataset, PlotOrientation.VERTICAL, true, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setNoDataMessage("NO DATA");
plot.setDomainZeroBaselineVisible(true);
plot.setRangeZeroBaselineVisible(true);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesOutlinePaint(0, Color.black);
renderer.setUseOutlinePaint(true);
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setAutoRangeIncludesZero(false);
domainAxis.setTickMarkInsideLength(2.0f);
domainAxis.setTickMarkOutsideLength(0.0f);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickMarkInsideLength(2.0f);
rangeAxis.setTickMarkOutsideLength(0.0f);
return chart;
}
@SuppressWarnings("deprecation")
public void ImportFileDeclared() {
Frame frame = new Frame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
String file = fileDialog.getFile();
String directory = fileDialog.getDirectory();
if(file != null){
File f = new File(directory, file);
graph.main(f);
}
}
public static void main(String args[]) {
System.out.print("Its started");
new AvsI();
}
}