Commits

Jo Traves committed e69eefb

First commit of simple GUI with load file functionality

  • Participants
  • Parent commits d72c0d2

Comments (0)

Files changed (1)

ca.usask.cs/giraffe/gui/StaticGUI.java

+package giraffe.gui;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.*;
+
+import java.io.*;
+
+
+public class StaticGUI {
+	
+	JPanel programdisplay = new JPanel();
+	int rows = 37;
+	int columns = 70;
+	JTextArea text = new JTextArea(rows, columns);
+	File loadfile;
+	JFrame chooserframe = new JFrame();
+	
+	public void mainWindow(){
+		JFrame mainFrame = new JFrame();
+		                          // width,height
+		Dimension dim = new Dimension(1300, 800);
+		mainFrame.setSize(dim);
+		
+		BorderLayout layout = new BorderLayout();
+		mainFrame.setLayout(layout);
+		
+		mainFrame.add(createToolBar(), BorderLayout.NORTH);
+		mainFrame.add(programDisplayPanel(), BorderLayout.WEST);
+		mainFrame.add(animationDisplayPanel(), BorderLayout.EAST);
+		
+		mainFrame.setVisible(true);
+		
+		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+		
+	}
+	
+	
+	
+	
+	
+	
+	public String filetostring(File load){
+
+		try {
+			FileInputStream fstream = new FileInputStream(load);
+			StringBuffer stringfile = new StringBuffer();
+			int input = fstream.read();
+			while(input != -1){
+			 stringfile = stringfile.append((char)input); 
+			 input = fstream.read();
+		    }
+			fstream.close();
+			return stringfile.toString();
+		} catch (IOException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+			return "File Not Found";
+		}
+	}
+	
+	public  void chooseFile(){
+		JFileChooser chooser = new JFileChooser();
+		
+		
+		chooserframe.add(chooser);
+		
+		class AddChooserListener implements ActionListener{
+			
+			public void actionPerformed(ActionEvent event){
+				JFileChooser theFileChooser = (JFileChooser) event.getSource();
+		        String command = event.getActionCommand();
+		        if (command.equals(JFileChooser.APPROVE_SELECTION)) {
+		        	loadfile = theFileChooser.getSelectedFile();
+				      System.out.println(loadfile);
+				      text.setText(filetostring(loadfile));
+				      chooserframe.setVisible(false);
+		        } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
+		             System.out.println(JFileChooser.CANCEL_SELECTION);
+		             chooserframe.setVisible(false);
+		             
+		        }
+			}
+			
+		}
+		ActionListener listener = new AddChooserListener();
+		chooser.addActionListener(listener);
+		
+		Dimension dim = new Dimension(500, 300);
+		chooserframe.setSize(dim);
+		chooserframe.setVisible(true);
+	}
+	
+	
+	public JToolBar createToolBar(){
+		JToolBar toolBar = new JToolBar();
+		toolBar.setFloatable(false);
+		JButton fileButton = new JButton(" Load File ");
+		toolBar.add(fileButton);
+		
+		
+		class AddFileListener implements ActionListener
+		{
+			
+			public void actionPerformed(ActionEvent event){
+					chooseFile();
+			}
+			
+		}
+		
+		ActionListener listener = new AddFileListener();
+		fileButton.addActionListener(listener);
+		return toolBar;
+		
+	}
+	
+	public JPanel programDisplayPanel(){
+		
+	
+		Dimension dimPD = new Dimension(60, 70);
+		programdisplay.setSize(dimPD);
+		
+		
+		Insets insets = new Insets(25,25,25,25);
+		text.setMargin(insets);
+		
+		text.setEditable(false);
+		
+		JScrollPane scrollProgramDisplay = new JScrollPane(text);
+		scrollProgramDisplay.add(scrollProgramDisplay.createHorizontalScrollBar());
+		
+		
+		programdisplay.add(scrollProgramDisplay);
+		
+		return programdisplay;
+		
+	}
+	
+	
+	public JPanel animationDisplayPanel(){
+		JPanel animationdisplay = new JPanel();
+		Dimension dimPD = new Dimension(60, 70);
+		animationdisplay.setSize(dimPD);
+		
+		JScrollPane scrollAnimationDisplay = new JScrollPane();
+		
+		animationdisplay.add(scrollAnimationDisplay);
+		
+		return animationdisplay;
+		
+	}
+	
+	
+	
+	
+	
+	
+	
+	public static void main(String[] args){
+		try {
+            // Set System L&F
+        UIManager.setLookAndFeel(
+            UIManager.getSystemLookAndFeelClassName());
+    } 
+    catch (UnsupportedLookAndFeelException e) {
+       // handle exception
+    }
+    catch (ClassNotFoundException e) {
+       // handle exception
+    }
+    catch (InstantiationException e) {
+       // handle exception
+    }
+    catch (IllegalAccessException e) {
+       // handle exception
+    }
+
+		
+		
+	StaticGUI gui = new StaticGUI();
+	gui.mainWindow();
+	return;
+		
+	}
+
+}