Problem z przyciskiem (Java.swing)

0

Witam, tworzę właśnie prostą gierkę "Tron" używając do tego biblioteki java.swing i napotkałem pewien problem. Projekt posiada klasę Board (która odpowiada za cały przebieg gry na chwilę obecną łącznie z rysowaniem itd.) oraz klasę Menu w której tworzę sobie kilka przycisków oraz dodaję Board który ma wyświetlać mi grę. Napotkałem jednak problem z ustawieniem działania przycisku Start, na chwilę obecną odpowiada on za wystartowanie zegara gry, tutaj zaczyna się jej właściwy przebieg, przycisk staje się nieaktywny i po zakończeniu gry przycisk staje się znów aktywny. W tym momencie chciałbym aby kolejne naciśnięcie przycisku powodowało "zrestartowanie" gry i zaczęcie jej od początku, niestety nie mam pojęcia jak się za to zabrać, nie udaje mi się ponownie uruchomić gry a próbowałem na kilka sposobów. Czy mógłbym Was prosić o pomoc ?

PS. Wiem, że kod to totalna amatorszczyzna i nie za wiele ma wspólnego z zasadami dobrego programowania ale dopiero się uczę. Z góry dzięki za wyrozumiałość.

Klasa Board:

/**
 * Created by HoulScream on 31.05.2017.
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

import static com.sun.deploy.uitoolkit.ToolkitStore.dispose;

public class Board extends JPanel implements ActionListener
{

    private final int B_WIDTH = 1600;
    private final int B_HEIGHT = 800;
    private final int DOT_SIZE = 25;
    private final int ALL_DOTS = 2048;

    private int dots = 0;
    private int delay = 100;
    public static boolean inGame = true;
    public static Timer timer;

    private final int p1x[] = new int[ALL_DOTS];
    private final int p1y[] = new int[ALL_DOTS];
    private boolean p1LeftDirection = false;
    private boolean p1RightDirection = true;
    private boolean p1UpDirection = false;
    private boolean p1DownDirection = false;
    private boolean p1Dead = false;
    private Image p1Body;
    private Image p1Head;

    private final int p2x[] = new int[ALL_DOTS];
    private final int p2y[] = new int[ALL_DOTS];
    private boolean p2LeftDirection = false;
    private boolean p2RightDirection = true;
    private boolean p2UpDirection = false;
    private boolean p2DownDirection = false;
    private boolean p2Dead = false;
    private Image p2Body;
    private Image p2Head;

    public Board()
    {

        addKeyListener(new KeyboardSettings());
        setBackground(Color.black);
        setFocusable(true);

        setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
        loadImages();
        initGame();
    }

    private void loadImages()
    {

        ImageIcon p1HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1rightmouth.png");
        ImageIcon p1BodyLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1snakeimage.png");
        p1Head = p1HeadLoader.getImage();
        p1Body = p1BodyLoader.getImage();

        ImageIcon p2HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2rightmouth.png");
        ImageIcon p2BodyLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2snakeimage.png");
        p2Head = p2HeadLoader.getImage();
        p2Body = p2BodyLoader.getImage();

    }

    private void initGame()
    {

        p1x[0] = 50;
        p1y[0] = 200;

        p2x[0] = 50;
        p2y[0] = 600;

        timer = new Timer(delay, this);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        doDrawing(g);
    }

    private void doDrawing(Graphics g)
    {
        if (inGame)
        {
            for (int z = 0; z < dots; z++)
            {
                if (z == 0)
                {
                    g.drawImage(p1Head, p1x[z], p1y[z], this);
                    g.drawImage(p2Head, p2x[z], p2y[z], this);
                }
                else
                {
                    g.drawImage(p1Body, p1x[z], p1y[z], this);
                    g.drawImage(p2Body, p2x[z], p2y[z], this);
                }
            }
            Toolkit.getDefaultToolkit().sync();
        }
        else
        {
            gameOver(g);
        }
    }

    private void gameOver(Graphics g)
    {
        String msg = "Null";

        if(p1Dead && p2Dead)
        {
            msg = "Deuce! WTF";
        }

        if(p1Dead && !p2Dead)
        {
            msg = "P2 Wins!";
        }

        if(p2Dead && !p1Dead)
        {
            msg = "P1 Wins!";
        }
        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = getFontMetrics(small);

        g.setColor(Color.white);
        g.setFont(small);
        g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
    }

    private void move()
    {

        for (int z = dots; z > 0; z--)
        {
            p1x[z] = p1x[(z - 1)];
            p1y[z] = p1y[(z - 1)];
            p2x[z] = p2x[(z - 1)];
            p2y[z] = p2y[(z - 1)];
        }

        if (p1LeftDirection)
        {
            p1x[0] -= DOT_SIZE;
        }

        if (p1RightDirection)
        {
            p1x[0] += DOT_SIZE;
        }

        if (p1UpDirection)
        {
            p1y[0] -= DOT_SIZE;
        }

        if (p1DownDirection)
        {
            p1y[0] += DOT_SIZE;
        }

        if (p2LeftDirection)
        {
            p2x[0] -= DOT_SIZE;
        }

        if (p2RightDirection)
        {
            p2x[0] += DOT_SIZE;
        }

        if (p2UpDirection)
        {
            p2y[0] -= DOT_SIZE;
        }

        if (p2DownDirection)
        {
            p2y[0] += DOT_SIZE;
        }

        dots++;
    }

    private void checkCollision()
    {
//Remis:
        if (p1x[0] == p2x[0] && p1y[0] == p2y[0])
        {
            inGame = false;
            p1Dead = true;
            p2Dead = true;
        }

//O samego siebie:
        for (int z = dots; z > 0; z--)
        {
            if ((p1x[0] == p1x[z]) && (p1y[0] == p1y[z]))
            {
                inGame = false;
                p1Dead = true;
            }

            if ((p2x[0] == p2x[z]) && (p2y[0] == p2y[z]))
            {
                inGame = false;
                p2Dead = true;
            }
        }
//P1 w bandy:
        if (p1y[0] >= B_HEIGHT)
        {
            inGame = false;
            p1Dead = true;

        }

        if (p1y[0] < 0)
        {
            inGame = false;
            p1Dead = true;
        }

        if (p1x[0] >= B_WIDTH)
        {
            inGame = false;
            p1Dead = true;
        }

        if (p1x[0] < 0)
        {
            inGame = false;
            p1Dead = true;
        }
//P2 w bandy:
        if (p2y[0] >= B_HEIGHT)
        {
            inGame = false;
            p2Dead = true;

        }

        if (p2y[0] < 0)
        {
            inGame = false;
            p2Dead = true;
        }

        if (p2x[0] >= B_WIDTH)
        {
            inGame = false;
            p2Dead = true;
        }

        if (p2x[0] < 0)
        {
            inGame = false;
            p2Dead = true;
        }
//O siebie nawzajem:

        for (int z = dots; z > 0; z--)
        {
            if ((p1x[0] == p2x[z]) && (p1y[0] == p2y[z]))
            {
                inGame = false;
                p1Dead = true;
            }

            if ((p2x[0] == p1x[z]) && (p2y[0] == p1y[z]))
            {
                inGame = false;
                p2Dead = true;
            }
        }
    }

    public void endGame()
    {
        if (!inGame)
        {
            Menu.start.setEnabled(true);
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        if (inGame)
        {
            checkCollision();
            move();
            endGame();
        }
        repaint();
    }

    private class KeyboardSettings extends KeyAdapter
    {

        @Override
        public void keyPressed(KeyEvent e)
        {
            int key = e.getKeyCode();

            if ((key == KeyEvent.VK_LEFT) && (!p1RightDirection))
            {
                ImageIcon p1HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1leftmouth.png");
                p1Head = p1HeadLoader.getImage();
                p1LeftDirection = true;
                p1UpDirection = false;
                p1DownDirection = false;
            }

            if ((key == KeyEvent.VK_RIGHT) && (!p1LeftDirection))
            {
                ImageIcon p1HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1rightmouth.png");
                p1Head = p1HeadLoader.getImage();
                p1RightDirection = true;
                p1UpDirection = false;
                p1DownDirection = false;
            }

            if ((key == KeyEvent.VK_UP) && (!p1DownDirection))
            {
                ImageIcon p1HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1upmouth.png");
                p1Head = p1HeadLoader.getImage();
                p1UpDirection = true;
                p1RightDirection = false;
                p1LeftDirection = false;
            }

            if ((key == KeyEvent.VK_DOWN) && (!p1UpDirection))
            {
                ImageIcon p1HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p1downmouth.png");
                p1Head = p1HeadLoader.getImage();
                p1DownDirection = true;
                p1RightDirection = false;
                p1LeftDirection = false;
            }

            if ((key == KeyEvent.VK_A) && (!p2RightDirection))
            {
                ImageIcon p2HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2leftmouth.png");
                p2Head = p2HeadLoader.getImage();
                p2LeftDirection = true;
                p2UpDirection = false;
                p2DownDirection = false;
            }

            if ((key == KeyEvent.VK_D) && (!p2LeftDirection))
            {
                ImageIcon p2HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2rightmouth.png");
                p2Head = p2HeadLoader.getImage();
                p2RightDirection = true;
                p2UpDirection = false;
                p2DownDirection = false;
            }

            if ((key == KeyEvent.VK_W) && (!p2DownDirection))
            {
                ImageIcon p2HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2upmouth.png");
                p2Head = p2HeadLoader.getImage();
                p2UpDirection = true;
                p2RightDirection = false;
                p2LeftDirection = false;
            }

            if ((key == KeyEvent.VK_S) && (!p2UpDirection))
            {
                ImageIcon p2HeadLoader = new ImageIcon("D:\\Szkoła\\IntelliJ Workspace\\tron\\src\\main\\resources\\p2downmouth.png");
                p2Head = p2HeadLoader.getImage();
                p2DownDirection = true;
                p2RightDirection = false;
                p2LeftDirection = false;
            }
        }
    }
}

Klasa Menu:

/**
 * Created by HoulScream on 31.05.2017.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Menu extends JFrame
{

    static JButton start;
    private JButton exit;
    //private JLabel obraz;
    private JComboBox<String> difficulty;
    private String[] diffLevel = {"CHOICE 1", "CHOICE 2", "CHOICE 3", "CHOICE 4", "CHOICE 5"};

    public JPanel board = new Board();


    public Menu()
    {

        super("Tron");
        setSize(1625, 925);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setResizable(false);
        setLocation(new Point(10, 10));

        Insets insets = new Insets(1, 1, 1, 1);

        start = new JButton("Start");
        start.setMargin(insets);
        start.setFocusable(false);
        start.setBounds(50, 50, 80, 30);
        start.addActionListener(new Start());

        difficulty = new JComboBox<String>(diffLevel);
        difficulty.setFocusable(false);
        difficulty.setBounds(200, 50, 80, 30);
        //difficulty.addActionListener(new Difficulty());

        exit = new JButton("Zakończ");
        exit.setMargin(insets);
        exit.setFocusable(false);
        exit.setBounds(350, 50, 80, 30);
        exit.addActionListener(new Exit());

        JPanel board = new Board();
        board.setBounds(10, 87, 1600, 800);

        add(start);
        add(difficulty);
        add(exit);
        add(board);
        // add(obraz);

        setVisible(true);
    }

    private class Exit implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Board.timer.stop();
            dispose();
        }
    }

    private class Start implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            start.setEnabled(false);
            Board.timer.start();
        }
    }

 /*   private class Difficulty implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            start.setEnabled(false);
            Board.timer.start();
        }
    }*/

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                new Menu();
            }
        });
    }
}

Jeszcze raz serdecznie proszę Was o pomoc i z góry dziękuję. Pozdrawiam

0

Czy dobrze rozumiem, że chcesz wiedzieć, że użytkownik nacisnął nieaktywny przycisk i jakoś to obsłużyć? Tak nie możesz, to nieintuicyjne. Możesz natomiast zmienić nazwę przycisku, po pierwszym naciśnięciu. Jak źle zrozumiałem, to objaśnij :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1