Guest User

full text

a guest
Aug 27th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. package com.game;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8.  
  9. public class GamePanel extends JPanel implements ActionListener {
  10.  
  11. public static final int SCREEN_WIDTH=300;
  12. public static final int SCREEN_HEIGHT=300;
  13. public static final int UNIT_SIZE=100;
  14. public static final int AMOUNT_OF_SQUARES= (int)((SCREEN_HEIGHT*SCREEN_WIDTH)/UNIT_SIZE);
  15. public char symbolChart=' ';
  16. public JButton button1, button2,button3,button4,button5,button6,button7,button8,button9;
  17. public int choose;
  18.  
  19. // boolean isSet=false;
  20.  
  21.  
  22.  
  23. public int[] x= new int[AMOUNT_OF_SQUARES];
  24. public int[] y = new int[AMOUNT_OF_SQUARES];
  25.  
  26. GamePanel(){
  27. this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
  28. this.setBackground(Color.darkGray);
  29.  
  30. this.setFocusable(true);
  31. drawXorO();
  32. startPanel();
  33.  
  34.  
  35.  
  36. this.setLayout(new GridLayout(3,3,10,10));
  37.  
  38.  
  39.  
  40. }
  41.  
  42. public void startPanel() {
  43.  
  44.  
  45. }
  46. public void paintComponent(Graphics g){
  47.  
  48. super.paintComponent(g);
  49. draw(g);
  50.  
  51.  
  52.  
  53. }
  54.  
  55. public void draw(Graphics g) {
  56.  
  57.  
  58. /*
  59. for(int i=0; i<SCREEN_WIDTH/UNIT_SIZE; i++){
  60. g.setColor(Color.white);
  61. g.drawLine(i*UNIT_SIZE,0,i*UNIT_SIZE,SCREEN_HEIGHT);
  62. g.drawLine(0,i*UNIT_SIZE,SCREEN_WIDTH, i*UNIT_SIZE);
  63. }
  64. */
  65.  
  66. // narysowac napis Game over jesli winner="cpu"
  67. }
  68.  
  69.  
  70.  
  71. public void drawXorO(){
  72.  
  73. //Jak wyglada X
  74. button1= new JButton();
  75. button1.setText(""+symbolChart);
  76. button1.addActionListener(this);
  77. button1.setFocusPainted(false);
  78. button1.setActionCommand("b1");
  79. this.add(button1);
  80.  
  81. button2= new JButton();
  82. button2.setText(""+symbolChart);
  83. button2.addActionListener(this);
  84. button2.setFocusPainted(false);
  85. button2.setActionCommand("b2");
  86. this.add(button2);
  87.  
  88.  
  89. button3= new JButton();
  90. button3.setText(""+symbolChart);
  91. button3.addActionListener(this);
  92. button3.setFocusPainted(false);
  93. button3.setActionCommand("b3");
  94. this.add(button3);
  95.  
  96. button4= new JButton();
  97. button4.setText(""+symbolChart);
  98. button4.addActionListener(this);
  99. button4.setFocusPainted(false);
  100. button4.setActionCommand("b4");
  101. this.add(button4);
  102.  
  103. button5= new JButton();
  104. button5.setText(""+symbolChart);
  105. button5.addActionListener(this);
  106. button5.setFocusPainted(false);
  107. button5.setActionCommand("b5");
  108. this.add(button5);
  109.  
  110. button6= new JButton();
  111. button6.setText(""+symbolChart);
  112. button6.addActionListener(this);
  113. button6.setFocusPainted(false);
  114. button6.setActionCommand("b6");
  115. this.add(button6);
  116.  
  117.  
  118. button7= new JButton();
  119. button7.setText(""+symbolChart);
  120. button7.addActionListener(this);
  121. button7.setFocusPainted(false);
  122. button7.setActionCommand("b7");
  123. this.add(button7);
  124.  
  125. button8= new JButton();
  126. button8.addActionListener(this);
  127. button8.setActionCommand("b8");
  128. button8.setFocusPainted(false);
  129. button8.setText(""+symbolChart);
  130. this.add(button8);
  131.  
  132. button9= new JButton();
  133. button9.addActionListener(this);
  134. button9.setActionCommand("b9");
  135. button9.setFocusPainted(false);
  136. button9.setText(""+symbolChart);
  137. this.add(button9);
  138.  
  139.  
  140.  
  141. } //tworzy przyciski i przypisuje im charta
  142.  
  143. public void checkWinner(){}
  144.  
  145. public void checkClicked(){} // to moze bedzie w action performed
  146.  
  147. public void randomCPUShot(){
  148.  
  149. choose=(int)((Math.random()*9)+1);
  150. System.out.println(choose);
  151.  
  152. switch (choose){
  153. case 1: checkButton(button1); break;
  154. case 2: checkButton(button2); break;
  155. case 3: checkButton(button3); break;
  156. case 4: checkButton(button4); break;
  157. case 5: checkButton(button5); break;
  158. case 6: checkButton(button6); break;
  159. case 7: checkButton(button7); break;
  160. case 8: checkButton(button8); break;
  161. case 9: checkButton(button9); break;
  162. default: break;
  163.  
  164.  
  165. }
  166.  
  167.  
  168.  
  169. }
  170. public void checkButton(JButton button){
  171. String text = button.getText();
  172.  
  173.  
  174.  
  175. if(text=="X"){ randomCPUShot();
  176. // mam blad taki ze oki losuje miejsce dla O ale jak jest to X no to zamienia X na 0
  177. }
  178. else if(text=="O"){
  179. randomCPUShot();
  180. }else
  181. {
  182. symbolChart='O';
  183. switch (choose){
  184. case 1: button1.setText(""+symbolChart); button1.setEnabled(false); break;
  185. case 2: button2.setText(""+symbolChart);button2.setEnabled(false);break;
  186. case 3: button3.setText(""+symbolChart);button3.setEnabled(false);break;
  187. case 4: button4.setText(""+symbolChart);button4.setEnabled(false);break;
  188. case 5: button5.setText(""+symbolChart);button5.setEnabled(false);break;
  189. case 6: button6.setText(""+symbolChart);button6.setEnabled(false);break;
  190. case 7: button7.setText(""+symbolChart);button7.setEnabled(false);break;
  191. case 8: button8.setText(""+symbolChart);button8.setEnabled(false);break;
  192. case 9: button9.setText(""+symbolChart);button9.setEnabled(false);break;
  193. default:break;
  194. }
  195. }
  196.  
  197. }
  198.  
  199.  
  200.  
  201. @Override
  202. public void actionPerformed(ActionEvent e) {
  203. String command = e.getActionCommand();
  204. switch (command){
  205. case "b1": button1.setEnabled(false); setSymbol(); button1.setText(""+symbolChart); randomCPUShot(); break;
  206. case "b2": button2.setEnabled(false); setSymbol(); button2.setText(""+symbolChart); randomCPUShot(); break;
  207. case "b3": button3.setEnabled(false); setSymbol(); button3.setText(""+symbolChart); randomCPUShot(); break;
  208. case "b4": button4.setEnabled(false); setSymbol(); button4.setText(""+symbolChart); randomCPUShot(); break;
  209. case "b5": button5.setEnabled(false); setSymbol(); button5.setText(""+symbolChart); randomCPUShot(); break;
  210. case "b6": button6.setEnabled(false); setSymbol(); button6.setText(""+symbolChart); randomCPUShot(); break;
  211. case "b7": button7.setEnabled(false); setSymbol(); button7.setText(""+symbolChart); randomCPUShot(); break;
  212. case "b8": button8.setEnabled(false); setSymbol(); button8.setText(""+symbolChart); randomCPUShot(); break;
  213. case "b9": button9.setEnabled(false); setSymbol(); button9.setText(""+symbolChart); randomCPUShot(); break;
  214.  
  215. default :break;
  216. }
  217. repaint();
  218. }
  219.  
  220. public void setSymbol() {
  221. symbolChart='X';
  222. // isSet=true;
  223.  
  224.  
  225.  
  226. repaint();
  227. }
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment