• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Could you please help me?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have been trying to fix this assignment for nearly 2 weeks now, and I am still stumped, so I was wondering if someone here might point out a few things for me. My assignment is to create a game in which organisms grow or die according to a set pattern, notably that if any organism is next to less than 2 or more than 3 organisms it will die, but if there is a spot adjacent to 3 organisms, the organism will live. However I got this to work perfectly fine in the console, but when I tried to make it into a JPanel program, I get strange results. Sometimes the program works fine, other times the program behaves erratically, like it will do the grow/die methods for only part of the cluster of organisms or it will lag (skipping a few iterations). Also the console tells me I have a null pointer exception. I am really confused about this and if you could so kindly help me I would really appreciate it, thanks!

Here is the source code:

*Note that the actual starting point of the program is the method "begin(),"
this is where I suspect the bugs are hidden, but I can't seem to understand why.*

Cheers

****************************************************************************

import javax.swing.*;
import java.awt.*;

public class GameFrame extends JFrame

{
public GameFrame()
{
super("Animation");//calls the 1-param constructor JFrame
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();//draw on panels
panel.setLayout(new GridLayout(1,1) );//1 rwo 1 col
Life life = new Life();//executing design

panel.add(life);
life.setBackground(Color.white);
setContentPane(panel);//all GUIs placed on ContentPane
show();//shows the panel BUT also calls paintComponent
life.begin();
}

public static void main(String[] asd)
{
GameFrame obj = new GameFrame();
}
}

//******************************************************************************

class Life extends JPanel
/*
* reading a life world
*
*/
{

final int M = 25;
final int N = 75;
final char DOT = '.';
final char ORGANISM = 'X';

char[][] grid;
char[][] newGrid;

int universalCounter = 0;

boolean flag = false;
boolean flag1 = false;

boolean flag2 = false;


//********************************

public void zero(char[][] array)
{
for(int row = 0; row <= M +1; row++)
{
for(int col = 0; col <= N +1; col++)
{
array[row][col] = DOT;
}
}
}

//********************************

public void print(Graphics g)
{
int x, y = 20;
for(int row = 1; row <= M; row++)
{
x = 5;
for(int col = 1; col <= N; col++)
{
g.drawString("" + grid[row][col], x, y);
//prints string begining at x,y
x = x + 7;
}
y = y + 15;
}
}

//********************************

public void read(String file)
{
FileStringReader f = new FileStringReader(file);
String s;
char ch;
for(int row = 1; row <= M; row++)
{
s = f.readLine();
for(int col = 0; col < N; col++)
{
ch = s.charAt(col);
this.grid[row][col + 1] = ch ;
}
}
}

//********************************

public void scanWorld(char[][] grid)
{
boolean liveOrDie;

for(int row = 1; row < M; row++)
{
for(int col = 1; col < N; col++)
{
char currentBox = grid[row][col];

liveOrDie = liveOrDie(grid, row, col);

}
}
}

//********************************

public boolean liveOrDie(char[][] grid, int row, int col)
{
char blockTest = grid[row][col];
char blockTest1 = grid[row-1][col-1];
char blockTest2 = grid[row-1][col];
char blockTest3 = grid[row-1][col+1];
char blockTest4 = grid[row][col-1];
char blockTest5 = grid[row][col+1];
char blockTest6 = grid[row+1][col-1];
char blockTest7 = grid[row+1][col];
char blockTest8 = grid[row+1][col+1];

int counter = 0;
boolean live;

//if(blockTest == ORGANISM) {
//counter++;
//}
if(blockTest1 == ORGANISM) {
counter++;
}
if(blockTest2 == ORGANISM) {
counter++;
}
if(blockTest3 == ORGANISM) {
counter++;
}
if(blockTest4 == ORGANISM) {
counter++;
}
if(blockTest5 == ORGANISM) {
counter++;
}
if(blockTest6 == ORGANISM) {
counter++;
}
if(blockTest7 == ORGANISM) {
counter++;
}
if(blockTest8 == ORGANISM) {
counter++;
}

if( (counter > 3) || (counter < 2) ) {

this.dies(this.newGrid, row, col);
return live = false;

} else {
if ( (counter == 3) && (blockTest == DOT) ){
this.grow(this.newGrid, row, col);
return live = true;
} else
if ( (counter == 3) && (blockTest == ORGANISM)){
this.preserve(grid, this.newGrid, row, col);
return live = true;
}
this.preserve(grid, this.newGrid, row, col);
return live = true;

}
}

//********************************

public void grow(char[][] newGrid, int row, int col ) {

for(int r = 1; r < M+1; r++){

if(row == r){

for(int c = 1; c < N+1; c++){

if(col == c){
newGrid[r][c] = ORGANISM;
}
}
}
}
}

//********************************

public void dies(char[][] newGrid, int row, int col ) {

for(int r = 1; r < M+1; r++){

if(row == r){

for(int c = 1; c < N+1; c++){

if(col == c){
newGrid[r][c] = DOT;
}
}
}
}
}

//********************************

public void preserve(char[][] grid, char[][] newGrid, int row, int col) {

for(int r = 1; r < M+1; r++) {

if(row == r) {

for(int c = 1; c < N+1; c++){

if(col == c) {

if(grid[r][c] == ORGANISM) {

newGrid[r][c] = grid[r][c];
}
}
}
}
}
}

//********************************

public void copyArray(char[][] firstArray, char[][] secondArray){

for(int row = 0; row < M+1; row++)
{
for(int col = 0; col < N+1; col++)
{
secondArray[row][col] = firstArray[row][col];
}
}
}

//********************************

public boolean isSame(char[][] firstArray, char[][] secondArray){

boolean same = true;

if(!isEmpty(firstArray) )
{

for(int row = 0; row < M+1 && same; row++)
{
for(int col = 0; col < N+1 && same; col++)
{
same = same && (firstArray[row][col] == secondArray[row][col]);
}
}
return same;

} else {
return same = false;
}

}

//********************************

public boolean isEmpty(char[][] array){

boolean empty = true;
for(int row = 1; row <= M; row++)
{
for(int col = 1; col <= N; col++)
{
empty = empty && (array[row][col] == DOT);
}
}
return empty;
}

//********************************

public void oneSec() {

try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}

//********************************

public static void manySec(long s) {
try {
Thread.currentThread().sleep(s * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}


//********************************

public void begin ()
{
this.grid = new char[M + 2][N + 2];
this.newGrid = new char[M + 2][N + 2];

this.zero(this.newGrid);
//this.zero(this.grid);
this.read("life2.dat");

this.repaint();
this.oneSec();

do {

this.scanWorld(this.grid);
this.repaint();

this.copyArray(this.newGrid, this.grid);
this.zero(this.newGrid); //grid should have updated world while newGrid returns to default state

this.oneSec();

this.universalCounter++;
} while( !flag1 && !flag2);

}


//********************************

public void paintComponent(Graphics g)
{
flag1 = isSame(this.grid, this.newGrid);
flag2 = isEmpty(this.grid);


if(flag)//skips executing paintComponent on first call
{


super.paintComponent(g);//erases panel Contents
g.setColor(Color.black);

if(!flag1 && !flag2)//not isSame nor isBlank
{

print(g);//prints the world
}
else if( flag1 )
{

print(g);
g.drawString("The worlds are repeating!", 10, 460);
}
else //flag2 is true
{
print(g);
g.drawString("The world is blank!", 10, 460);

}

g.drawString("Iteration " + (universalCounter), 10, 440);


}
else
flag = true;
}

}
 
Paul Chung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Btw, here is the class FileStringReader, which you also need for this program

****************************************************************************

import java.io.*;
//enables reading of file
public class FileStringReader
{ FileInputStream in;
BufferedReader reader ;

public FileStringReader( String s)
{ try
{ File f1 = new File( s );
in = new FileInputStream(f1);
InputStreamReader inStream = new InputStreamReader(in);
reader = new BufferedReader(inStream);
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
}

public String readLine()
{ String inputLine ="";
try
{inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}

public void close()
{ try
{ reader.close();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
}
}
 
Paul Chung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...And the database file (life2.txt):

******************************************************************************
Starts from here:

...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
..................XXX......................................................
...............XXX...XXX.........................XXXXX.....................
.............XXXX.....XXXX...................XXXXX...XXXXX.................
...............XXX...XXX.........................XXXXX.....................
..................XXX......................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
..................XXX......................................................
...............XXX...XXX.........................XXXXX.....................
.............XXXX.....XXXX...................XXXXX...XXXXX.................
...............XXX...XXX.........................XXXXX.....................
..................XXX......................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic