Help with images

I'm creating a tic tac toe program and I want to store the images in an array to call on them when drawing the neccessary shape.

staticvoid LoadImages()

{

if (images)

{

if (pictureArray ==null)

{

pictureArray =new Array();

}

pictureArray[0] =new Image();

pictureArray[0] = Image.IO.read(C:/Documents and Settings/Compaq Owner/Desktop/TicTacToe/empty.jpg);

pictureArray[1] =new Image();

pictureArray[1] ='C:\Documents and Settings\Compaq_Owner\Desktop\TicTacToe\X.jpg';

pictureArray[2] =new Image();

pictureArray[2] ='C:\Documents and Settings\Compaq_Owner\Desktop\TicTacToe\O.jpg';

}

}

Can anyone give me an idea of how to do so? That's what I have so far, I saw it done like this in another program, but I'm not sure how it works or if it's the best way to do it.

[1397 byte] By [Phaceiala] at [2007-11-27 6:56:34]
# 1

> pictureArray = new Array();

> pictureArray[0] = new Image();

> pictureArray[0] = Image.IO.read(C:/Documents and Settings/Compaq

1. what is pictureArray?

2. it's not how to get image...

i hope this will give you a good start

static Image[] pictureArray;

static void LoadImages() {

if (images) {

if (pictureArray == null) pictureArray = new Image[3];

java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); // i doubt you capable to import.

pictureArray[0] = tk.createImage("C:/empty.jpg");

pictureArray[1] = tk.createImage("C:/X.jpg");

pictureArray[2] = tk.createImage("C:/O.jpg");

}

}

j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks, now that I got that part working and I changed it up a bit, I have another question.

if(turn % 2 == 0){

image = tk.createImage("C:/X.jpg");

} else {

image = tk.createImage("C:/O.jpg");

}

for(int i=1; i<=9; i++){

if(a.getSource() == cells[i]){

cells[i].createImage(image);

cells[i].setEnabled(false);

}

}

Cells is declared as a Jbutton array, but I want to replace it with an image once it's clicked, Is there a method to do so, or will I have to make one?

Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 3
do you working with action listener? is it 'a' is action event?
j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 4
Yes I added an action listener to the cells
Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 5

okay. i hope you can get it:

ActionListener listener = new ActionListener() {

public actionPerformed(ActionEvent evt) {

Image = (turn % 2 == 0) ? xImage : oImage;

JButton btn = (JButton) evt.getSource();

btn.setIcon(new ImageIcon(image));

btn.setEnabled(false);

}

};

for (int i = 0; i < buttons.length; i++) buttons[i].addActionListener(listener);

j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 6

I fixed my problem, but the problem I have now is loading the images from the directory they're in. I have to load them from C:\Documents and Settings\Compaq_Owner\Desktop\TicTacToe\X.jpg and

C:\Documents and Settings\Compaq_Owner\Desktop\TicTacToe\O.jpg...

However, when I put those in I get an error message

Message was edited by:

Phaceial

Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 7

1. in previous post, you asked how to load images & store it in array. so repeating reloading is wasting. and instead storing in array, i prefer to save it in different variable.

so the previous code shoud be change like this (not using static keyword):

//static Image[] pictureArray;

Image xImage, oImage, emptyImage;

private void LoadImages() {

Toolkit tk = Toolkit.getDefaultToolkit();

emptyImage = tk.createImage("C:/empty.jpg");

xImage = tk.createImage("C:/X.jpg");

oImage = tk.createImage("C:/O.jpg");

}

2. the loop is outside because all your 9 buttons is using same listener.

so my loop was used for adding that listener to all button.

you also don't need to check if which button in button array that are being pressed.

when you call getSource from action event, you already get a reference to that button.

j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 8
back-slash'\' is escape character. to using '\' as a character in string,u should add escape char first, like this "\\".so your file string should be like this:"C:\\Documents and Settings\\Compaq_Owner\\Desktop\\TicTacToe\\O.jpg"
j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 9

Okay so my following method looks like this:

public void actionPerformed(ActionEvent a) {

turn++;

java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();

//Decides who's turn it is

if(turn % 2 == 0){

image = tk.createImage("C:\\Documents and Settings\\Compaq_Owner\\Desktop\\TicTacToe\\X.jpg");

} else {

image = tk.createImage("C:\\Documents and Settings\\Compaq_Owner\\Desktop\\TicTacToe\\O.jpg");

}

//Draw letters and deactivate buttons

for(int i=1; i<=9; i++){

if(a.getSource() == cells[i]){

cells[i].setIcon(new ImageIcon(image));

cells[i].setEnabled(false);

}

}

//Who's the winner

for(int i=0; i<=7; i++){

if( cells[winCombinations[i][0]].getIcon() == cells[winCombinations[i][1]].getIcon() &&

cells[winCombinations[i][1]].getIcon() == cells[winCombinations[i][2]].getIcon() &&

cells[winCombinations[i][0]].getIcon() != image){

win = true;

}

}

//Box tells the outcome

if(win == true){

JOptionPane.showMessageDialog(null, image + " wins the game!");

System.exit(0);

} else if(turn == 9 && win == false){

JOptionPane.showMessageDialog(null, "The game was tie!");

System.exit(0);

}

}

The only problem I have now is identifying who wins, I put down one thing and it says player "O" wins.

Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 10
your loop for checking who is win will always result false. i suggest you keep that tic-tac-toe game state in a 2D integer array.
j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 11
before I switched it to images, it worked fine with the letters x and o switched the image to null and I'm atleast able to play the game and have it end as a tie, just need to find a way to know we have a winner
Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 12

if you comparing two primitive data types like char:

'x' == 'x'

then it's will works.

however, comparing two images (or object) by '==' this comparator will compare its reference.

Image i1 = new Image();

Image i2 = new Image();

i1 == i2 will result false, even both image come from same jpeg file

j_shadinataa at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...
# 13
Is there a way to make the Images have color? I've been fooling around with it and I haven't come up with a way
Phaceiala at 2007-7-12 18:33:35 > top of Java-index,Java Essentials,New To Java...