Hi ive been working for a few days now trying to figure out how to make it so when a button is pressed in this game instead of just a simple X or O showing up an image of lets say a dog for X and a cat for O or something along these lines would show up instead- i cannot figure it out so would any one be able to help me? Ive got it so the words scr/x(or)o.jpg show up but no image :(
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.ImageIcon;
public class TicTacToe extends JPanel implements ActionListener
{
//Class constants how big ect the window will be
private static final int WINDOW_WIDTH = 400;
private static final int WINDOW_HEIGHT = 400;
private static final int TEXT_WIDTH = 10;
ImageIcon X =new ImageIcon("scr/x.jpg");
ImageIcon O= new ImageIcon("scr/o.jpg");
/*Sets up the a 3 by 3 playing grid used the supplied java help
to figure this out*/
private static final GridLayout Layout_Style = new GridLayout(3,3);
//Button and window-how the frame and buttons or squares should look
private JFrame window = new JFrame("TicTacToe");
private JButton Square1 = new JButton("");
private JButton Square2 = new JButton("");
private JButton Square3 = new JButton("");
private JButton Square4 = new JButton("");
private JButton Square5 = new JButton("");
private JButton Square6 = new JButton("");
private JButton Square7 = new JButton("");
private JButton Square8 = new JButton("");
private JButton Square9 = new JButton("");
String mark = "scr/x.jpg";
boolean win = false;
public TicTacToe()
{
window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exits the application once the game has been won
window.setLocationRelativeTo(null);//Centers the window
//Add to window the internet helped me to see what this should look like
window.getContentPane().setLayout(Layout_Style);
window.getContentPane().add(Square1);
window.getContentPane().add(Square2);
window.getContentPane().add(Square3);
window.getContentPane().add(Square4);
window.getContentPane().add(Square5);
window.getContentPane().add(Square6);
window.getContentPane().add(Square7);
window.getContentPane().add(Square8);
window.getContentPane().add(Square9);
//Add listener
Square1.addActionListener(this);
Square2.addActionListener(this);
Square3.addActionListener(this);
Square4.addActionListener(this);
Square5.addActionListener(this);
Square6.addActionListener(this);
Square7.addActionListener(this);
Square8.addActionListener(this);
Square9.addActionListener(this);
window.setVisible(true);
}
//ActionPerformed
public void actionPerformed(ActionEvent e)
{
// counts one click of the mouse and then searches for which button has been pressed
int count=1;
do
{
if (e.getSource()==Square1)
/*If user marks Square 1 then it will set a mark or it will continue on
searching until a Square is marked, prob cleaner way to do this tho..*/
{
Square1.setText(mark);
Square1.setEnabled(false);
}
else if (e.getSource()==Square2)
{
Square2.setText(mark);
Square2.setEnabled(false);
}
else if (e.getSource()==Square3)
{
Square3.setText(mark);
Square3.setEnabled(false);
}
else if (e.getSource()==Square4)
{
Square4.setText(mark);
Square4.setEnabled(false);
}
else if (e.getSource()==Square5)
{
Square5.setText(mark);
Square5.setEnabled(false);
}
else if (e.getSource()==Square6)
{
Square6.setText(mark);
Square6.setEnabled(false);
}
else if (e.getSource()==Square7)
{
Square7.setText(mark);
Square7.setEnabled(false);
}
else if (e.getSource()==Square8)
{
Square8.setText(mark);
Square8.setEnabled(false);
}
else if (e.getSource()==Square9)
{
Square9.setText(mark);
Square9.setEnabled(false);
}
//Checks to see if 3 corresponding X's or O's Match-up
if (Square1.getText().equals(Square2.getText()) && Square2.getText().equals
(Square3.getText()) && Square1.getText().equals("")==false)
{
Square1.setBackground(Color.BLACK);// i don't think this colour thing actually works but i can't see why
Square2.setBackground(Color.BLACK);
Square3.setBackground(Color.BLACK);
win=true;
}
else if (Square4.getText().equals(Square5.getText()) && Square5.getText().equals
(Square6.getText())&& Square4.getText().equals("")==false)
{
Square4.setBackground(Color.BLACK);
Square5.setBackground(Color.BLACK);
Square6.setBackground(Color.BLACK);
win=true;
}
else if (Square7.getText().equals(Square8.getText()) && Square8.getText().equals
(Square9.getText())&& Square7.getText().equals("")==false)
{
Square7.setBackground(Color.BLACK);
Square8.setBackground(Color.BLACK);
Square9.setBackground(Color.BLACK);
win=true;
}
else if (Square1.getText().equals(Square4.getText()) && Square4.getText().equals
(Square7.getText())&& Square1.getText().equals("")==false)
{
Square1.setBackground(Color.BLACK);
Square4.setBackground(Color.BLACK);
Square7.setBackground(Color.BLACK);
win=true;
}
else if (Square2.getText().equals(Square5.getText()) && Square5.getText().equals
(Square8.getText())&& Square2.getText().equals("")==false)
{
Square2.setBackground(Color.BLACK);
Square5.setBackground(Color.BLACK);
Square8.setBackground(Color.BLACK);
win=true;
}
else if (Square3.getText().equals(Square6.getText()) && Square6.getText().equals
(Square9.getText())&& Square3.getText().equals("")==false)
{
Square3.setBackground(Color.BLACK);
Square6.setBackground(Color.BLACK);
Square9.setBackground(Color.BLACK);
win=true;
}
else if (Square1.getText().equals(Square5.getText()) && Square5.getText().equals
(Square9.getText())&& Square1.getText().equals("")==false)
{
Square1.setBackground(Color.BLACK);
Square5.setBackground(Color.BLACK);
Square9.setBackground(Color.BLACK);
win=true;
}
else if (Square3.getText().equals(Square5.getText()) && Square5.getText().equals
(Square7.getText())&& Square3.getText().equals("")==false)
{
Square3.setBackground(Color.BLACK);
Square5.setBackground(Color.BLACK);
Square7.setBackground(Color.BLACK);
win=true;
}
if (count==9 && win== false)
{
JOptionPane.showMessageDialog(null, mark + "Too bad its a tie");//at the moment this isn't working but i cannot see why
System.exit(1);
win=false;
}
System.out.println("Button Pressed");
if (count!=9 && win==true)
{
JOptionPane.showMessageDialog(null, mark + " You Win!!!");
System.exit(1);
}
if (mark.equals("scr/x.jpg"))
{
mark="scr/o.jpg";
}
else
{
mark="scr/x.jpg";
}
}
while(win=false);
}
public static void main(String[] args)
{
TicTacToe gui = new TicTacToe();
}
}