global variables?

Now that my connectfour game works, I want to clean up the code as best I can so I can use it repeatedly. One thing I didn't like is that I was passing an array of images around through all my constructors. Since I only have one array of images in the entire program I want to kind of have it be a sort of global variable that I can access anywhere. Or if I can't do that, since this array of images sits in my GameDisplay class (basically the driver) and I don't have any GameDisplay objects being instantiated maybe I could do something with that. Here's what I'm thinking:

publicclass Board

{

Image display;

Piece[][] grid =new Piece[6][7];//something

ArrayList<Piece> pieces;

int length,width;

public Board()

{

display = GameDisplay.images[0];//I know I've seen something like this done before but I'm not sure how to actually do it

//or display = images[0] where images is some global variable or something

pieces =new ArrayList();

length = 7;

width = 6;

emptyBoard();

}

On a side note, I've heard something about singeltons in java, but I'm not sure if that applies

[1562 byte] By [JFactor2004a] at [2007-11-27 9:08:30]
# 1
I was close...defining images as public static, made it available globally so GameDisplay.images[0] worked
JFactor2004a at 2007-7-12 21:46:46 > top of Java-index,Java Essentials,New To Java...
# 2

Now that it's working, this would be a good time to go back and redesign it to use a model for the game that's separate from the display. You can create another class that maintains the state of the pieces, the images, what is supposed to be currently displayed, etc. Give it methods that let the applet change it's state, and query it's state for displaying the board.

That way your display code only needs to ask what to display, and display it. The workings of moving pieces and such is separated.

hunter9000a at 2007-7-12 21:46:46 > top of Java-index,Java Essentials,New To Java...
# 3

> Now that it's working, this would be a good time to

> go back and redesign it to use a model for the game

> that's separate from the display. You can create

> another class that maintains the state of the pieces,

> the images, what is supposed to be currently

> displayed, etc. Give it methods that let the applet

> change it's state, and query it's state for

> displaying the board.

To the OP: Which I thought you already have done per our prevous conversations, correct?

petes1234a at 2007-7-12 21:46:46 > top of Java-index,Java Essentials,New To Java...
# 4

I understand the necessity of having the GameDisplay only be responsible for displaying things but I'm not sure what specifically you guys are saying...heres my display class. I'm not sure what else can be done...I already have Board, Location, Piece, and a GamePlay class

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class GameDisplay extends Applet implements MouseListener, ActionListener

{

GamePlay game;

public static Image[] images;//On a side note, whats the difference of making this public, static, only public, only static, final etc.? I'm not quite familiar with all the differences

String [] fonts;

Button reset;

Font font;

int font_size;

public void init() //initializes things

{

images = loadImages();

game = new GamePlay();

reset = new Button("Reset");

addMouseListener(this);

reset.addActionListener(this);

add(reset);

font_size = 50;

font = new Font("Sanserif", Font.BOLD, font_size);

}

public void actionPerformed (ActionEvent ae)//responsible for reset

{

if(ae.getSource()==reset)

{

System.out.println("new game");

game.reset();

repaint();

}

}

public void mouseClicked (MouseEvent me)//Updates after a move is made

{

if(!game.gameOver)

{

game.moveMade(me.getX(),me.getY());

repaint();

}

}

public void mouseEntered (MouseEvent me) {}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}

public void paint(Graphics g)//draws the board and pieces

{

g.drawImage(game.board.display, 0, 0, null);

for(int i=0;i<game.board.pieces.size();i++)

{

Piece p=game.board.pieces.get(i);

g.drawImage(p.display, p.X, p.Y,null);

}

g.setFont(font);

g.setColor(Color.black);

g.drawString(game.message,275,300);

}

private Image[] loadImages()//Loads the images used in the game

{

String[] paths =

{

"C:\\Users\\JP\\Projects\\Connect Four\\src\\ConnectFourBoard.gif",

"C:\\Users\\JP\\Projects\\Connect Four\\src\\redpiece.gif",

"C:\\Users\\JP\\Projects\\Connect Four\\src\\blackpiece.gif"

};

Image[] images = new Image[paths.length];

Toolkit t = Toolkit.getDefaultToolkit();

MediaTracker mt = new MediaTracker(this);

for(int j = 0; j >< images.length; j++)

{

images[j] = t.createImage(paths[j]);

mt.addImage(images[j], 0);

}

try

{

mt.waitForAll();

}

catch(InterruptedException e)

{

System.out.println("mt interrupted");

}

return images;

}

}

Message was edited by:

JFactor2004

JFactor2004a at 2007-7-12 21:46:46 > top of Java-index,Java Essentials,New To Java...