hello! im working ona program that reads in a file called pot4.asc. my button Read File , is supposed to read in a file when clicked. i already have all the work done in my model class , im just trying to get it to work with a GUI and a button. i cant figure out what im doing wrong.
my action listener is at the bottom , i called my model class and its readFromFile. shouldnt that work? im thinking maybe i missed something with the textfield?
import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Gui { public static void main(String[] args) { JFrame frame = new JFrame("TeaPot"); JTextField textField = new JTextField("pot4.asc",10); GridLayout applicationLayout = new GridLayout(1,2); frame.setLayout(applicationLayout); JPanel rightPanel = new JPanel(); JPanel leftPanel = new JPanel(); rightPanel.add(textField); GridLayout canvasLayout = new GridLayout(1,1); leftPanel.setLayout(canvasLayout); MyCanvas canvas = new MyCanvas(300,300); leftPanel.add(canvas); FlowLayout buttonLayout = new FlowLayout(); rightPanel.setLayout(buttonLayout); JButton button1 = new JButton("Read In Data"); button1.addActionListener(new eventRead()); rightPanel.add( button1 ); JButton button2 = new JButton("Write Out"); button2.addActionListener(new eventWrite()); rightPanel.add( button2 ); JButton button3 = new JButton("Statistical Data"); button3.addActionListener(new eventPrintStatistics()); rightPanel.add( button3 ); frame.add(leftPanel); frame.add(rightPanel); frame.setSize(800, 400); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } class eventRead implements ActionListener{ public void actionPerformed(ActionEvent e) { String inputFilename = "pot4.asc"; JLabel label = new JLabel("Data Read"); Model m = new Model(); try{ m.readFromFile(inputFilename); label.setText("File Read"); System.out.println("File Read"); } catch (Exception e1){ System.out.println(e1.getMessage()); inputFilename="pot4.asc"; System.out.println("Using "+ inputFilename+ " instead"); } } }
model class
import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Model { ArrayList<Triangle> triangleArray = new ArrayList<Triangle>(); void readFromFile(String filename) throws Exception { File file = new File(filename); readFromFile(file); } void writeDataToFile(File file) { try{ PrintWriter out = new PrintWriter(file); for(Triangle t: triangleArray){ out.println("triangle"); out.print( t.toString() ); } out.close(); }catch(Exception e){ } } void writeInfoToFile(File file) { double area = computeSurfaceArea(); double nTriangle = triangleArray.size(); double min = computeMin(); double max= computeMax(); double averageArea = area / nTriangle; try{ PrintWriter out = new PrintWriter(file); out.println("Total number of triangles = "+nTriangle); out.println("Area = "+area); out.println("Min = "+min); out.println("Max = "+max); out.println("Average Area = "+averageArea); out.close(); }catch(Exception e){ e.printStackTrace(); } } void readFromFile(File file) throws Exception { try { Scanner s2 = new Scanner(file); while (s2.hasNext()) { s2.nextLine(); // tosss the first line String[] threeLines = { //array with 3 lines, reads in 3 lines s2.nextLine(), s2.nextLine(), s2.nextLine() }; Triangle newTriangleObject = new Triangle(threeLines); triangleArray.add( newTriangleObject ); } } catch (Exception e) { System.out.println("Model.readFromFile: "+e.getMessage()); String bottle = "error: "+e.getMessage(); throw new Exception(bottle); //e.printStackTrace(); } } double computeSurfaceArea() { double v = 0; // loop through each triangle and sum up their areas for ( Triangle t : triangleArray ) { double sArea = t.computeArea(); v += sArea; } return v; } double computeMax(){ double max= -Double.MAX_VALUE; for(Triangle t: triangleArray){ max = (max > t.computeArea() ? max : t.computeArea()); } return max; } double computeMin(){ double min= Double.MAX_VALUE; for ( Triangle t : triangleArray) { if (min > t.computeArea() ){ min =t.computeArea(); } } return min; } }