Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.game;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class GamePanel extends JPanel implements ActionListener {
- public static final int SCREEN_WIDTH=300;
- public static final int SCREEN_HEIGHT=300;
- public static final int UNIT_SIZE=100;
- public static final int AMOUNT_OF_SQUARES= (int)((SCREEN_HEIGHT*SCREEN_WIDTH)/UNIT_SIZE);
- public char symbolChart=' ';
- public JButton button1, button2,button3,button4,button5,button6,button7,button8,button9;
- public int choose;
- // boolean isSet=false;
- public int[] x= new int[AMOUNT_OF_SQUARES];
- public int[] y = new int[AMOUNT_OF_SQUARES];
- GamePanel(){
- this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
- this.setBackground(Color.darkGray);
- this.setFocusable(true);
- drawXorO();
- startPanel();
- this.setLayout(new GridLayout(3,3,10,10));
- }
- public void startPanel() {
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- draw(g);
- }
- public void draw(Graphics g) {
- /*
- for(int i=0; i<SCREEN_WIDTH/UNIT_SIZE; i++){
- g.setColor(Color.white);
- g.drawLine(i*UNIT_SIZE,0,i*UNIT_SIZE,SCREEN_HEIGHT);
- g.drawLine(0,i*UNIT_SIZE,SCREEN_WIDTH, i*UNIT_SIZE);
- }
- */
- // narysowac napis Game over jesli winner="cpu"
- }
- public void drawXorO(){
- //Jak wyglada X
- button1= new JButton();
- button1.setText(""+symbolChart);
- button1.addActionListener(this);
- button1.setFocusPainted(false);
- button1.setActionCommand("b1");
- this.add(button1);
- button2= new JButton();
- button2.setText(""+symbolChart);
- button2.addActionListener(this);
- button2.setFocusPainted(false);
- button2.setActionCommand("b2");
- this.add(button2);
- button3= new JButton();
- button3.setText(""+symbolChart);
- button3.addActionListener(this);
- button3.setFocusPainted(false);
- button3.setActionCommand("b3");
- this.add(button3);
- button4= new JButton();
- button4.setText(""+symbolChart);
- button4.addActionListener(this);
- button4.setFocusPainted(false);
- button4.setActionCommand("b4");
- this.add(button4);
- button5= new JButton();
- button5.setText(""+symbolChart);
- button5.addActionListener(this);
- button5.setFocusPainted(false);
- button5.setActionCommand("b5");
- this.add(button5);
- button6= new JButton();
- button6.setText(""+symbolChart);
- button6.addActionListener(this);
- button6.setFocusPainted(false);
- button6.setActionCommand("b6");
- this.add(button6);
- button7= new JButton();
- button7.setText(""+symbolChart);
- button7.addActionListener(this);
- button7.setFocusPainted(false);
- button7.setActionCommand("b7");
- this.add(button7);
- button8= new JButton();
- button8.addActionListener(this);
- button8.setActionCommand("b8");
- button8.setFocusPainted(false);
- button8.setText(""+symbolChart);
- this.add(button8);
- button9= new JButton();
- button9.addActionListener(this);
- button9.setActionCommand("b9");
- button9.setFocusPainted(false);
- button9.setText(""+symbolChart);
- this.add(button9);
- } //tworzy przyciski i przypisuje im charta
- public void checkWinner(){}
- public void checkClicked(){} // to moze bedzie w action performed
- public void randomCPUShot(){
- choose=(int)((Math.random()*9)+1);
- System.out.println(choose);
- switch (choose){
- case 1: checkButton(button1); break;
- case 2: checkButton(button2); break;
- case 3: checkButton(button3); break;
- case 4: checkButton(button4); break;
- case 5: checkButton(button5); break;
- case 6: checkButton(button6); break;
- case 7: checkButton(button7); break;
- case 8: checkButton(button8); break;
- case 9: checkButton(button9); break;
- default: break;
- }
- }
- public void checkButton(JButton button){
- String text = button.getText();
- if(text=="X"){ randomCPUShot();
- // mam blad taki ze oki losuje miejsce dla O ale jak jest to X no to zamienia X na 0
- }
- else if(text=="O"){
- randomCPUShot();
- }else
- {
- symbolChart='O';
- switch (choose){
- case 1: button1.setText(""+symbolChart); button1.setEnabled(false); break;
- case 2: button2.setText(""+symbolChart);button2.setEnabled(false);break;
- case 3: button3.setText(""+symbolChart);button3.setEnabled(false);break;
- case 4: button4.setText(""+symbolChart);button4.setEnabled(false);break;
- case 5: button5.setText(""+symbolChart);button5.setEnabled(false);break;
- case 6: button6.setText(""+symbolChart);button6.setEnabled(false);break;
- case 7: button7.setText(""+symbolChart);button7.setEnabled(false);break;
- case 8: button8.setText(""+symbolChart);button8.setEnabled(false);break;
- case 9: button9.setText(""+symbolChart);button9.setEnabled(false);break;
- default:break;
- }
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String command = e.getActionCommand();
- switch (command){
- case "b1": button1.setEnabled(false); setSymbol(); button1.setText(""+symbolChart); randomCPUShot(); break;
- case "b2": button2.setEnabled(false); setSymbol(); button2.setText(""+symbolChart); randomCPUShot(); break;
- case "b3": button3.setEnabled(false); setSymbol(); button3.setText(""+symbolChart); randomCPUShot(); break;
- case "b4": button4.setEnabled(false); setSymbol(); button4.setText(""+symbolChart); randomCPUShot(); break;
- case "b5": button5.setEnabled(false); setSymbol(); button5.setText(""+symbolChart); randomCPUShot(); break;
- case "b6": button6.setEnabled(false); setSymbol(); button6.setText(""+symbolChart); randomCPUShot(); break;
- case "b7": button7.setEnabled(false); setSymbol(); button7.setText(""+symbolChart); randomCPUShot(); break;
- case "b8": button8.setEnabled(false); setSymbol(); button8.setText(""+symbolChart); randomCPUShot(); break;
- case "b9": button9.setEnabled(false); setSymbol(); button9.setText(""+symbolChart); randomCPUShot(); break;
- default :break;
- }
- repaint();
- }
- public void setSymbol() {
- symbolChart='X';
- // isSet=true;
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment