Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package tictactoe;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import javax.swing.*;
  9.  
  10. public class Board extends JPanel implements ActionListener {
  11. private int[][] board = new int[3][3];
  12. private boolean redClick = false;
  13. private Calculate calc = new Calculate();
  14. private JLabel[][] label = new JLabel[3][3];
  15. private ImageIcon cross = Main.getImageIcon("/images/cross_alpha.png");
  16. private ImageIcon circle = Main.getImageIcon("/images/circle_alpha.png");
  17. private Font font = new Font("SansSerif", Font.BOLD, 46);
  18. private JLabel text = new JLabel();
  19. private JButton btnExit = new JButton("Avsluta");
  20. private JButton btnRestart = new JButton("Starta om");
  21.  
  22. public Board() {
  23. setPreferredSize(new Dimension(700, 700));
  24. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  25. JPanel boardGrid = new JPanel(new GridLayout(board.length, board[0].length, -1, -1));
  26. JPanel btnLayout = new JPanel();
  27. btnLayout.setLayout(new BoxLayout(btnLayout, BoxLayout.X_AXIS));
  28.  
  29. for (int i = 0; i < board.length; i++) {
  30. for (int j = 0; j < board[i].length; j++) {
  31. int row = i;
  32. int col = j;
  33. JLabel label = new JLabel();
  34. this.label[i][j] = label;
  35. label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  36. label.setOpaque(true);
  37. label.setHorizontalAlignment(SwingConstants.CENTER);
  38. boardGrid.add(label);
  39. label.addMouseListener(new MouseListener() {
  40. public void mouseClicked(MouseEvent e) {
  41.  
  42. }
  43. // other mouse listener functions
  44.  
  45. @Override
  46. public void mousePressed(MouseEvent e) {
  47. action(row, col);
  48. }
  49.  
  50. @Override
  51. public void mouseReleased(MouseEvent e) {
  52.  
  53. }
  54.  
  55. @Override
  56. public void mouseEntered(MouseEvent e) {
  57.  
  58. }
  59.  
  60. @Override
  61. public void mouseExited(MouseEvent e) {
  62.  
  63. }
  64. });
  65.  
  66. }
  67. }
  68.  
  69. btnExit.setFont(font);
  70. btnExit.addActionListener(this);
  71. btnRestart.setFont(font);
  72. btnRestart.addActionListener(this);
  73. btnLayout.setMaximumSize(new Dimension(500, 80));
  74. boardGrid.setMaximumSize(new Dimension(600, 600));
  75.  
  76. btnLayout.add(btnExit);
  77. btnLayout.add(Box.createRigidArea(new Dimension(60, 0)));
  78. btnLayout.add(btnRestart);
  79.  
  80. text.setFont(font);
  81. text.setText("X tur");
  82.  
  83. add(text);
  84. add(boardGrid);
  85. add(btnLayout);
  86. }
  87.  
  88. public void action(int row, int col) {
  89. if(board[row][col] == 0 && calc.isPlaying() == true)
  90. {
  91. if (redClick == false) {
  92. label[row][col].setIcon(cross);
  93. redClick = true;
  94. calc.setCalculate(col, row, 1);
  95. board[row][col] = 1;
  96. text.setText("O tur");
  97. text.setForeground(new Color(33, 170, 224));
  98. } else if (redClick) {
  99. board[row][col] = 2;
  100. label[row][col].setIcon(circle);
  101. redClick = false;
  102. calc.setCalculate(col, row, 2);
  103. text.setText("X tur");
  104. text.setForeground(Color.BLACK);
  105. }
  106. }
  107.  
  108. System.out.println(row + ", " + col);
  109. calc.calcWinner(board, redClick);
  110. }
  111.  
  112. public void setText(String text) {
  113. this.text.setText(text);
  114. // this.text.setForeground(color);
  115. }
  116.  
  117. @Override
  118. public void actionPerformed(ActionEvent e) {
  119. if (e.getSource() == btnExit) {
  120. System.exit(0);
  121. } else if (e.getSource() == btnRestart) {
  122. for (int i = 0; i < board.length; i++) {
  123. for (int j = 0; j < board[i].length; j++) {
  124. label[i][j].setIcon(null);
  125. board[i][j] = 0;
  126. redClick = false;
  127. calc.setPlaying(true);
  128. text.setText("X tur");
  129. text.setForeground(Color.BLACK);
  130. }
  131. }
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement