Results 1 to 4 of 4
  1. #1
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default java GUI , reading in files

    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?
    Java Code:
    
    
    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

    Java Code:
    
    
    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;
    
    	}  
    
    
    
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: java GUI , reading in files

    Was your code produce any error message when it was executed, maybe the file pot4.asc was not available for the program to read?

  3. #3
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: java GUI , reading in files

    no , theres no problem with errors. when i run it , my statisical stats are writing out as null. so i was guessing maybe i was reading in the files wrong. hmmm maybe it isnt the reading in that wrong. was just making sure that my actionlistener that did the reading looked right . because i cant tell if its working lol.

  4. #4
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: java GUI , reading in files

    here my canvas class , that does all the drawing and stuff. maybe thats wrong. im supposed to draw out the statistical info ( like min/max) onto my canvas. i called the model class and called the methods i used for the data and drew it onto the canvas with drawstring.
    as of write now it doesnt print out right. it doesnt printout anything that has to do with the pot4.asc file. just some nulls. this is why i thought the readInFile listener was broken, because its not getting info from the pot4.asc file
    Java Code:
    
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class MyCanvas extends JPanel {
    
    
    	Model m = new Model ();
    	double area = m.computeSurfaceArea();
    	double nTriangle = m.triangleArray.size();
    	double min = m.computeMin();
    	double max= m.computeMax();
    	double averageArea =area /nTriangle;
    	String totalArea = area+"";
    	String numTri = nTriangle+"";
    	String mini= min+"";
    	String maxi = max+"";
    	String avArea = averageArea+"";
    
    	private static final long serialVersionUID = -5625058787715414029L;
    	int width;
    	int height;
    
    	MyCanvas(int width, int height) {
    		super();
    		this.width = width; this.height = height;
    		setSize(width, height);
    	}
    
    	public MyCanvas() {
    		// TODO Auto-generated constructor stub
    	}
    
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		g.setColor(Color.BLACK);
    		g.drawRect(10, 10, width-1, height-1);
    
    		g.setColor(Color.ORANGE);
    		g.fillRect(20, 20, 30, 30);
    
    		g.setColor(Color.RED);
    		g.fillOval(100, 100, 20, 20);
    
    		g.drawString(totalArea, 150, 200);
    		g.drawString(numTri, 150, 220);
    		g.drawString(mini, 150, 240);
    		g.drawString(maxi, 150, 260);
    		g.drawString(avArea, 150, 280);
    
          
    
    	}
    
    }

Similar Threads

  1. Question about fast reading files in Java.
    By Martino in forum Suggestions & Feedback
    Replies: 0
    Last Post: 05-09-2011, 03:34 PM
  2. Reading files JAVA
    By dienesh77 in forum New To Java
    Replies: 14
    Last Post: 04-06-2011, 05:22 AM
  3. Reading .dat files in java
    By anilreddy76 in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2010, 01:00 PM
  4. java api for reading adobe frame maker files
    By venkatmatcha in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2008, 06:00 AM
  5. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •