EnderShadow
EnderShadow

Reputation: 105

JButton disappears when GUI is clicked

I'm making checkers in java and the "New Game" button disappears when I click on the GUI. It reappears when I hover my mouse over it, but will disappear again if I click the GUI. Do you know what I did wrong/am doing incorrectly?

public void setFrame()
    {
        boardSize  = 10;
        squareSize = 50;
        int imageSize = boardSize * squareSize;       
        image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
        imageIcon = new ImageIcon(image);
        jLabel = new JLabel(imageIcon);
        button = new JButton("New Game");
        button.setFocusable(false);
        button.setBounds(375, 5, 100, 20);
        pnl = new JPanel();
        pnl.setBounds(400, 10, 200, 100);
        pnl.setLayout(null);
        pnl.add(button);

        jFrame = new JFrame("Checker Board");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.add(jLabel, BorderLayout.CENTER);
        jFrame.add(pnl);
        jFrame.setSize(506, 558);
        jFrame.setResizable(false);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

        jFrame.validate();
    }

    /**
     * Paint the checker board onto the Image.
     */
    public void paint()
    {
        Graphics graphics = jFrame.getGraphics();

        pnl.paint(graphics);
        button.paint(graphics);

        graphics.setColor(Color.black);
        Font font = new Font("Score", Font.BOLD, 20);
        graphics.setFont(font);
        graphics.drawString("Score: ", 150, 47);
        graphics.drawString("Turn: ", 20, 47);
        graphics.setFont(font.deriveFont(0, 16.0F));
        graphics.drawString("Red: " + Game.score.getScoreRed() + "    Black: " + Game.score.getScoreBlack(), 230, 47);
        graphics.drawString((Game.redTurn ? "Red" : "Black"), 80, 47);

        // paint a red  board
        graphics.setColor(Color.red);
        graphics.fillRect(xShift, zShift, boardSize * squareSize, boardSize * squareSize);

        // paint the black squares
        graphics.setColor(Color.black);
        for (int row = 0; row < boardSize; row++)
        {
            for (int col = row % 2; col < boardSize; col += 2)
            {
                graphics.fillRect( row * squareSize + xShift, col * squareSize + zShift, squareSize, squareSize );
            }
        }

        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                if(Game.board.pieces[i][j] != null)
                {
                    Color pieceColor = Game.board.pieces[i][j].getColor().equals(EnumTeam.BLACK) ? Color.gray : Color.pink;
                    graphics.setColor(pieceColor);
                    graphics.fillOval((i * 50) + 10 + xShift, (j * 50) + 10 + zShift, 30, 30);
                    if(Game.board.pieces[i][j].isKing())
                    {
                        pieceColor = Game.board.pieces[i][j].getColor().equals(EnumTeam.BLACK) ? Color.darkGray : Color.magenta;
                        graphics.setColor(pieceColor);
                        graphics.fillOval((i * 50) + 20 + xShift, (j * 50) + 20 + zShift, 10, 10);
                    }
                }
            }
        }

        graphics.setColor(Color.cyan);
        drawRect(graphics, Game.board.getSelectedX(), Game.board.getSelectedZ(), 5);
    }

Upvotes: 0

Views: 2479

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

Do not, ever use Graphics graphics = jFrame.getGraphics(); (or getGraphics generally)! This is not how custom painting is done in Swing. The mere fact that you'v then cleared the graphics context is your core problem.

All painting should be done within the context of the painting API, preferably by overriding paintComponent from any component that extends JComponent (I, personally, prefer JPanel)

Create a custom component and use it it perform you custom painting. Layout it out with your other components on the frame.

Set Performing Custom Painting and Painting in AWT and Swing for more details about how painting works in Swing.

A MouseListener is not really an appropriate listener to use for buttons, a better choice would be to use a ActionListener which takes into account mouse clicks and keyboard events...

See How to write an Action Listener and How to use buttons for more details...

Upvotes: 3

Related Questions