//tic tac toe..using applet ...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
//
//
@author - techieRJ
//
**/
//<applet code=tic_tac_toe width=500 height=500></applet>
public class tic_tac_toe extends Applet implements ActionListener
{
Button FF,FS,FT,SF,SS,ST,TF,TS,TT;
String F_F="z",F_S="z",F_T="z",S_F="z",S_S="z",S_T="z",T_F="z",T_S="z",T_T="z";
Button Select_X,Select_O,Replay;
//where f=first s=second t=third...first letter is for row and second is for column;
Panel panel1,panel2,panel3,cardpanel;
Label lbl_win;
CardLayout card;
String value_selected;
String last_value="O";
public void init()
{
Font font=new Font("Verdana", Font.BOLD, 20);
setFont(font);
setForeground(Color.blue);
setBackground(Color.yellow);
card = new CardLayout();
setLayout(card);
cardpanel = new Panel();
add(cardpanel);
cardpanel.setLayout(card);
//first panel
panel1 = new Panel();
cardpanel.add(panel1,"First");
Select_X = new Button("Select X");
Select_O = new Button("Select O");
panel1.add(Select_X);
panel1.add(Select_O);
//add action event
Select_X.addActionListener(this);
Select_O.addActionListener(this);
//end of panel 1
//panel 2
panel2 =new Panel();
cardpanel.add(panel2,"Second");
panel2.setLayout(new GridLayout(3,3));
FF = new Button("");
FS = new Button("");
FT = new Button("");
SF = new Button("");
SS = new Button("");
ST = new Button("");
TF = new Button("");
TS = new Button("");
TT = new Button("");
panel2.add(FF);
panel2.add(FS);
panel2.add(FT);
panel2.add(SF);
panel2.add(SS);
panel2.add(ST);
panel2.add(TF);
panel2.add(TS);
panel2.add(TT);
FF.addActionListener(this);
FS.addActionListener(this);
FT.addActionListener(this);
SF.addActionListener(this);
SS.addActionListener(this);
ST.addActionListener(this);
TF.addActionListener(this);
TS.addActionListener(this);
TT.addActionListener(this);
//end of panel2
//start of panel 3
panel3 = new Panel();
panel3.setLayout(new GridLayout(2,1));
cardpanel.add(panel3,"Third");
Replay = new Button("Repaly");
lbl_win = new Label("");
panel3.add(lbl_win);
panel3.add(Replay);
lbl_win.setBounds(10,10,100,200);
Replay.setBounds(50,50,300,200);
Replay.addActionListener(this);
//end of panel 3
}
public void actionPerformed(ActionEvent e)
{
//action first event
if(e.getSource()==Select_X)
{
value_selected= "X";
last_value = value_selected;
card.show(cardpanel,"Second");
}
if(e.getSource()==Select_O)
{
value_selected= "O";
last_value = value_selected;
card.show(cardpanel,"Second");
}
//end of panel 1
//second panel
if(e.getSource()==FF)
{
FF.setLabel(last_value);
FF.setEnabled(false);
F_F=last_value;
last();
won();
check();
}
if(e.getSource()==FS)
{
FS.setLabel(last_value);
FS.setEnabled(false);
F_S=last_value;
last();
won();
check();
}
if(e.getSource()==FT)
{
FT.setLabel(last_value);
FT.setEnabled(false);
F_T=last_value;
last();
won();
check();
}
if(e.getSource()==SF)
{
SF.setLabel(last_value);
S_F=last_value;
SF.setEnabled(false);
last();
won();
check();
}
if(e.getSource()==SS)
{
SS.setLabel(last_value);
SS.setEnabled(false);
S_S=last_value;
last();
won();
check();
}
if(e.getSource()==ST)
{
ST.setLabel(last_value);
ST.setEnabled(false);
S_T=last_value;
last();
won();
check();
}
if(e.getSource()==TF)
{
TF.setLabel(last_value);
TF.setEnabled(false);
T_F=last_value;
last();
won();
check();
}
if(e.getSource()==TS)
{
TS.setLabel(last_value);
TS.setEnabled(false);
T_S=last_value;
last();
won();
check();
}
if(e.getSource()==TT)
{
TT.setLabel(last_value);
TT.setEnabled(false);
T_T=last_value;
last();
won();
check();
}
//panel 3
if(e.getSource()==Replay)
{
again();
}
}
public void last()
{
if(last_value.equals("X"))
{
last_value="O";
}
else
{
last_value="X";
}
}
public void won()
{
//case 1 :--- horizontal line
if((F_F.equals("X") && F_S.equals("X") && F_T.equals("X")) ||(F_F.equals("O") && F_S.equals("O") && F_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((S_F.equals("X") && S_S.equals("X") && S_T.equals("X") ) || (S_F.equals("O") && S_S.equals("O") && S_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((T_F.equals("X") && T_S.equals("X") && T_T.equals("X") ) || (T_F.equals("O") && T_S.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
//case 2 :---- vertical line
if((F_F.equals("X") && S_F.equals("X") && T_F.equals("X")) ||(F_F.equals("O") && S_F.equals("O") && T_F.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_S.equals("X") && S_S.equals("X") && T_S.equals("X")) || (F_S.equals("O") && S_S.equals("O") && T_S.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_T.equals("X") && S_T.equals("X") && T_T.equals("X")) || (F_T.equals("O") && S_T.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
//case 3 : diagonal
if((F_F.equals("X") && S_S.equals("X") && T_T.equals("X")) || (F_F.equals("O") && S_S.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_T.equals("X") && S_S.equals("X") && T_F.equals("X")) || (F_T.equals("O") && S_S.equals("O") && T_F.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
}
public void again()
{
F_F="z";F_S="z";F_T="z";S_F="z";S_S="z";S_T="z";T_F="z";T_S="z";T_T="z";
FF.setEnabled(true);
FS.setEnabled(true);
FT.setEnabled(true);
SF.setEnabled(true);
SS.setEnabled(true);
ST.setEnabled(true);
TF.setEnabled(true);
TS.setEnabled(true);
TT.setEnabled(true);
FF.setLabel("");
FS.setLabel("");
FT.setLabel("");
SF.setLabel("");
SS.setLabel("");
ST.setLabel("");
TF.setLabel("");
TS.setLabel("");
TT.setLabel("");
lbl_win.setText("");
card.show(cardpanel,"First");
}
public void check()
{
if(!F_F.equals("z") && !F_S.equals("z") && !F_T.equals("z") && !S_F.equals("z") && !S_S.equals("z") && !S_T.equals("z") && !T_F.equals("z") && !T_S.equals("z") && !T_T.equals("z"))
{
lbl_win.setText("Draw.....");
card.show(cardpanel,"Third");
}
}
}
Showing posts with label applet. Show all posts
Showing posts with label applet. Show all posts
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class calculator extends Applet implements ActionListener
{
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btn_mul,btn_div,btn_min,btn_add,btn_enter,btn_decimal,btn_clr;
Label lbltext;
String temp1="",temp2,temp4;
double temp3;
boolean decimal=false,mul=false,div=false,add=false,min=false,first=true;
public void init()
{
setForeground(Color.blue);
setBackground(Color.yellow);
Font font = new Font("Verdana", Font.BOLD, 20);
setFont(font);
setLayout(null);
btn1 = new Button("1");
btn2 = new Button("2");
btn3 = new Button("3");
btn4 = new Button("4");
btn5 = new Button("5");
btn6 = new Button("6");
btn7 = new Button("7");
btn8 = new Button("8");
btn9 = new Button("9");
btn0 = new Button("0");
btn_mul = new Button("*");
btn_div = new Button("/");
btn_min = new Button("-");
btn_add = new Button("+");
btn_enter = new Button("=");
btn_decimal = new Button(".");
btn_clr = new Button("Clear All");
lbltext = new Label("Press Button to add value..");
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn5);
add(btn6);
add(btn7);
add(btn8);
add(btn9);
add(btn0);
add(btn_mul);
add(btn_div);
add(btn_min);
add(btn_add);
add(btn_enter);
add(btn_decimal);
add(lbltext);
add(btn_clr);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
btn_mul.addActionListener(this);
btn_div.addActionListener(this);
btn_min.addActionListener(this);
btn_add.addActionListener(this);
btn_enter.addActionListener(this);
btn_decimal.addActionListener(this);
btn_clr.addActionListener(this);
lbltext.setBounds(10,10,450,30);
btn1.setBounds(10,45,50,50);
btn2.setBounds(90,45,50,50);
btn3.setBounds(170,45,50,50);
btn_mul.setBounds(250,45,50,50);
btn4.setBounds(10,125,50,50);
btn5.setBounds(90,125,50,50);
btn6.setBounds(170,125,50,50);
btn_div.setBounds(250,125,50,50);
btn7.setBounds(10,205,50,50);
btn8.setBounds(90,205,50,50);
btn9.setBounds(170,205,50,50);
btn_min.setBounds(250,205,50,50);
btn0.setBounds(10,285,50,50);
btn_decimal.setBounds(90,285,50,50);
btn_enter.setBounds(170,285,50,50);
btn_add.setBounds(250,285,50,50);
btn_clr.setBounds(10,365,250,50);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
temp1=temp1+"1";
lbltext.setText(temp1);
}
if(e.getSource()==btn2)
{
temp1=temp1+"2";
lbltext.setText(temp1);
}
if(e.getSource()==btn3)
{
temp1=temp1+"3";
lbltext.setText(temp1);
}
if(e.getSource()==btn4)
{
temp1=temp1+"4";
lbltext.setText(temp1);
}
if(e.getSource()==btn5)
{
temp1=temp1+"5";
lbltext.setText(temp1);
}
if(e.getSource()==btn6)
{
temp1=temp1+"6";
lbltext.setText(temp1);
}
if(e.getSource()==btn7)
{
temp1=temp1+"7";
lbltext.setText(temp1);
}
if(e.getSource()==btn8)
{
temp1=temp1+"8";
lbltext.setText(temp1);
}
if(e.getSource()==btn9)
{
temp1=temp1+"9";
lbltext.setText(temp1);
}
if(e.getSource()==btn0)
{
temp1=temp1+"0";
lbltext.setText(temp1);
}
if(e.getSource()==btn_decimal&& decimal==false)
{
temp1=temp1+".";
lbltext.setText(temp1);
decimal=true;
}
if(e.getSource()==btn_clr)
{
decimal=false;
mul=false;
div=false;
add=false;
min=false;
first=true;
temp1="";
temp2="";
temp3=0;
temp4="";
lbltext.setText("");
}
if(e.getSource()==btn_mul)
{
mul=true;
div=false;
min=false;
add=false;
temp2=temp1;
lbltext.setText("");
temp1="";
//if(temp4!=null){action(); }
}
if(e.getSource()==btn_div)
{
mul=false;
div=true;
min=false;
add=false;
temp2=temp1;
lbltext.setText("");
temp1="";
//if(first==false){action(); }
}
if(e.getSource()==btn_min)
{
mul=false;
div=false;
min=true;
add=false;
temp2=temp1;
lbltext.setText("");
temp1="";
if(first==false){action(); }
first=false;
}
if(e.getSource()==btn_add)
{
mul=false;
div=false;
min=false;
add=true;
temp2=temp1;
lbltext.setText("");
temp1="";
//if(first==false){action(); }
first=false;
}
if(e.getSource()==btn_enter)
{
action();
}
}
public void action()
{
if(mul)
{
temp3 = Double.parseDouble(temp2) * Double.parseDouble(temp1);
temp4=Double.toString(temp3);
temp1=temp4;
lbltext.setText(temp4);
mul=false;
div=false;
min=false;
add=false;
}
if(div)
{
temp3 = Double.parseDouble(temp2) / Double.parseDouble(temp1);
temp4=Double.toString(temp3);
temp1=temp4;
lbltext.setText(temp4);
mul=false;
div=false;
min=false;
add=false;
}
if(min)
{
temp3 = Double.parseDouble(temp2) - Double.parseDouble(temp1);
temp4=Double.toString(temp3);
temp1=temp4;
lbltext.setText(temp4);
mul=false;
div=false;
min=false;
add=false;
}
if(add)
{
temp3 = Double.parseDouble(temp2) + Double.parseDouble(temp1);
temp4=Double.toString(temp3);
temp1=temp4;
lbltext.setText(temp4);
mul=false;
div=false;
min=false;
add=true;
}
}
}
Output