package ce.ass2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import ce.board.Board;
import ce.board.MoveData;
import java.awt.Color;
import javax.swing.JButton;
public class TickerBean extends JButton implements java.io.Serializable{
int timeDelay=500;
private LifeBean lb=new LifeBean();
private Board tickerBoard;
private javax.swing.Timer t = new javax.swing.Timer(timeDelay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tickerBoard=lb.getBoard(); // <==========>
System.out.println("tickerBoard is " + tickerBoard);
//lb.setBoard(boardRef);
if (tickerBoard!=null) {
lb.step();
}
else
System.out.println("ticker is null");
} catch (Exception ex) {
System.out.println("ticker bean step failed!");
System.out.println(ex);
}
}
});
public TickerBean() {
super("TickerBean"); // Setting the bean's label
setEnabled(false); // Can't be clicked
setBorderPainted(false); // Make it look less like a button
}
public void start() {
t.start();
}
public void stop() {
t.stop();
}
public void setBoard(Board board) {
tickerBoard=board;
}
}
package ce.ass2;
import ce.board.Board;
import ce.board.MoveData;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
public class LifeBean extends JButton implements Cloneable,Serializable
{
private Board boardRef;
private int boardSize = 21; // 21x21 cells
private int cellSize = 16; // 16x16 pixels
private boolean gameInited = false;
private Color[][] clearBoardColours; // Colours for a clear board
private Color[][] currentBoardColours = new Color[boardSize][boardSize];
private Color[][] tempBoardColours = new Color[boardSize][boardSize];
private int counter=0;
private final PropertyChangeSupport property = new PropertyChangeSupport( this );
/**
* No-argument constructor. Every bean must have a no-argument constructor.
*/
public LifeBean() {
super("LifeBean"); // Setting the bean's label
setEnabled(false); // Can't be clicked
setBorderPainted(false); // Make it look less like a button
}
/** Set the board bean with which this LifeBean bean is interacting.
* This method initialises the board, calling its setBoard() method.
*
* @param boardRef the board
*/
public void setBoard(Board boardRef) {
this.boardRef = boardRef;
clearBoardColours = new Color[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
clearBoardColours[i][j] = Color.white;
currentBoardColours[i][j]=Color.white;
}
}
System.out.append("==== >> reset");
boardRef.initBoard(boardSize, cellSize, clearBoardColours);
gameInited = true;
System.out.println("board set" + boardRef + gameInited);
}
/** Return a reference to the board bean.
*
*/
public Board getBoard() {
return this.boardRef;
}
/** Process one move. Called as a result of the player making a move on the
* board. This method first determines whether the move is valid. If the move
* is invalid, it calls the board's update() method indicating an invalid
* move. If the move is valid, the update() call indicates a valid move and
* sends one or more updates to the board for display.
*
*
* @param md the move
*/
public void move(MoveData md) throws Exception {
if (!gameInited) {
return;
}
boolean valid = valid(md);
if (!valid) {
boardRef.invalidMove();
} else {
// System.out.println("@@@@@@@@@ update tempboard @@@@@@@@@@@ ");
boardRef.update(md.sourceRow, md.sourceCol, Color.gray);
currentBoardColours[md.sourceRow][md.sourceCol]= Color.gray;
//tempBoardColours[md.sourceRow][md.sourceCol]= Color.gray;
// For this example, just colour the square
}
tempBoardColours=(Color[][]) ObjectCloner.deepCopy(currentBoardColours);
}
/** Step board one cycle according to the rules of the game */
public void step() throws Exception {
if (!gameInited) {
return;
} else {
/* Apply the rules of the game and update the board. You can update the
* board using a series of calls to updateBoard() or a call to initBoard().
*/
for (int r=0 ; r<boardSize ; r++) {
for (int c=0 ; c<boardSize ; c++) {
if (currentBoardColours[r][c]==Color.gray) {
[c]==Color.gray);
if (isSurvive(r,c)) { // remain alive
tempBoardColours[r][c]=Color.gray;
} else { // is dead
tempBoardColours[r][c]=Color.white;
currentBoardColours[r][c]=Color.gray;
}
}
else {
if (isBorn(r,c)) {
tempBoardColours[r][c]=Color.gray;
}
}
}
}
clear();
for (int r=0 ; r<boardSize ; r++) {
for (int c=0 ; c<boardSize ; c++) {
if (tempBoardColours[r][c]==Color.gray) {
currentBoardColours[r][c]=Color.gray;
// System.out.println("r=" + r + " c=" + c);
}
}
}
boardRef.initBoard(boardSize, cellSize, tempBoardColours);
// System.out.println("######### update currentboard ########3 ");
}
}
private boolean isBorn(int r, int c) {
counter =0;
check(r-1,c-1);
check(r-1,c);
check(r-1,c+1);
check(r,c-1);
check(r,c+1);
check(r+1,c-1);
check(r+1,c);
check(r+1,c+1);
// System.out.println(r + " " + c + "is born ==> " + (counter==3) + counter );
return (counter==3);
// dead cell has 3 neighburing cells alive, born new cell
}
private boolean isSurvive(int r, int c) {
counter =0;
check(r-1,c-1);
check(r-1,c);
check(r-1,c+1);
check(r,c-1);
check(r,c+1);
check(r+1,c-1);
check(r+1,c);
check(r+1,c+1);
// System.out.println(r + " " + c + "is surve ==> "
// + (counter==3 || counter ==2) + counter );
return counter==3 || counter ==2;
// alive cell has 2 or 3 neighbouring cells alive, surive
}
private void check(int row, int col) {
if ((row >= 0 && col >= 0) && (row < boardSize && col < boardSize)) {
if(currentBoardColours[row][col]==Color.gray ) {
counter++;
}
}
}
/** Determine whether the proposed move is valid.
*/
private boolean valid(MoveData md) {
return true; // For Life, no move (click) on the board is invalid
}
/**
* Clearing the color from the cells which on the board.
*/
public void clear() {
System.out.println("clear()" + boardRef);
setBoard(boardRef);
/*for (int i=0;i<boardSize;i++) {
for (int j=0;j<boardSize;j++) {
clearBoardColours[i][j] = Color.white;
}
} */
}
public void Save() {
try
{
ObjectOutputStream outputStream =
new ObjectOutputStream(new FileOutputStream("arrayfile"));
Color[][] boardColours=new Color[boardSize][boardSize];
for (int r=0 ; r<boardSize ; r++) {
for (int c=0 ; c<boardSize ; c++) {
if (currentBoardColours[r][c]==Color.gray) {
boardColours[r][c]=Color.blue;
System.out.println(" board r=" + r + " c=" + c);
}
}
}
outputStream.writeObject(boardColours);
System.out.println("start printing");
outputStream.close( );
}
catch(IOException e)
{
System.out.println("Error writing to file.");
System.exit(0);
}
System.out.println(
"Array written to file arrayfile.");
}
public void load() throws Exception {
try
{
ObjectInputStream inputStream =
new ObjectInputStream(new FileInputStream("arrayfile"));
Color[][] boardColours =
(Color[][])inputStream.readObject( );
for (int r=0 ; r<boardSize ; r++) {
for (int c=0 ; c<boardSize ; c++) {
if (boardColours[r][c]!=null) {
currentBoardColours[r][c]=Color.gray;
System.out.println("current board r=" + r + " c=" + c);
}
}
}
boardRef.initBoard(boardSize, cellSize, currentBoardColours);
inputStream.close( );
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find file arrayfile.");
System.exit(0);
}
catch(ClassNotFoundException e)
{
System.out.println("Problems with file input.");
System.exit(0);
}
catch(IOException e)
{
System.out.println("Problems with file input.");
System.exit(0);
}
}
public int getBoardSize() {
return boardSize;
}
} // LifeBean
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* LifeGameJFrame.java
*
* Created on May 5, 2009, 4:48:31 PM
*/
public class LifeGameJFrame extends javax.swing.JFrame {
/** Creates new form LifeGameJFrame */
public LifeGameJFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
board1 = new ce.board.Board();
lifeBean1 = new ce.ass2.LifeBean();
tickerBean1 = new ce.ass2.TickerBean();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
try {
board1.addMoveListener(new ce.board.MoveListener() {
public void move(ce.board.MoveEvent evt) {
board1Move(evt);
}
});
} catch (java.util.TooManyListenersException e1) {
e1.printStackTrace();
}
lifeBean1.setBoard(board1);
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Step");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(47, 47, 47)
.addComponent(board1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(206, 206, 206)
.addComponent(lifeBean1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(69, 69, 69)
.addComponent(tickerBean1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(39, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(board1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(85, 85, 85)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lifeBean1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tickerBean1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)))
.addContainerGap(21, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void board1Move(ce.board.MoveEvent evt) {
try {
lifeBean1.move(board1.getMove());
} catch (java.lang.Exception e1) {
e1.printStackTrace();
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
tickerBean1.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
tickerBean1.stop();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try {
lifeBean1.step();
} catch (java.lang.Exception e1) {
e1.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LifeGameJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private ce.board.Board board1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private ce.ass2.LifeBean lifeBean1;
private ce.ass2.TickerBean tickerBean1;
// End of variables declaration
}
LifeGameJFrame is a container which link these beans to build an application
LifeBean is a implement the rule of life in this game
TickerBean is handling start and stop from the game with 500 miliseconds
problem:
***********************
tickerBoard=lb.getBoard(); // <==========>
***********************
my problem is why these line return me null ? can somebody explain to me ? i have tried used print out method to figure it out, but it didn't help me, is there anyway to get object from LifeBean's getBoard() in TickerBean?