All pastes #253028 Raw Edit

Something

public java v1 · immutable
#253028 ·published 2006-11-20 06:32 UTC
rendered paste body
import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;import java.io.*;import java.util.Scanner;public class GridPanel extends JPanel{	double x1, y1;	final int gridSize = 100;	final int draw = 0;	final int fill = 1;	final int pad = 40;	int DELAY = 1;	Engine eng;	JButton next, start, stop, clear;	JTextField run;	private JFileChooser chooser;	private Scanner scan;		int iterations;	private Timer timer;	int counter = 0;	private JComboBox combobox;	static final long serialVersionUID = 01;	public GridPanel()	{		eng = new Engine(gridSize, gridSize);			next = new JButton("Next");		next.addActionListener(new GridPanel.ButtonListener());		start = new JButton("Start");		start.addActionListener (new GridPanel.ButtonListener());		stop = new JButton("Stop");		stop.addActionListener (new GridPanel.ButtonListener());		clear = new JButton("Clear");		clear.addActionListener (new GridPanel.ButtonListener());		run = new JTextField("Run Count");				add(next);		add(start);		add(stop);		add(run);		add(clear);				timer = new Timer(DELAY, new LoopListener());				// File Operation Combo Box				String[] options = {"File...","Save","Open","Quit"};		combobox = new JComboBox (options);		combobox.addActionListener((new ComboListener()));		add(combobox);					}		public void paintComponent (Graphics a)	{		super.paintComponent(a);			Graphics2D d2 = (Graphics2D)a;			d2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);				double w = getWidth();		double h = getHeight();				x1 = (w - 2 * pad) / gridSize;		y1 = (h - 2 * pad) / gridSize;				// Draw Rows		double x2 = pad, y2 = pad, x3 = w - pad, y3 = h - pad;		for (int m = 0; m <= gridSize; m++)		{			d2.draw(new Line2D.Double (x2, y2, x3, y2));			y2 += y1;		}					// Draw Columns		y2 = pad;		for (int m = 0; m <= gridSize; m++)		{			d2.draw(new Line2D.Double(x2,y2,x2,y3));			x2 += x1;		}					// Fill Cells when Clicked		d2.setPaint(Color.blue);		for (int row = 0; row < eng.returnRowCount(); row++)		{			for (int col = 0; col < eng.returnColumnCount(row); col++)			{				if (eng.getCell(row, col) == true)				{					x2 = pad + col * x1 + 1;					y2 = pad + row * y1 + 1;					d2.fill(new Rectangle2D.Double(x2, y2, x1-1, y1-1));				}			}		}	}			public void changeCellColor(int row, int column)	{		if (eng.getCell(row, column) == false)			eng.setCell(row, column, true);			else 				eng.setCell(row, column, false);		//System.out.println(row + " " + column);		repaint();	}		private class ButtonListener implements ActionListener	{		public void actionPerformed (ActionEvent event)		{			Object source = event.getSource();			if (source == next)			{				eng.nextStep();				repaint();			}						if (source == start)			{				timer.start();							}						if (source == stop)			{				timer.stop();				counter = 0;			}						if (source == clear)			{				for (int i=0; i<eng.returnRowCount(); i++)				{					for (int j=0; j < eng.returnColumnCount(i); j++)					{						eng.setCell(i, j, false);					}				}				repaint();			}		}	}		private class LoopListener implements ActionListener	{		public void actionPerformed (ActionEvent event)		{			eng.nextStep();			repaint();			counter++;			if (counter == Integer.parseInt(run.getText()))			{				timer.stop();				counter = 0;			}			System.out.println(counter);			}	}		private void runLoop(int loop)	{		int count = 0;		while (count <= loop)		{			eng.nextStep();			repaint();		}	}		private class ComboListener implements ActionListener	{		public void actionPerformed (ActionEvent event)		{			if (combobox.getSelectedIndex() == 0) {}			if (combobox.getSelectedIndex() == 1)			{				chooser = new JFileChooser();				chooser.showSaveDialog(null);				saveFile(chooser.getSelectedFile());			}			if (combobox.getSelectedIndex() == 2)			{				chooser = new JFileChooser();				chooser.showOpenDialog(null);				System.out.println(chooser.getSelectedFile());				File chosenfile = chooser.getSelectedFile();				openFile(chooser.getSelectedFile());			}						try{			}			catch (IOException e)			{ }					}	}		private void saveFile(File file) throws IOException	{		FileWriter fw = null;						try		{			fw = new FileWriter(file);		}				catch (IOException e)		{			System.out.println("Usage : java FileIOdemo <filename> " );			System.exit(0);		}				BufferedWriter bw = new BufferedWriter(fw);		PrintWriter outFile = new PrintWriter(bw);		for (int i = 0 ; i < eng.returnRowCount() ; i++)		{			for (int j = 0 ; j < eng.returnColumnCount(i) ; j++)				outFile.print(booleanToString(eng.getCell(i, j)) + " ") ;			outFile.println();		}				outFile.close();	}		private void openFile(File file) throws IOException	{				try		{			scan = new Scanner(file);		}		catch (IOException e)		{			System.out.println("PUKE!");		}				String s1;				s1 = scan.nextLine();		String tmp[] = s1.split(" ");				for (int i=0; i < eng.returnRowCount(); i++)		{			s1 = scan.nextLine();			tmp = s1.split(" ");			for (int j=0; j<eng.returnColumnCount(i); j++)			{				eng.setCell(i, j, stringToBoolean(tmp[j]));			}		}	}		public static final boolean stringToBoolean(String str) 	{	        if (str.equals("0")) {	            return false;	        }	        //if it's non false, it's true by definition	        else 	        	return true;	}		public static final int booleanToString(boolean bool)	{		if (bool)		{			int a = 1;			return a;		}		else 		{			int a = 0;			return a;		}			}}-- Compiler Error --dynamic-addr-88-162:~/Xcode/CSC171/Project 3 Cody$ javac *.javaGridPanel.java:202: exception java.io.IOException is never thrown in body of corresponding try statement                        catch (IOException e)                        ^GridPanel.java:189: unreported exception java.io.IOException; must be caught or declared to be thrown                                saveFile(chooser.getSelectedFile());                                        ^GridPanel.java:197: unreported exception java.io.IOException; must be caught or declared to be thrown                                openFile(chooser.getSelectedFile());                                        ^3 errorsdynamic-addr-88-162:~/Xcode/CSC171/Project 3 Cody$