All pastes #253041 Raw Edit

Unnamed

public text v1 · immutable
#253041 ·published 2006-11-20 07:10 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());
				openFile(chooser.getSelectedFile());
				repaint();
			}
			
			
		}
	}
	
	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.exit(0);
		}
		
		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(0); j++)
			{
				eng.setCell(i, j, stringToBoolean(tmp[j]));
				System.out.println (i + " " + 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;
		}
		
	}

}

Upon running this code to output, I get a 100x100 grid (0-99 x 0-99), which is correct.

However, when trying to take that back in as input, it processes up through row #98, column 99, and then throws this exception:
98 99 <--- this being the output generated by nested for loops to show which cell the loop is handling.
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Scanner.java:1471)
        at GridPanel.openFile(GridPanel.java:252)
        at GridPanel.access$700(GridPanel.java:8)
        at GridPanel$ComboListener.actionPerformed(GridPanel.java:196)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1197)
        at javax.swing.JComboBox.setSelectedItem(JComboBox.java:561)
        at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:597)
        at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:808)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:232)
        at java.awt.Component.processMouseEvent(Component.java:5554)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
        at apple.laf.AquaComboBoxUI$AquaComboPopup$1.processMouseEvent(AquaComboBoxUI.java:443)
        at java.awt.Component.processEvent(Component.java:5319)
        at java.awt.Container.processEvent(Container.java:2010)
        at java.awt.Component.dispatchEventImpl(Component.java:4021)
        at java.awt.Container.dispatchEventImpl(Container.java:2068)
        at java.awt.Component.dispatchEvent(Component.java:3869)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
        at java.awt.Container.dispatchEventImpl(Container.java:2054)
        at java.awt.Window.dispatchEventImpl(Window.java:1774)
        at java.awt.Component.dispatchEvent(Component.java:3869)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)