1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| 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(); 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(); } } |