All pastes #986393 Raw Edit

board

public text v1 · immutable
·published 2008-04-15 19:44 UTC
rendered paste body
import java.awt.*; import javax.swing.*; import java.awt.event.*;

public class Board extends JFrame implements MouseListener {
	JButton b;
	JMenuBar menuBar = new JMenuBar();
	JMenu menu = new JMenu("The Game");
	JFrame frame = new JFrame("Most Awesome");
	CellPane[][] board;
	private int boardSize ;
	private Color color = null;
	private Container cont = getContentPane();
	boolean[][] cArray = new boolean[boardSize][boardSize];
	CellPane cell;
	boolean bool;
	public Board(int size, Color c){
		boardSize = size;
		color = c; 
	}
	
	public void drawBoard(){
		menu.setVisible(true);
		menu.setName("life");
		menuBar.add(menu);
		menuBar.setName("woo");
		frame.setJMenuBar(menuBar);
		board = new CellPane[boardSize][boardSize];
		cont.setLayout(new GridLayout(boardSize, boardSize, 1, 1));
		cont.setForeground(Color.black);
		cont.setBackground(Color.black);
		cont.addMouseListener(this);
		cont.setName("Game of Life");
		
		for(int i = 0; i  < boardSize; i++){
			for (int j = 0; j < boardSize; j ++){
				int [] a ={i, j};
				board[i][j] = new CellPane(a);
				board[i][j].setBackground(color);
				cont.add(board[i][j]);
			}
		}
//		frame.add(cont);
		
//		button.addMouseListener(this);
	
	}
	
	public void nextState(){
		for (int i = 0; i < boardSize; i ++){
			for (int j = 0; j < boardSize; j ++){
				if(board[i][j].getNeighborhood() < 2){
					board[i][j].killCell();
				}
				else if(board[i][j].getNeighborhood() < 4 && (board[i][j].getLife() == false)){
					board[i][j].setColor();
				}
				else{
					board[i][j].killCell();
				}
			}
		}
	}
	
	public void startEvent(){
		while(true){
			evalBoard();
			nextState();
		}
		/*get start from buttonlistener
		 * while (true)
		 * nextState;
		 * maybe wait between updates if to fast
		 */
	}
	//evaluates all cells int the board at any iteration
	public void evalBoard(){
		for(int i = 0; i < boardSize; i++){
			for(int j = 0; j < boardSize; j++){
				int count = 0;
				int neighborhood = 0;
					if(checkWest(i, j)){
						neighborhood ++;
					}
					if(checkEast(i, j)){
						neighborhood ++;
					}
					if(checkNorth(i, j)){
						neighborhood ++;
					}
					if(checkSouth(i, j)){
						neighborhood ++;
					}
					board[i][j].setNeighborhood(neighborhood);
			}
		}
	}
	
	public boolean checkWest(int i, int j){
		try{
			System.out.print(i);
			bool = board[i % boardSize][Math.abs(j - 1) % boardSize].getLife();
			return bool;
		}
		catch(Exception e){
			bool =board[i][j + boardSize - 1].getLife();
			return bool;
		}
	}
	
	public boolean checkEast(int i, int j){
		try{ 
			bool =  board[i][Math.abs(j + 1)%boardSize].getLife();
			return bool;
		}
		catch(Exception e){
			bool = board[i][Math.abs(j - boardSize + 1) % boardSize].getLife();
			return bool;
		}
	}
	
	public boolean checkNorth(int i, int j){
		try{
			bool = board[Math.abs(i - 1)%boardSize][j].getLife();
			return bool;
		}
		catch(Exception e){
			bool =board[Math.abs(i + boardSize - 1)%boardSize][j].getLife();
			return bool;
		}
	}
	
	public boolean checkSouth(int i, int j){
		try{
			bool = board[(i+1)%boardSize][j].getLife();
			return bool;
		}
		catch(Exception e){
			bool = board[Math.abs(i - boardSize + 1)%boardSize][j].getLife();
			return bool;
		}
	}
	
	public boolean checkNortWest(int i, int j){
		try{
			return board[(i - 1)% boardSize][(j - 1) % boardSize].getLife();
		}
		catch(Exception e){
			return board[i% boardSize][(j + boardSize - 1) % boardSize].getLife();
		}
	}
	
	public boolean checkSouthWest(int i, int j){
		try{
			return board[(i + 1)% boardSize][(j - 1)% boardSize].getLife();
		}
		catch(Exception e){
			return board[i% boardSize][(j + boardSize - 1)% boardSize].getLife();
		}
	}
	public boolean checkNorthEast(int i, int j){
		try{
			return board[(i - 1)% boardSize][(j + 1)% boardSize].getLife();
		}
		catch(Exception e){
			return board[i% boardSize][(j - boardSize + 1)% boardSize].getLife();
		}
	}
	public boolean checkSouthEast(int i, int j){
		try{
			return board[(i + 1)% boardSize][(j + 1)% boardSize].getLife();
		}
		catch(Exception e){
			return board[i% boardSize][(j - boardSize + 1)% boardSize].getLife();
		}
	}
	
	//MouseEvents implemented from interface
	public void mouseEntered(MouseEvent e){
	}
	public void mousePressed(MouseEvent e){
	}
	public void mouseReleased(MouseEvent e){
	}
	public void mouseExited(MouseEvent e){
	}
	
	//MouseEvent that switches cell form life to unlife
	public void mouseClicked(MouseEvent e){
		try{
		Point p = e.getPoint();
		cell = (CellPane) cont.getComponentAt(p);
			cell.setColor(); // sets color and active state
			
		}	
		catch(Exception a){
			startEvent();
		}
	}
	
	
	
	
}