You are on page 1of 2

TicTacToe

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="TicTacToe" width="500" height="400"></applet>
*/
public class TicTacToe extends Applet implements ActionListener

String str;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
public void init()
{
System.out.println("Nitin Pathak");
System.out.println("CSE(A2)");
System.out.println("17ERECS045");

str = "0";
b1=new Button();
b2=new Button();
b3=new Button();
b4=new Button();
b5=new Button();
b6=new Button();
b7=new Button();
b8=new Button();
b9=new Button();

GridLayout gb=new GridLayout(3,3);


setLayout(gb);

add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);

b1.setBackground(Color.green);
b2.setBackground(Color.green);
b3.setBackground(Color.green);

Page 1
TicTacToe
b4.setBackground(Color.blue);
b5.setBackground(Color.blue);
b6.setBackground(Color.blue);

b7.setBackground(Color.green);
b8.setBackground(Color.green);
b9.setBackground(Color.green);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);

}
public void paint()
{
//setBackground(Color.blue);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1) b1.setLabel(printValue());
if (e.getSource() == b2) b2.setLabel(printValue());
if (e.getSource() == b3) b3.setLabel(printValue());
if (e.getSource() == b4) b4.setLabel(printValue());
if (e.getSource() == b5) b5.setLabel(printValue());
if (e.getSource() == b6) b6.setLabel(printValue());
if (e.getSource() == b7) b7.setLabel(printValue());
if (e.getSource() == b8) b8.setLabel(printValue());
if (e.getSource() == b9) b9.setLabel(printValue());
}

public String printValue()


{
if (str == "X") {str = "0"; return str;}
else {str = "X"; return str;}
}
}

Page 2

You might also like