Hello people I need some help with making tic tac toe , i have made it with buttons , but now i want to make it without buttons , like I place my own tic tac toe grid on the frame and then when user clicks on a certain block then it should but circle or cross on that place , I know mouseListener will be used , but i want to know how could i possibly do it on my own image?
You could do it with Rectangles
class MyButton{
Rectangle bounds = {x,y,width,height};
String name;
}
u could put them all into array too.
ArrayList buttons;
in event handler:
mousePressed(Event e){
for(MyButton t: buttons){
if(t.bounds.contains(e.getX(),e.getY())){
system out println t.name pressed
}
}
I don’t get the rectangle thing you told , I do know about the mouseListeners and stuff :-\
The Rectangle is for catching a mouse click , you could also add a picture for MyButton:
class MyButton{
Rectangle bounds = {x,y,width,height};
String name;
BufferedImage circle;
BufferedImage cross;
int picture = 0;
public MyButton(x,y,w,h){
here set object members
URL url = this.getClass().getResource("circle.png");
circle= ImageIO.read(url);
url = this.getClass().getResource("cross.png");
cross= ImageIO.read(url);
}
public void clicked(){
if picture == 0 then change to 1 or 2 depending if cross or circle
}
public void draw(Graphics g){
if(picture == 0){
draw white square
}
if( picture == 1) {
g.drawImage circle using bounds as location
}
if( picture == 2) {
g.drawImage cross using bounds as location
}
}
}
in game create the array of buttons if one is clicked do with it what u need in MyButton clicked method.
First, I admire you took a step back from your RTS plans and started with tic-tac-toe.
Rectangle is a class inside the java.awt.* package. Use the Javadocs to see how to use it. List can be found in java.util.*, and the example of ‘indexunknown’ shows you how to use the enhanced for loop, you can switch to a simple for loop, or use an iterator with a while loop.
I’m really not trying to be lame here, but searching for stuff with Google is often much faster (a few seconds) to yield an answer than using this forum (a few hours).
Riven but sometimes google doesnt have the answers ;).
Google knows everything about tic tac toe.
I have searched on google but didn’t find any custom made java tic tac toe , all of em are made with buttons
, i too have made one with buttons but i want to do it the other way now :persecutioncomplex:
In my first post, I mean that Google knows everything about Rectangle too, as you asked about that.
I’m guessing by buttons you mean the keyboard. If you already made the game using keyboard input and I’m guessing console output then all you really need to change are those 2 things, the logic should run the same either way.
For the drawing a simple passive rendering should work great. Look into overwriting java.awt.Panel or javax.swing.JPanel. For input just use code similar to what indexunknown posted for your grid cells then just keep a list of the (9 or whatever you need) cells and whenever the mouse is pressed you can loop through checking each cell untill you find the cell that was clicked on or the user didn’t click a cell. If the user did click a cell use the logic you already wrote for the console version.
The JavaDoc is a great way to learn about Java.
http://java.sun.com/javase/6/docs/api/
The top left box shows the package (such as java.awt) the bottom left panel shows the actual classes and stuff in that package (like Rectangle).
by buttons he means taht he used java.awt.JButton (atleast I thinkt hat is the location)
ok here’s my code so far the problem now i have is to choose which turn is it of the cross or circle and also how to make checks that this player has won or not
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class myTicTacToe extends MouseAdapter{
public myTicTacToe(){
jf=new JFrame("Tic Tac Toe");
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.setResizable(false);
jf.setLayout(null);
ii=new ImageIcon("F:\\ttt\\frame.gif");
c=new ImageIcon("F:\\ttt\\circle.gif");
cx=new ImageIcon("F:\\ttt\\cross.gif");
jl=new JLabel(ii);
circle=new JLabel(c);
cross=new JLabel(cx);
jpi=new JPanel(); // panel
jf.addMouseListener(this);
test=new JLabel();
jf.add(test);
test.setBounds(20,20,50,50);
}
public void init(){
jf.add(jl);
jf.add(jpi);
test.setBounds(50,10,100,10);
jpi.setBounds(50,75,290,290);
jpi.setLayout(null);
jl.setBounds(50,75,300,300);
for(int i=0;i<9;i++){
box[i]=new JLabel();
jpi.add(box[i]);
box[i].addMouseListener(this);
}
}
public void mousePressed(MouseEvent me){
int x=me.getX();
int y=me.getY();
test.setText(x+" "+y);
if( (x>76 && x<151) && (y>131 && y<203) ){
box[0].setBounds(27,27,70,70);
box[0].setIcon(new ImageIcon("circle.gif"));
}
else if( (x>169 && x<242) && (y>131 && y<203) ){
box[1].setBounds(118,27,70,70);
box[1].setIcon(new ImageIcon("cross.gif"));
System.out.println("2nd");
}
else if( (x>258 && x<333) && (y>131 && y<203) ){
box[2].setBounds(210,27,70,70);
box[2].setIcon(new ImageIcon("circle.gif"));
System.out.println("3rd");
}
else if( (x>76 && x<151) && (y>222 && y<288) ){
box[3].setBounds(27,115,70,70);
box[3].setIcon(new ImageIcon("cross.gif"));
System.out.println("4");
}
else if( (x>169 && x<242) && (y>222 && y<288) ){
box[4].setBounds(118,115,70,70);
box[4].setIcon(new ImageIcon("cross.gif"));
System.out.println("5");
}
else if( (x>258 && x<333) && (y>222 && y<288) ){
box[5].setBounds(210,115,70,70);
box[5].setIcon(new ImageIcon("cross.gif"));
System.out.println("6");
}
else if( (x>76 && x<151) && (y>308 && y<381) ){
box[6].setBounds(27,205,70,70);
box[6].setIcon(new ImageIcon("cross.gif"));
System.out.println("7");
}
else if( (x>169 && x<242) && (y>308 && y<381) ){
box[7].setBounds(118,205,70,70);
box[7].setIcon(new ImageIcon("cross.gif"));
System.out.println("8");
}
else if( (x>258 && x<333) && (y>308 && y<381) ){
box[8].setBounds(210,205,70,70);
box[8].setIcon(new ImageIcon("cross.gif"));
System.out.println("9");
}
}
private JFrame jf;
private String p1;
private String p2;
private ImageIcon ii,c,cx;
private JLabel jl;
private JLabel circle,test;
private JLabel cross;
private JPanel jpi;
private JLabel box[]=new JLabel[9];
private int count=0;
private boolean player=false;
}
class myMain{
public static void main(String arg[]){
myTicTacToe ttt=new myTicTacToe();
ttt.init();
}
}
You basically do not have any game code, only a slightly intelligent user-interface class.
Simplistic game code could look like this:
class Player
{
Game game;
void pick(int x, int y)
{
game.playerPicks(this, x, y);
}
}
class Game
{
Player player1, player2;
Player playing;
Square[][] squares = new Square[3][3];
void playerPicks(Player p, int x, int y)
{
if(p != playing)
throw new IllegalStateException("shh, it was not your turn");
Square square = squares[x][y];
if(square.owner != null)
throw new IllegalStateException("this square is already taken");
square.owner = p;
if(playing == player1)
player = player2;
else if(playing == player2)
player = player1;
// check for winning move
view.repaint();
}
}
class Square
{
int x, y, w, h;
Player owner;
}
class View
{
void paint(Graphics g)
{
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
Square square = game.squares[x][y];
Player owner = square.owner;
// draw the square at X,Y, with the icon for the player, if any
}
}
}
void mouseClicked(...)
{
Player player = game.playing;
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
Square square = game.squares[x][y];
if(square.x >= xMouse && square.x + square.w <= xMouse)
if(square.y >= yMouse && square.y + square.h <= yMouse)
player.pick(x, y);
}
}
}
}
If anybody is going to suggest improvements to this code… well, don’t be Captain Obvious, this is code to get him started.
:-\
Do you realise how tempting it will be for him to copy/paste??
yeah, me learning how ot make tic tac to was ruined by finding the exact source code for the game, so I didnt have to findout how to do it.
Riven thanks , your code did give me an idea about neat coding that is to be used in games , and I don’t copy source codes so don’t worry about it :).
And can you name any good books related to java game programming ?
Thanks
I don’t buy books… maybe somebody else can jump in.
That’s your own fault.
I know, but in my defense,
I googled, how to make a grid of buttons.
I get a guys asking how to do that exact thing, great!
only problem, the source given is a game of tic tac to, I picked the lesser of two evils
A)Dont finish the project
B)Look at the code