Advertisement
Guest User

ConnectLDriver3

a guest
Apr 17th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class ConnectLDriver extends JApplet implements MouseListener {
  8.  
  9. ConnectLGame thisGame = new ConnectLGame();
  10. Canvas canvas;
  11. uiHandler uih;
  12. int sqWid,sqHgt;
  13. int turn;
  14. JTextField label;
  15. JComboBox aiBox;
  16. boolean initialClick = false;
  17. boolean falseClick = false;
  18. Integer[] aiLevel = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  19.  
  20.  
  21. public class Canvas extends JPanel {
  22. public void paintComponent(Graphics g){
  23. super.paintComponent(g);
  24.  
  25. int w,h;
  26. int rows = thisGame.rows;
  27. int cols = thisGame.cols; // number of rows and columns for the board
  28.  
  29. w = getWidth();
  30. h = getHeight();
  31.  
  32. g.setColor( Color.black);
  33. g.drawLine(10,10,200,200);
  34.  
  35. sqWid = w/cols;
  36. sqHgt = h/rows;
  37.  
  38. g.setColor(Color.gray);
  39. g.fillRect(0, 0, 500, 500);
  40.  
  41. for (int j = 0; j < rows; j++) {
  42. for (int i=0; i < cols; i++) {
  43. g.setColor(Color.black);
  44. g.drawLine(i*sqWid, 0, i*sqWid, h);
  45. g.drawLine(0, j*sqHgt, w, j*sqHgt);
  46.  
  47. if(thisGame.getValueInLoc(j, i) != thisGame.MARK_NONE) {
  48. if(thisGame.getValueInLoc(j, i) %2 == thisGame.MARK_O){
  49. g.setColor(Color.red);
  50. g.fillOval(i*sqWid, j*sqHgt, sqWid, sqHgt);
  51. }
  52. else if (thisGame.getValueInLoc(j, i) %2 == thisGame.MARK_X)
  53. g.setColor(Color.black);
  54. g.fillOval(i*sqWid, j*sqHgt, sqWid, sqHgt);
  55. }
  56. }
  57. }
  58. if(thisGame.getGameState() == thisGame.GAME_STATE_X_TURN)
  59. label.setText("Black's Turn");
  60. else if(thisGame.getGameState() == thisGame.GAME_STATE_O_TURN)
  61. label.setText("Red's Turn");
  62. else if (thisGame.getGameState() == thisGame.GAME_STATE_O_WON)
  63. label.setText("Red Wins");
  64. else if (thisGame.getGameState() == thisGame.GAME_STATE_X_WON)
  65. label.setText("Black Wins");
  66. if(falseClick)
  67. label.setText("Choose another column");
  68. }
  69. }
  70.  
  71. private class uiHandler implements ActionListener { // The event listener.
  72. public void actionPerformed(ActionEvent e) {
  73. initialClick = true;
  74. checkBox();
  75. thisGame.reset();
  76. canvas.repaint();
  77.  
  78. }
  79. } // end uiHandler class
  80.  
  81. public void init() { // initialize the applet
  82.  
  83. this.setSize(500, 500);
  84. createComponents();
  85.  
  86. }
  87.  
  88. public void createComponents() {
  89.  
  90. JPanel content = new JPanel();
  91. content.setLayout( new BorderLayout());
  92.  
  93. JPanel uip = new JPanel();
  94. uip.setLayout( new FlowLayout());
  95. label = new JTextField("Press New Game button");
  96. aiBox = new JComboBox(aiLevel);
  97.  
  98.  
  99. JButton b4 = new JButton("New Game");
  100. uip.add(aiBox);
  101. uip.add(b4);
  102. uip.add(label);
  103.  
  104.  
  105. uih = new uiHandler();
  106. b4.addActionListener(uih);
  107. canvas = new Canvas();
  108. canvas.setBackground(new Color(210, 180, 140));
  109.  
  110. canvas.addMouseListener(this);
  111.  
  112. content.add(canvas, BorderLayout.CENTER);
  113. content.add(uip, BorderLayout.SOUTH);
  114. setContentPane(content);
  115.  
  116. }
  117.  
  118.  
  119. public void checkBox() {
  120.  
  121. if(initialClick == false || thisGame.getGameState() == thisGame.GAME_STATE_O_WON || thisGame.getGameState() == thisGame.GAME_STATE_X_WON)
  122. {
  123.  
  124. aiBox.setEnabled(true);
  125. }
  126. else
  127. aiBox.setEnabled(false);
  128. }
  129. @Override
  130. public void mouseClicked(MouseEvent e) {
  131. // TODO Auto-generated method stub
  132.  
  133. //System.out.println( "Coords "+ e.getX() + " " + e.getY());
  134. int x = e.getX();
  135. int y = e.getY();
  136. int c, r;
  137. c = x / sqWid;
  138. r = y / sqHgt;
  139. // System.out.println("TEST " + c + " " + r);
  140.  
  141. if(initialClick == false)
  142. label.setText("Press New Game button");
  143. else {
  144.  
  145. if(thisGame.placeChecker(c)) {
  146. falseClick = false;
  147. if ( thisGame.getGameState() == ConnectLGame.GAME_STATE_O_TURN ||
  148. thisGame.getGameState() == ConnectLGame.GAME_STATE_X_TURN) {
  149.  
  150. mmPlayer.play(thisGame, aiBox.getSelectedIndex());
  151. }
  152. }
  153. else {
  154. falseClick = true;
  155.  
  156. }
  157.  
  158. checkBox();
  159. canvas.repaint();
  160. }
  161.  
  162.  
  163. }
  164.  
  165. @Override
  166. public void mouseEntered(MouseEvent arg0) {
  167. // TODO Auto-generated method stub
  168.  
  169. }
  170.  
  171.  
  172. @Override
  173. public void mouseExited(MouseEvent arg0) {
  174. // TODO Auto-generated method stub
  175.  
  176. }
  177.  
  178.  
  179. @Override
  180. public void mousePressed(MouseEvent arg0) {
  181. // TODO Auto-generated method stub
  182.  
  183. }
  184.  
  185.  
  186. @Override
  187. public void mouseReleased(MouseEvent arg0) {
  188. // TODO Auto-generated method stub
  189.  
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement