Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tictactoe;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.*;
- public class Board extends JPanel implements ActionListener {
- private int[][] board = new int[3][3];
- private boolean redClick = false;
- private Calculate calc = new Calculate();
- private JLabel[][] label = new JLabel[3][3];
- private ImageIcon cross = Main.getImageIcon("/images/cross_alpha.png");
- private ImageIcon circle = Main.getImageIcon("/images/circle_alpha.png");
- private Font font = new Font("SansSerif", Font.BOLD, 46);
- private JLabel text = new JLabel();
- private JButton btnExit = new JButton("Avsluta");
- private JButton btnRestart = new JButton("Starta om");
- public Board() {
- setPreferredSize(new Dimension(700, 700));
- setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
- JPanel boardGrid = new JPanel(new GridLayout(board.length, board[0].length, -1, -1));
- JPanel btnLayout = new JPanel();
- btnLayout.setLayout(new BoxLayout(btnLayout, BoxLayout.X_AXIS));
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- int row = i;
- int col = j;
- JLabel label = new JLabel();
- this.label[i][j] = label;
- label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- label.setOpaque(true);
- label.setHorizontalAlignment(SwingConstants.CENTER);
- boardGrid.add(label);
- label.addMouseListener(new MouseListener() {
- public void mouseClicked(MouseEvent e) {
- }
- // other mouse listener functions
- @Override
- public void mousePressed(MouseEvent e) {
- action(row, col);
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- });
- }
- }
- btnExit.setFont(font);
- btnExit.addActionListener(this);
- btnRestart.setFont(font);
- btnRestart.addActionListener(this);
- btnLayout.setMaximumSize(new Dimension(500, 80));
- boardGrid.setMaximumSize(new Dimension(600, 600));
- btnLayout.add(btnExit);
- btnLayout.add(Box.createRigidArea(new Dimension(60, 0)));
- btnLayout.add(btnRestart);
- text.setFont(font);
- text.setText("X tur");
- add(text);
- add(boardGrid);
- add(btnLayout);
- }
- public void action(int row, int col) {
- if(board[row][col] == 0 && calc.isPlaying() == true)
- {
- if (redClick == false) {
- label[row][col].setIcon(cross);
- redClick = true;
- calc.setCalculate(col, row, 1);
- board[row][col] = 1;
- text.setText("O tur");
- text.setForeground(new Color(33, 170, 224));
- } else if (redClick) {
- board[row][col] = 2;
- label[row][col].setIcon(circle);
- redClick = false;
- calc.setCalculate(col, row, 2);
- text.setText("X tur");
- text.setForeground(Color.BLACK);
- }
- }
- System.out.println(row + ", " + col);
- calc.calcWinner(board, redClick);
- }
- public void setText(String text) {
- this.text.setText(text);
- // this.text.setForeground(color);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnExit) {
- System.exit(0);
- } else if (e.getSource() == btnRestart) {
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- label[i][j].setIcon(null);
- board[i][j] = 0;
- redClick = false;
- calc.setPlaying(true);
- text.setText("X tur");
- text.setForeground(Color.BLACK);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement