Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Need help soon with correcting code!: Reading file to JPanel

    I have been working on this project and was very sure it'd work; everything compiles and all, and when I go to run main, the window opens with the JButtons and everything, and the JPanel for DrawingPanel shows up (I colored it red to make sure it showed up, then removed it as it wasn't needed anymore than just to test), and when I pick a file, it will say "Displaying file: [file name here]" as it should, but the file won't show up. The file is a simple txt file in this format:

    line,10,10,110,110
    line,110,10,10,110

    and it is supossed to be read in and drawn on DrawingPanel, but nothing shows up. Could someone please help me in anyway? I can't figure out the problem, but if anyone spots at least something, it may help me figure it out. I need to turn this in by tonight at 11:55pm, so ideally the sooner I can get help, the better :\

    Project04Driver
    Code:
    public class Project04Driver {
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
            	public void run() {
            		Project04Window newGUI = new Project04Window();
            	}
            });
        }
    }

    Project04Window
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    public class Project04Window implements ActionListener {
    	private JLabel lblOutput;
    	private JButton btnChooseFile, btnExit;
    	private File theFile;
    
        public Project04Window() {
        	JFrame frame = new JFrame("Event Tester GUI");
        	frame.setPreferredSize(new Dimension(600, 400));
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        	JPanel mainPanel = new JPanel();
        	mainPanel.setPreferredSize(new Dimension(600, 400));
    
      		JPanel panel1 = new JPanel();
      		panel1.setPreferredSize(new Dimension(400, 50));
      		btnChooseFile = new JButton("Choose File");
      		btnChooseFile.addActionListener(this);
      		panel1.add(btnChooseFile);
    
      		btnExit = new JButton("Exit");
      		btnExit.addActionListener(this);
      		panel1.add(btnExit);
      		mainPanel.add(panel1);
    
    
    		JPanel panel2 = new JPanel();
    		panel2.setPreferredSize(new Dimension(400, 50));
      		lblOutput = new JLabel("Displaying file: none");
      		lblOutput.setVisible(true);
      		panel2.add(lblOutput);
      		mainPanel.add(panel2);
    
    
      		DrawingPanel contentPanel = new DrawingPanel(theFile);
      		contentPanel.setPreferredSize(new Dimension(550, 230));
      		mainPanel.add(contentPanel);
    
    
      		frame.getContentPane().add(mainPanel);
      		frame.pack();
      		frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
        	Object source = e.getSource();
    
        	if(source.equals(btnChooseFile)){
        		JFileChooser chooser = new JFileChooser();
        		int returnVal = chooser.showOpenDialog(null);
    
        		if(returnVal == JFileChooser.APPROVE_OPTION) {
        			lblOutput.setText("Displaying file: " + chooser.getSelectedFile().getName());
        			theFile = chooser.getSelectedFile();
        		}
        	}
        	else {
        		System.exit(0);
        	}
        }
    }

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    public class DrawingPanel extends JPanel {
    	private ArrayList<String> fileData;
    
        public DrawingPanel(File file) {
        	fileData = new ArrayList<String>();
        	if(file != null) {
        	try{
        		Scanner inFile = new Scanner(file);
    
    			String line;
        		while(inFile.hasNextLine()) {
        			line = inFile.nextLine();
    				fileData.add(line);
        		}
        		inFile.close();
    
        	}
        	catch(FileNotFoundException fnfe) {
        		System.out.println("Unable to open the file");
        	}
        	catch(IOException ioe) {
        		System.out.println("A problem was encountered when reading the file");
        	}
        	}
        	else {
        		System.out.println("Choose a file to show here");
        	}
        }
    
        public void paintComponent(Graphics g){
        	super.paintComponent(g);
    
        	/**g.drawLine(10, 10, 110, 110);**/
    
        	int counter = 0;
        		for(String txt : fileData) {
        		    String[] text = txt.split(",");
    				String shape = text[0];
    				int v1 = Integer.parseInt(text[1]);
    				int v2 = Integer.parseInt(text[2]);
    				int v3 = Integer.parseInt(text[3]);
    				int v4 = Integer.parseInt(text[4]);
    
    				if(shape.equals("line")) {
    					g.drawLine(v1, v2, v3, v4);
    					g.setColor(Color.BLACK);
    				}
    				else {
    					g.drawLine(10,10,110,110);
    					g.setColor(Color.BLACK);
    				}
        		}
    
        }
    
    
    }


    EDIT: Nevermind, I figured it out
    Last edited by Airin; 11-20-2010 at 11:59 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
  •