All pastes #251507 Raw Edit

Stuff

public java v1 · immutable
#251507 ·published 2006-11-18 08:05 UTC
rendered paste body
package sudoku;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.EventListener;import javax.swing.*;/** * The SudokuPanel class currently draws the Sudoku Board and allows for text entry. New and quit work as advertised. *  * @author Kyle Wilson * @version 15 November 2006 *  * code for menu bar adapted from Scribble Program example by Xiaoping Jia Copyright 2002 *  */public class SudokuPanel extends JPanel{	/**	 * Constructs a SudokuPanel object and initializes a SudokuPanelListener to act as a MouseListener	 *	 */  	public SudokuPanel() {		super();		sBoard = new int[9][9];		setLayout(new GridLayout(9,9));		sudokuBoard = new SudokuTextField[9][9];		Font font = new Font("Monospace", Font.BOLD, 24);		SudokuPanelListener SPListener = new SudokuPanelListener(this);		for(int i=0; i<sudokuBoard.length; i++) {			for(int j=0; j<sudokuBoard[i].length; j++) {				sudokuBoard[i][j] = new SudokuTextField(j,i);				sudokuBoard[i][j].setFont(font);				sudokuBoard[i][j].addActionListener(SPListener);				add(sudokuBoard[i][j]);			}		}	}			  	      /**	       * Clears the current Sudoku Puzzle and leave an empty board.	       *	       */		  public void newFile() { 		    for(int i = 0; i < 9; i++)				for(int j = 0; j < 9; j++)				{ sudokuBoard[i][j].setText("");}		    		    repaint();		  }		  /**		   * Opens a saved Sudoku puzzle.		   * @param filename - The name of the file to open.		   */		  public void openFile(String filename) {		    try {		      ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename)); 		      sudokuBoard = (SudokuTextField[][]) in.readObject(); 		      in.close(); 		      repaint();		    } catch (IOException e1) {		      System.out.println("Unable to open file: " + filename); 		    } catch (ClassNotFoundException e2) {		      System.out.println(e2); 		    }		  }		  		  /**		   * Saves a sudoku puzzle currently in progress.		   * @param filename - the name to save the sudoku puzzle under.		   */		  public void saveFile(String filename) { 		    try {		      ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename)); 		      out.writeObject(sudokuBoard); 		      out.close(); 		      System.out.println("Save Sudoku to " + filename);		    } catch (IOException e) {		      System.out.println("Unable to write file: " + filename); 		    }  		  }		  		  		  		  			class SudokuTextField extends JTextField {				int x,y;				String value;				boolean duplicate;				public SudokuTextField(int x, int y) {					this.x = x;					this.y = y;					duplicate = false;					setOpaque(false);				}								  public void checkDuplicate(SudokuTextField test)				  {					  int testint;					  try					  {						  testint = Integer.parseInt(test.paramString());						  sBoard[test.x][test.y] = testint;						  if(testint > 9 || testint < 1)						  {							  JOptionPane.showMessageDialog(null, "Not A Valid Entry\nSudoku Numbers must be between 1 and 9", "Alert", JOptionPane.ERROR_MESSAGE);							  return;						  }						  for(int i = 0; i < 9; i++)						  {							  sudokuBoard[test.x][i].duplicate = false;							  sudokuBoard[i][test.y].duplicate = false;							  for(int j = 0; j < 9; j++)							  {								  if(i != j && sBoard[test.x][i] == sBoard[test.x][j])								  {									  sudokuBoard[test.x][i].duplicate = true;									  sudokuBoard[test.x][j].duplicate = true;								  }								  if(i != j && sBoard[i][test.y] == sBoard[j][test.y])								  {									  sudokuBoard[i][test.y].duplicate = true;									  sudokuBoard[j][test.y].duplicate = true;								  }							  }						  }						  int myx = test.x - (test.x % 3);						  int myy = test.y - (test.y % 3);						  for(int i=myx; i<myx+3; i++) 						  {								for(int j=myy; j<myy+3; j++) 								{									sudokuBoard[i][j].duplicate = false;									for(int i2=myx; i2<myx+3; i2++)									{										for(int j2=myy; j2<myx+3; j2++)										{											if(i != i2 && j != j2 && sBoard[i][j] == sBoard[i2][j2])											{												sudokuBoard[i][j].duplicate = true;												sudokuBoard[i2][j2].duplicate = true;											}										}									}								}							}							  					  }					  catch(NumberFormatException e)					  {						  if(test.paramString().equals(""))							  return;						  JOptionPane.showMessageDialog(null, "Not A Valid Number", "Alert", JOptionPane.ERROR_MESSAGE);					  }				  }				public void update(Graphics g) {					paint(g);				}				public void paint(Graphics g) {					g.setColor(Color.WHITE);					g.fillRect(0,0,getSize().width,getSize().height);					if(duplicate){						g.setColor(Color.RED);						g.drawLine(0,0,getSize().width-1,0);						g.drawLine(0,0,0,getSize().height-1);					}					else						g.setColor(Color.BLACK);					g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height);								g.drawLine(0,getSize().height-1,getSize().width,getSize().height-1);										if(x == 3 || x == 6) {						g.drawLine(0,0,0,getSize().width);					}					if(y == 3 || y == 6) {						g.drawLine(0,0,getSize().width,0);					}					super.paintComponent(g);				}			}		  		  protected EventListener listener;		  protected boolean mouseButtonDown = false; 		  protected int x, y;		  protected SudokuTextField[][] sudokuBoard;		  protected int[][] sBoard;}