Friday, May 3, 2013

GSM BASED DOOR LOCKING SYSTEM WITH PIC MICRO CONTROLLER


INTERFACE WITH 4×4 keypad and 2×16 LCD This 16-button keypad provides a useful human interface component for micro controller projects. Convenient adhesive backing provides a simple way to mount the keypad in a variety of applications. Matrix keypads use a combination of four rows and four columns to provide button states to the host device, typically a micro-controller. Underneath each key is a push button, with one end connected to one row, and the other end connected to one column. see the image from the link below. http://tutorial.cytron.com.my/wp-content/uploads/2012/08/keypad.jpg In order for the micro-ocontroller to determine which button is pressed, it first needs to pull each of the four column either low or high one at a time, and then poll the states of the four rows. Depending on the states of the rows, the micro controller can tell which button is pressed. CONNECTION ————————————————————————————————————————————————————————- Connection in between SK40C and 4×4 keypad are only required 9 wires which are 5V(VDD) and Port C0-C7. Additional 4 of 10k ohm resistors were used to pull out the 4 columns of the keypad.. see the image from the link below. http://tutorial.cytron.com.my/wp-content/uploads/2012/08/SK40C_4x4-keypad_LCD.jpg see the schematic diagram from the link below http://tutorial.cytron.com.my/wp-content/uploads/2012/08/SK40C_4x4-keypad_LCD_Schematic.png ADDITIONAL INFORMATION ————————————————————————————————————————————————————————- 4×4 Keypad pin can directly connect to microcontroller or keypad decoder IC for decode purpose. However, DIY scanning of keypad consumes a lot of understanding in programming and more program space. The better way to interface a keypad to the PIC is to use a keypad encoder in between the keypad and the microcontroller. One of the Keypad Encoder is theMMC74C922. Simply hook up all the row and the column pins directly to the encoder and it will output a HIGH pulse on the Data Available pin whenever a key is pressed. After that, the encoder will output 4 bits of data to the PIC. Feature Ultra-thin design Adhesive backing Excellent price/performance ratio Easy interface to any microcontroller Example programs provided for the BASIC Key Specifications Maximum Rating: 24 VDC, 30 mA Interface: 8-pin access to 4×4 matrix Operating temperature: 32 to 122 °F(0 to 50°C) Dimensions:64x62x8 mm Application Ideas Security systems Menu selection Data entry for embedded systems CODE OVERVIEW ————————————————————————————————————————————————————————- system.h http://tutorial.cytron.com.my/wp-content/uploads/2012/08/button.jpg In this part of the source code, the pins which were connected to the columns and the rows of the 4×4 keypad were initialized at here. keypad.c http://tutorial.cytron.com.my/wp-content/uploads/2012/08/p11.jpg This part of source code will determined which 4×4 keypad button being pressed. e.g: KP_R1 = 0; KP_R2 = 1; KP_R3 = 1; KP_R4 = 1; __delay_us(30); It will test whether the 1st row of the keypad being pressed or not. If not, it will goto to 2nd row to check for the button pressed. If the button of the 1st row was being pressed, it will test which column is the location. if (KP_C1 == 0) return ’1′; if (KP_C2 == 0) return ’2′; if (KP_C3 == 0) return ’3′; if (KP_C4 == 0) return ‘A’; If the 1st column, it will return 1 in ASCII form. If it was 2nd column, 2 in ASCII form being return. Same go to column 3 and 4. return 0xFF; If the keypad does not being pressed, this function will return 0xFF to the function which will used it. http://tutorial.cytron.com.my/wp-content/uploads/2012/08/wait2.jpg unsigned char keypad_wait(); This function will test whether the keypad button being pressed or not. do{c_pressed_key = keypad_read();}while (c_pressed_key == 0xFF); If the keypad_read() function does not return anything(return 0xFF only) to the c_pressed_key, it will wait at here until it accept one of the 16 characters on the keypad from keypad_read(). return c_pressed_key; Return the accepted data(character in ASCII form) back to main function. MAIN PROGRAM http://tutorial.cytron.com.my/wp-content/uploads/2012/08/init.jpg unsigned char key_get; It is an unsigned char variable used to store the data get from the 4×4 keypad. TRISC = 0b00001111; Initialize the PORTC<7:4> as output, PORTC<3:0> as input lcd_initialize(); Please refer here for the SK40C with lcd tutorial. http://tutorial.cytron.com.my/wp-content/uploads/2012/08/while3.jpg while(1) While loop used in here in order the micro-controller can always read the data from the keypad LED1 = 1; LED2 = 0; Set the LED1 and LED2 light up and off at first respectively. lcd_putstr(“ Enter Word:”); Display Enter Word on the first row of the 16×2 lcd lcd_2ndline(); Go to 2nd row of the 16×2 lcd for(unsigned int i = 0; i < 17; i++) This loop used in order for the LCD to displayed 16 characters key_get = keypad_wait(); The variable key_get will store the data from keypad_wait() function. LED1 ^= 1; LED2 ^= 1; The led will be toggle each time for the button of the keypad being pressed. Either LED1 on and LED2 off or vice versa. lcd_clear(); Clear the LCD

Friday, June 15, 2012

Simple Client Server Chat Application


//Server Chat Code
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
class ServerChat extends JFrame implements Runnable,ActionListener
{
TextArea msgArea;
Thread recieveThread;
TextField msgText;
Button sendButton;
DatagramSocket ds;
int cport=10,sport=11;
ServerChat() throws Exception
{
msgArea=new TextArea(10,10);
msgText=new TextField(10);
sendButton=new Button("send");

setLayout(new FlowLayout(FlowLayout.CENTER));
add(msgArea);
add(msgText);
add(sendButton);

sendButton.addActionListener(this);
setBounds(10,10,200,200);
setVisible(true);
ds=new DatagramSocket(sport);
recieveThread=new Thread(this);
recieveThread.start();
}
public void actionPerformed(ActionEvent e)
{

try
{
String message=msgText.getText();
DatagramPacket dp=new DatagramPacket(message.getBytes(),message.length(),InetAddress.getLocalHost(),cport);

ds.send(dp);
msgArea.append("You:"+message+"\n");
}catch(Exception e1){}

}
public void run()
{

byte b[]=new byte[1000];
while(true)
{
try
{
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String data=new String(dp.getData(),0,dp.getLength());
msgArea.append("Client:"+data+"\n");
}catch(Exception e){}
}

}
public static void main(String x[]) throws Exception
{
new ServerChat();
}


}

// Client chat Code

import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
class ClientChat extends JFrame implements Runnable,ActionListener
{
TextArea msgArea;
Thread recieveThread;
TextField msgText;
Button sendButton;
DatagramSocket ds;
int cport=10,sport=11;
ClientChat() throws Exception
{
msgArea=new TextArea(10,10);
msgText=new TextField(10);
sendButton=new Button("send");

setLayout(new FlowLayout(FlowLayout.CENTER));
add(msgArea);
add(msgText);
add(sendButton);

sendButton.addActionListener(this);
setBounds(10,10,200,200);
setVisible(true);
ds=new DatagramSocket(cport);
recieveThread=new Thread(this);
recieveThread.start();
}
public void actionPerformed(ActionEvent e)
{

try
{
String message=msgText.getText();
DatagramPacket dp=new DatagramPacket(message.getBytes(),message.length(),InetAddress.getLocalHost(),sport);

ds.send(dp);
msgArea.append("You:"+message+"\n");
}catch(Exception e1){}

}
public void run()
{

byte b[]=new byte[1000];
while(true)
{
try
{
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String data=new String(dp.getData(),0,dp.getLength());
msgArea.append("Server:"+data+"\n");
}catch(Exception e){}
}

}
public static void main(String x[]) throws Exception
{
new ClientChat();
}


}

Complete Chat Application using Java Socket Programming


In this chat application there are mainly three programs are their.One program is for login purpose and the second one is the client program and the third is for server program.

As you know client program will use Socket API to bind with the server by providing IP address and Port details. And server will use ServerSocet API to bind with the specific port provided.

Below is source code for complete chat application

LogonApp


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
import javax.swing.event.*;

class LogonApp implements ActionListener, WindowListener
{
 Image icon  = Toolkit.getDefaultToolkit().getImage("icon.gif") ;
 JFrame frameMain = new JFrame("Login Details");
 JPanel panMain = new JPanel();
 JPanel pan1 = new JPanel();
 JPanel pan2 = new JPanel();
 JPanel panLogin = new JPanel();
 JLabel lblName = new JLabel("Enter your user Name: ");
 JTextField txtName = new JTextField();
 JLabel lblIP = new JLabel("Enter the servers IP address: ");
 JTextField txtIP = new JTextField();
 JButton butConnect = new JButton("Connect");

 public LogonApp()
 {
 

  frameMain.setIconImage(icon);
  pan1.setLayout(new GridLayout(2,2));
  pan2.setLayout(new BorderLayout());
  pan1.add(lblName);
  pan1.add(txtName);
  pan1.add(lblIP);
  pan1.add(txtIP);
  pan2.add(butConnect, BorderLayout.SOUTH);
  panMain.add(pan1,BorderLayout.CENTER);
  panMain.add(pan2,BorderLayout.SOUTH);
  frameMain.setContentPane(panMain);
  frameMain.setSize(400,120);
  frameMain.setVisible(true);
  butConnect.addActionListener(this);
  frameMain.addWindowListener(this);
  txtIP.addActionListener(this);
  txtName.addActionListener(this);
 }
 public static void main(String args[])
 {
  LogonApp l1 = new LogonApp();
 }


 public void actionPerformed(ActionEvent ae)
 {

  if(ae.getSource().equals(txtIP))
  {
   butConnect.requestFocus();
  }
  if(ae.getSource().equals(txtName))
  {
   txtIP.requestFocus();
  }
  if(ae.getSource().equals(butConnect))
  {
   if(!(txtIP.getText().equals("")))
   {
   
    if(txtName.getText().equals(""))
    {
     ClientApp c1 = new ClientApp("ANON",txtIP.getText());
     frameMain.dispose();
    }
    else
    {
     ClientApp c1 = new ClientApp(txtName.getText(),txtIP.getText());
     frameMain.dispose();
    }
   }
   else
   {
    JOptionPane.showMessageDialog(
    frameMain,
    "Please Enter a Valid Server IP",
    "Error",
    JOptionPane.ERROR_MESSAGE);
   }
  }
 }
  public void windowOpened(WindowEvent e)
 {}
 public void windowClosing(WindowEvent e)
 {

  System.exit(0);
 }
 public void windowClosed(WindowEvent e)
 {
 
 }
 public void windowIconified(WindowEvent e)
 {}
 public void windowDeiconified(WindowEvent e)
 {

 }
 public void windowActivated(WindowEvent e)
 {}
 public void windowDeactivated(WindowEvent e)
 {
 
 }
}

ClientApp

/**
 *This is the client side of my client server chat program
 **/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
import javax.swing.event.*;

class ClientApp implements ActionListener,WindowListener, Runnable
{
 Socket s;
 BufferedReader br;
 BufferedWriter bw;
 JPanel rPanel = new JPanel();
 JLabel lblLogo;
 String status = "Connected";
 JFrame mainFrame = new JFrame("ODON Solutions Connections");
 Image icon  = Toolkit.getDefaultToolkit().getImage("icon.gif") ;
 JList usersList = new JList();
 JPanel panMainer = new JPanel();
 JPanel panMain = new JPanel();
 JPanel panSendMes = new JPanel();
 JLabel lblStatus;
 JTextField txtMessage = new JTextField();
 JButton butSend = new JButton("Send");
 Color c1 = new Color(255,60,33);
 Color c2 = new Color(192,192,192);
 Vector messageLog = new Vector();
 JScrollPane listPane = new JScrollPane(usersList);
 public String userName=null;
 ImageIcon image = new ImageIcon("Animation1.gif");
 ImageIcon image2 = new ImageIcon("trial.gif");

 //nought and crosses board

 JPanel NNCPanel = new JPanel();
 JPanel panBoard = new JPanel();
 JPanel panButtons = new JPanel();
 JLabel lbl1 = new JLabel("    Game Status:-->");
 JLabel lbl2 = new JLabel("Opponants Turn");
 JButton but11 = new JButton();
 JButton but12 = new JButton();
 JButton but13 = new JButton();
 JButton but21 = new JButton();
 JButton but22 = new JButton();
 JButton but23 = new JButton();
 JButton but31 = new JButton();
 JButton but32 = new JButton();
 JButton but33 = new JButton();
 JButton butStart = new JButton("Start");
 JButton butRestart = new JButton("Restart");
 int b11 = 1;
 int b12 = 1;
 int b13 = 1;
 int b21 = 1;
 int b22 = 1;
 int b23 = 1;
 int b31 = 1;
 int b32 = 1;
 int b33 = 1;
 int sb11 = 0;
 int sb12 = 0;
 int sb13 = 0;
 int sb21 = 0;
 int sb22 = 0;
 int sb23 = 0;
 int sb31 = 0;
 int sb32 = 0;
 int sb33 = 0;
 int myTurn=0;
 ImageIcon nought = new ImageIcon("Nought.gif");
 ImageIcon cross = new ImageIcon("Cross.gif");
 ImageIcon blank = new ImageIcon("Blank.gif");

 public ClientApp(String userName,String IPAddress)
 {
 
  lblLogo = new JLabel("",image,SwingConstants.CENTER);
  panMainer.setLayout(new GridLayout(1,2));
  this.userName = userName;
  lblStatus = new JLabel("USER: "+ userName +"       "+"STATUS: "+status);
  messageLog.clear();
  Border compound,raisedbevel,blackline;
        compound = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
  raisedbevel = BorderFactory.createRaisedBevelBorder();
  blackline = BorderFactory.createLineBorder(Color.black);
  butSend.setBackground(c1);
  butSend.setForeground(Color.white);
  butSend.addActionListener(this);
  lblLogo.setBorder(blackline);
  lblStatus.setBorder(compound);
  panMain.setBorder(raisedbevel);
  rPanel.add(NNCPanel);
  rPanel.add(lblLogo);
  rPanel.setLayout(new GridLayout(2,2));
  usersList.setBorder(blackline);
  usersList.setBackground(c2);
  usersList.setForeground(Color.blue);
  txtMessage.setBorder(compound);
  txtMessage.addActionListener(this);
  lblStatus.setForeground(Color.white);
  panMain.setBackground(c1);
  mainFrame.setIconImage(icon);
  panMain.setLayout(new BorderLayout());
  lblStatus.setHorizontalTextPosition(JLabel.LEFT);
  panSendMes.setLayout(new BorderLayout());
  panSendMes.add(txtMessage,BorderLayout.CENTER);
  panSendMes.add(butSend,BorderLayout.EAST);
  panMain.add(lblStatus,BorderLayout.NORTH);
  panMain.add(listPane,BorderLayout.CENTER);
  panMain.add(panSendMes,BorderLayout.SOUTH);
  panMainer.add(panMain);
  panMainer.add(rPanel);
  mainFrame.setContentPane(panMainer);
  mainFrame.addWindowListener(this);
  //noughts and crosses bit
 
  NNCPanel.setLayout(new BorderLayout());
  panBoard.setLayout(new GridLayout(3,3));
  panButtons.setLayout(new GridLayout(2,2));
  but11.setBackground(Color.WHITE);
  but12.setBackground(Color.WHITE);
  but13.setBackground(Color.WHITE);
  but21.setBackground(Color.WHITE);
  but22.setBackground(Color.WHITE);
  but23.setBackground(Color.WHITE);
  but31.setBackground(Color.WHITE);
  but32.setBackground(Color.WHITE);
  but33.setBackground(Color.WHITE);
  but11.addActionListener(this);
  but12.addActionListener(this);
  but13.addActionListener(this);
  but21.addActionListener(this);
  but22.addActionListener(this);
  but23.addActionListener(this);
  but31.addActionListener(this);
  but32.addActionListener(this);
  but33.addActionListener(this);
  butStart.addActionListener(this);
  butRestart.addActionListener(this);
  panBoard.add(but11);
  panBoard.add(but12);
  panBoard.add(but13);
  panBoard.add(but21);
  panBoard.add(but22);
  panBoard.add(but23);
  panBoard.add(but31);
  panBoard.add(but32);
  panBoard.add(but33);
  panButtons.add(butStart);
  panButtons.add(butRestart);
  panButtons.add(lbl1);
  panButtons.add(lbl2);
  NNCPanel.add(panBoard,BorderLayout.CENTER);
  NNCPanel.add(panButtons,BorderLayout.SOUTH);
  mainFrame.pack();
  mainFrame.setSize(450,500);
  mainFrame.setVisible(true);
  txtMessage.requestFocus();
  try{
    s = new Socket(IPAddress,3000);
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    bw.flush();
    Thread th = new Thread(this);
    th.start();
  
    
   }catch(Exception er){}
 }
 
 public static void main(String args[])
 {
  ClientApp c1 = new ClientApp("ANON","192.168.0.20");
 }


 public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource().equals(butSend)||ae.getSource().equals(txtMessage))
  {
   if(!txtMessage.getText().equals(""))
   {
    
    messageLog.add(userName+": "+txtMessage.getText());
    usersList.setListData(messageLog);
    usersList.ensureIndexIsVisible((usersList.getLastVisibleIndex())+1);
    try{
     bw.write(userName+": "+txtMessage.getText());
     bw.newLine();
     bw.flush();
    }catch(Exception err){}
    txtMessage.setText("");
    txtMessage.requestFocus();
    lblLogo.setIcon(image2);
   }
  }
 
   if(ae.getSource()==but11&&b11==1&&myTurn==1)
  {
   but11.setIcon(cross);
   mainFrame.requestFocus();
   b11=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    
     bw.write("b11");
     bw.newLine();
     bw.flush();
   }catch(Exception er){}
  } 
  else
  if(ae.getSource()==but12&&b12==1&&myTurn==1)
  {
   but12.setIcon(cross);
   mainFrame.requestFocus();
   b12=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
  
    bw.write("b12");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but13&&b13==1&&myTurn==1)
  {
   but13.setIcon(cross);
   mainFrame.requestFocus();
   b13=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
     bw.write("b13");
     bw.newLine();
     bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but21&&b21==1&&myTurn==1)
  {
   but21.setIcon(cross);
   mainFrame.requestFocus();
   b21=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b21");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but22&&b22==1&&myTurn==1)
  {
   but22.setIcon(cross);
   mainFrame.requestFocus();
   b22=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b22");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but23&&b23==1&&myTurn==1)
  {
   but23.setIcon(cross);
   mainFrame.requestFocus();
   b23=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b23");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but31&&b31==1&&myTurn==1)
  {
   but31.setIcon(cross);
   mainFrame.requestFocus();
   b31=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b31");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but32&&b32==1&&myTurn==1)
  {
   but32.setIcon(cross);
   mainFrame.requestFocus();
   b32=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b32");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but33&&b33==1&&myTurn==1)
  {
   but33.setIcon(cross);
   mainFrame.requestFocus();
   b33=0;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b33");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  if(ae.getSource()==butRestart)
  {
   restartGame();
   try{
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
 }

 public void restartGame()
 {
   but11.setIcon(blank);
   but12.setIcon(blank);
   but13.setIcon(blank);
   but21.setIcon(blank);
   but22.setIcon(blank);
   but23.setIcon(blank);
   but31.setIcon(blank);
   but32.setIcon(blank);
   but33.setIcon(blank);
   myTurn=0;
   b11=1;
   b12=1;
   b13=1;
   b21=1;
   b22=1;
   b23=1;
   b31=1;
   b32=1;
   b33=1;
   sb11 = 0;
    sb12 = 0;
   sb13 = 0;
   sb21 = 0;
   sb22 = 0;
   sb23 = 0;
   sb31 = 0;
   sb32 = 0;
   sb33 = 0;
   lbl2.setText("Opponants turn");

 }
 public void run()
 {
  while(true)
  {
  
  
   try{
    String strTemp = br.readLine();
    if(strTemp.equals("RESTART"))
    {
     restartGame();
    
    }
    else
    if(strTemp.equals("b11")&& myTurn==0)
    {
     b11=0;
     sb11=1;
     but11.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
    
    
    }
    else   
    if(strTemp.equals("b12")&& myTurn==0)
    {
     b12=0;
     sb12=1;
     but12.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b13")&& myTurn==0)
    {
     b13=0;
     sb13=1;
     but13.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b21")&& myTurn==0)
    {
     b21=0;
     sb21=1;
     but21.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b22")&& myTurn==0)
    {
     b22=0;
     sb22=1;
     but22.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b23")&& myTurn==0)
    {
     b23=0;
     sb23=1;
     but23.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b31")&& myTurn==0)
    {
     b31=0;
     sb31=1;
     but31.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b32")&& myTurn==0)
    {
     b32=0;
     sb32=1;
     but32.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("b33")&& myTurn==0)
    {
     b33=0;
     sb33=1;
     but33.setIcon(nought);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    }
    else
    if(strTemp.equals("VICTORY"))
    {
     victory();
    }
    else
    {
      
     messageLog.add(strTemp);
     usersList.setListData(messageLog);
    }
    }catch(Exception er){}
  }
 }

 public void windowOpened(WindowEvent e)
 {}
 public void windowClosing(WindowEvent e)
 {
  try{
   s.close();
   }catch(Exception er){}
  System.exit(0);
 }
 public void windowClosed(WindowEvent e)
 {
  try{
   s.close();
   }catch(Exception er){}
  System.exit(0);
 }
 public void windowIconified(WindowEvent e)
 {}
 public void windowDeiconified(WindowEvent e)
 {

 }
 public void windowActivated(WindowEvent e)
 {}
 public void windowDeactivated(WindowEvent e)
 {
 
 }
 public void victory()
 {
  JOptionPane.showMessageDialog(mainFrame, "You Win!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
 }

 public void hasWon()
 {
  if(sb11==1&&sb12==1&&sb13==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but11.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but11.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
    if(sb11==1&&sb22==1&&sb33==1)
   {
   
    int i = 0;
    int i2=0;
    while(i<10)
    {
     while(i2<1000)
     {
      but11.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but22.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but11.setIcon(nought);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but22.setIcon(nought);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(nought);
      i2++;
     }
     i2=0;
     i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "Come Sweet Death!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb11==1&&sb21==1&&sb31==1)
  {
  
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but11.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but11.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "Loser!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb12==1&&sb22==1&&sb32==1)
  {
  
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but12.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "Fatality!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb13==1&&sb22==1&&sb31==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "AWWW.. what happened!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
  else
  if(sb21==1&&sb22==1&&sb23==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but21.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "Whos your Daddy!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
  else
  if(sb31==1&&sb32==1&&sb33==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Suck at this!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb13==1&&sb23==1&&sb33==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(nought);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(nought);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You have been Defeated!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
    }catch(Exception er){}
  }

 }


}

ChatServer.java




/**
 *This is the Server side of my client server chat program
 **/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
import javax.swing.event.*;

class ChatServer implements ActionListener,WindowListener, Runnable
{
 Socket s;
 ServerSocket ss;
 BufferedReader br;
 BufferedWriter bw;
 JPanel rPanel = new JPanel();
 JLabel lblLogo;
 String status = "Connected";
 JFrame mainFrame = new JFrame("ODON Solutions Connections");
 Image icon  = Toolkit.getDefaultToolkit().getImage("icon.gif") ;
 JList usersList = new JList();
 JPanel panMainer = new JPanel();
 JPanel panMain = new JPanel();
 JPanel panSendMes = new JPanel();
 JLabel lblStatus;
 JTextField txtMessage = new JTextField();
 JButton butSend = new JButton("Send");
 Color c1 = new Color(255,60,33);
 Color c2 = new Color(192,192,192);
 Vector messageLog = new Vector();
 JScrollPane listPane = new JScrollPane(usersList);
 public String userName="Server";
 public String IPAddress=null;
 ImageIcon image = new ImageIcon("Animation1.gif");
 ImageIcon image2 = new ImageIcon("trial.gif");

 //nought and crosses board

 JPanel NNCPanel = new JPanel();
 JPanel panBoard = new JPanel();
 JPanel panButtons = new JPanel();
 JLabel lbl1 = new JLabel("     Game Status:->");
 JLabel lbl2 = new JLabel("Your Turn");
 JButton but11 = new JButton();
 JButton but12 = new JButton();
 JButton but13 = new JButton();
 JButton but21 = new JButton();
 JButton but22 = new JButton();
 JButton but23 = new JButton();
 JButton but31 = new JButton();
 JButton but32 = new JButton();
 JButton but33 = new JButton();
 JButton butStart = new JButton("Start");
 JButton butRestart = new JButton("Restart");
 int b11 = 1;
 int b12 = 1;
 int b13 = 1;
 int b21 = 1;
 int b22 = 1;
 int b23 = 1;
 int b31 = 1;
 int b32 = 1;
 int b33 = 1;
 int sb11 = 0;
 int sb12 = 0;
 int sb13 = 0;
 int sb21 = 0;
 int sb22 = 0;
 int sb23 = 0;
 int sb31 = 0;
 int sb32 = 0;
 int sb33 = 0;
 int myTurn=1;
 ImageIcon nought = new ImageIcon("Nought.gif");
 ImageIcon cross = new ImageIcon("Cross.gif");
 ImageIcon blank = new ImageIcon("Blank.gif");

 public ChatServer()
 {
 
  lblLogo = new JLabel("",image,SwingConstants.CENTER);
  panMainer.setLayout(new GridLayout(1,2));
  lblStatus = new JLabel("USER: "+ userName +"       "+"STATUS: "+status);
  messageLog.clear();
  Border compound,raisedbevel,blackline;
        compound = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
  raisedbevel = BorderFactory.createRaisedBevelBorder();
  blackline = BorderFactory.createLineBorder(Color.black);
  butSend.setBackground(c1);
  butSend.setForeground(Color.WHITE);
  butSend.addActionListener(this);
  lblLogo.setBorder(blackline);
  lblStatus.setBorder(compound);
  panMain.setBorder(raisedbevel);
  rPanel.add(NNCPanel);
  rPanel.add(lblLogo);
  rPanel.setLayout(new GridLayout(2,2));
  usersList.setBorder(blackline);
  usersList.setBackground(c2);
  usersList.setForeground(Color.blue);
  txtMessage.setBorder(compound);
  txtMessage.addActionListener(this);
  lblStatus.setForeground(Color.white);
  panMain.setBackground(c1);
  mainFrame.setIconImage(icon);
  panMain.setLayout(new BorderLayout());
  lblStatus.setHorizontalTextPosition(JLabel.LEFT);
  panSendMes.setLayout(new BorderLayout());
  panSendMes.add(txtMessage,BorderLayout.CENTER);
  panSendMes.add(butSend,BorderLayout.EAST);
  panMain.add(lblStatus,BorderLayout.NORTH);
  panMain.add(listPane,BorderLayout.CENTER);
  panMain.add(panSendMes,BorderLayout.SOUTH);
  panMainer.add(panMain);
  panMainer.add(rPanel);
  mainFrame.setContentPane(panMainer);
  mainFrame.addWindowListener(this);
 
  //Noughts and crosses
  NNCPanel.setLayout(new BorderLayout());
  panBoard.setLayout(new GridLayout(3,3));
  panButtons.setLayout(new GridLayout(2,2));
  but11.setBackground(Color.WHITE);
  but12.setBackground(Color.WHITE);
  but13.setBackground(Color.WHITE);
  but21.setBackground(Color.WHITE);
  but22.setBackground(Color.WHITE);
  but23.setBackground(Color.WHITE);
  but31.setBackground(Color.WHITE);
  but32.setBackground(Color.WHITE);
  but33.setBackground(Color.WHITE);
  but11.addActionListener(this);
  but12.addActionListener(this);
  but13.addActionListener(this);
  but21.addActionListener(this);
  but22.addActionListener(this);
  but23.addActionListener(this);
  but31.addActionListener(this);
  but32.addActionListener(this);
  but33.addActionListener(this);
  butStart.addActionListener(this);
  butRestart.addActionListener(this);
  panBoard.add(but11);
  panBoard.add(but12);
  panBoard.add(but13);
  panBoard.add(but21);
  panBoard.add(but22);
  panBoard.add(but23);
  panBoard.add(but31);
  panBoard.add(but32);
  panBoard.add(but33);
  panButtons.add(butStart);
  panButtons.add(butRestart);
  panButtons.add(lbl1);
  panButtons.add(lbl2);
  NNCPanel.add(panBoard,BorderLayout.CENTER);
  NNCPanel.add(panButtons,BorderLayout.SOUTH);
  mainFrame.pack();
  mainFrame.setSize(450,500);
  mainFrame.setVisible(true);
  txtMessage.requestFocus();
 //set serverSocket and Socket properties
  try{
   
    ss = new ServerSocket(3000);
    s = ss.accept();
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    Thread th = new Thread(this);
    th.start();
 
   }catch(Exception er){}


 }


 public static void main(String args[])
 {
  ChatServer cs = new ChatServer();
 }


 public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource().equals(butSend)||ae.getSource().equals(txtMessage))
  {
   if(!txtMessage.getText().equals(""))
   {
    messageLog.add(userName+": "+txtMessage.getText());
    usersList.setListData(messageLog);
    usersList.ensureIndexIsVisible((usersList.getLastVisibleIndex())+1);
    try{
     bw.write(userName+": "+txtMessage.getText());
     bw.newLine();
     bw.flush();
    }catch(Exception err){}
    txtMessage.setText("");
    txtMessage.requestFocus();
    lblLogo.setIcon(image2);
   }
  }
 
  if(ae.getSource()==but11&&b11==1&&myTurn==1)
  {
   but11.setIcon(nought);
   mainFrame.requestFocus();
   b11=0;
   sb11=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    
     bw.write("b11");
     bw.newLine();
     bw.flush();
   }catch(Exception er){}
  } 
  else
  if(ae.getSource()==but12&&b12==1&&myTurn==1)
  {
   but12.setIcon(nought);
   mainFrame.requestFocus();
   b12=0;
   sb12=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
  
    bw.write("b12");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but13&&b13==1&&myTurn==1)
  {
   but13.setIcon(nought);
   mainFrame.requestFocus();
   b13=0;
   sb13=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
     bw.write("b13");
     bw.newLine();
     bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but21&&b21==1&&myTurn==1)
  {
   but21.setIcon(nought);
   mainFrame.requestFocus();
   b21=0;
   sb21=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b21");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but22&&b22==1&&myTurn==1)
  {
   but22.setIcon(nought);
   mainFrame.requestFocus();
   b22=0;
   sb22=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b22");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but23&&b23==1&&myTurn==1)
  {
   but23.setIcon(nought);
   mainFrame.requestFocus();
   b23=0;
   sb23=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b23");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but31&&b31==1&&myTurn==1)
  {
   but31.setIcon(nought);
   mainFrame.requestFocus();
   b31=0;
   sb31=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b31");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but32&&b32==1&&myTurn==1)
  {
   but32.setIcon(nought);
   mainFrame.requestFocus();
   b32=0;
   sb32=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b32");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  else
  if(ae.getSource()==but33&&b33==1&&myTurn==1)
  {
   but33.setIcon(nought);
   mainFrame.requestFocus();
   b33=0;
   sb33=2;
   myTurn=0;
   lbl2.setText("Oponents turn...");
   try{
    bw.write("b33");
    bw.newLine();
    bw.flush();
   }catch(Exception er){}
  }
  if(ae.getSource()==butRestart)
  {
  restartGame();
  try{
   bw.write("RESTART");
   bw.newLine();
   bw.flush();
  }catch(Exception er){}
  }
 
 
 
 }
 
 public void restartGame()
 {
   but11.setIcon(blank);
   but12.setIcon(blank);
   but13.setIcon(blank);
   but21.setIcon(blank);
   but22.setIcon(blank);
   but23.setIcon(blank);
   but31.setIcon(blank);
   but32.setIcon(blank);
   but33.setIcon(blank);
   myTurn=1;
   b11=1;
   b12=1;
   b13=1;
   b21=1;
   b22=1;
   b23=1;
   b31=1;
   b32=1;
   b33=1;
   sb11 = 0;
    sb12 = 0;
   sb13 = 0;
   sb21 = 0;
   sb22 = 0;
   sb23 = 0;
   sb31 = 0;
   sb32 = 0;
   sb33 = 0;
   lbl2.setText("Your turn...");
 
 }
 public void run()
 {
  while(true)
  {
  
  
   try{
    String strTemp = br.readLine();
   
    if(strTemp.equals("RESTART"))
    {
     restartGame();
    
    }
    else 
    if(strTemp.equals("b11")&& myTurn==0)
    {
     b11=0;
     sb11=1;
     but11.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else   
    if(strTemp.equals("b12")&& myTurn==0)
    {
     b12=0;
     sb12=1;
     but12.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b13")&& myTurn==0)
    {
     b13=0;
     sb13=1;
     but13.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b21")&& myTurn==0)
    {
     b21=0;
     sb21=1;
     but21.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b22")&& myTurn==0)
    {
     b22=0;
     sb22=1;
     but22.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b23")&& myTurn==0)
    {
     b23=0;
     sb23=1;
     but23.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
     
    }
    else
    if(strTemp.equals("b31")&& myTurn==0)
    {
     b31=0;
     sb31=1;
     but31.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
     
    }
    else
    if(strTemp.equals("b32")&& myTurn==0)
    {
     b32=0;
     sb32=1;
     but32.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
    
    }
    else
    if(strTemp.equals("b33")&& myTurn==0)
    {
     b33=0;
     sb33=1;
     but33.setIcon(cross);
     myTurn=1;
     lbl2.setText("Your Turn");
     hasWon();
     
    }
    else
    if(strTemp.equals("VICTORY"))
    {
     victory();
    }
    else
    {
      
     messageLog.add(strTemp);
     usersList.setListData(messageLog);
    }
    }catch(Exception er){}
  }
 }

 public void windowOpened(WindowEvent e)
 {}
 public void windowClosing(WindowEvent e)
 {
  try{
   s.close();
   ss.close();
   }catch(Exception er){}
  System.exit(0);
 }
 public void windowClosed(WindowEvent e)
 {
  try{
   s.close();
   ss.close();
   }catch(Exception er){}
  System.exit(0);
 }
 public void windowIconified(WindowEvent e)
 {}
 public void windowDeiconified(WindowEvent e)
 {

 }
 public void windowActivated(WindowEvent e)
 {}
 public void windowDeactivated(WindowEvent e)
 {
 
 }
 public void victory()
 {
  JOptionPane.showMessageDialog(mainFrame, "You Win!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
 }
 public void hasWon()
 {
  if(sb11==1&&sb12==1&&sb13==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but11.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but11.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
    if(sb11==1&&sb22==1&&sb33==1)
   {
    int i = 0;
    int i2=0;
    while(i<10)
    {
     while(i2<1000)
     {
      but11.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but22.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but11.setIcon(cross);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but22.setIcon(cross);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(blank);
      i2++;
     }
     i2=0;
     while(i2<100000)
     {
      but33.setIcon(cross);
      i2++;
     }
     i2=0;
     i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb11==1&&sb21==1&&sb31==1)
  {
  
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but11.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but11.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb12==1&&sb22==1&&sb32==1)
  {
  
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but12.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but12.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb13==1&&sb22==1&&sb31==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
  else
  if(sb21==1&&sb22==1&&sb23==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but21.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but21.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but22.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
  else
  if(sb31==1&&sb32==1&&sb33==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but31.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but31.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but32.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }
   else
  if(sb13==1&&sb23==1&&sb33==1)
  {
   int i = 0;
   int i2=0;
   while(i<10)
   {
    while(i2<1000)
    {
     but13.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but13.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but23.setIcon(cross);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(blank);
     i2++;
    }
    i2=0;
    while(i2<100000)
    {
     but33.setIcon(cross);
     i2++;
    }
    i2=0;
    i++;
   }
   JOptionPane.showMessageDialog(mainFrame, "You Lost!", "Noughts and Crosses", JOptionPane.INFORMATION_MESSAGE);
   restartGame();
   try{
    bw.write("VICTORY");
    bw.newLine();
    bw.flush();
    bw.write("RESTART");
    bw.newLine();
    bw.flush();
   
    }catch(Exception er){}
  }

 }

}