Page 1 of 1

Java Newbie, Please Help - 'Renaming' a JButton

Posted: 24 Jul 2010, 10:59
by pienkie
Hi guys,

I'm trying to make a Scrabble game for a school project, but I'm stuck with a silly issue. The idea is that the board is made up out of 15x15 JButtons, and then this button is replaced with one sporting an image representing the letter the player chose to put there. Now I am getting a NullPointerException every time I try to change the JButton in any manner. I have tried different ways of accomplishing this, but I have not succeeded.

Any help would be GREATLY appreciated, as I'm seriously running out of time.
Thanks

(Please bear in mind that the program is in very early stages, and I am very new to java :wink: )

Here is my code:

Code: Select all

package scrabble;

public class Main {

    public static void main(String[] args) {
        Board board = new Board(true);
    }
}

Code: Select all

package scrabble;

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

public class Player implements ActionListener {

    private JFrame frame;
    private Container con;
    private JButton[] btnHand = new JButton[7];
    private String[] alpha = new String[26];
    private int btnX, btnY;
    private Board board = new Board(false);

    public Player() {
        assignAplhabet();
    }

    private void assignAplhabet() {
        int count = 0;
        for (int asc = 65; asc < 91; asc++) {
            alpha[count] = "" + (char) asc;
            count++;
        }
    }

    public void start() {
        frame = new JFrame("PLAYER 1");
        frame.setSize(400, 100);
        frame.setLocation(0, 0);
        con = frame.getContentPane();
        con.setLayout(null);
        con.setBackground(Color.LIGHT_GRAY);
        for (int k = 0; k < 7; k++) {
            int rndm = (int) (Math.random() * 26);
            // btnP1[k] = new JButton(alpha[rndm]);
            btnHand[k] = new JButton(new ImageIcon( alpha[rndm] + ".gif"));
            btnHand[k].setBounds(10 + k * 52, 10, 47, 47);
            con.add(btnHand[k]);
            // btnHand[k].addActionListener(this);
        }
        frame.setVisible(true);
    }

    public void boardBtnClicked(int x, int y, int w, int h) {
        btnX = x;
        btnY = y;
        frame.dispose();
        frame.setLocation((w / 2) - 200, (h / 2) - 100);
        frame.setTitle("Choose which letter to place.");
        frame.show();
        for (int r = 0; r < 7; r++) {
            btnHand[r].addActionListener(this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        String text = (String) e.getActionCommand();
        board.placeLetter(btnX, btnY, text);
    }
}
The error message is in here:

Code: Select all

package scrabble;

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

public class Board implements ActionListener {

    private JFrame boardFrame;
    private JLabel title;
    private Container con;
    private JButton[][] btnBlock = new JButton[15][15];
    private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private Gameplay gameplay;
    private Color defaultColor;
    private Player p1;

    public Board(boolean first) {
        if (first) {
            createBoardFrame();
            createBoardBtns();
            //gameplay = new Gameplay();
            p1 = new Player();
            p1.start();
        }

    }

    private void createBoardFrame() {
        boardFrame = new JFrame("| SCRABBLE |");
        boardFrame.setSize(screenSize.width, screenSize.height);
        boardFrame.setLocation(0, 0);
        boardFrame.setVisible(true);
        boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con = boardFrame.getContentPane();
        con.setLayout(null);
        con.setBackground(Color.CYAN);
    }

    private void createBoardBtns() {
        int wide = screenSize.width;
        int high = screenSize.height;

        for (int y = 0; y < 15; y++) {
            for (int x = 0; x < 15; x++) {
                btnBlock[x][y] = new JButton("");
                btnBlock[x][y].setBounds(((wide - 780) / 2) + (x * 52), ((high - 780) / 2)
                        + (y * 52), 47, 47);
                btnBlock[x][y].addActionListener(this);
                con.add(btnBlock[x][y]);
            }
        }
        btnBlock[7][7].setBackground(Color.RED);
        title = new JLabel("SCRABBLE", JLabel.CENTER);
        title.setBounds((wide / 2) - 375, 0, 750, 50);
        title.setFont(new Font("Serif", Font.BOLD, 48));
        title.setForeground(Color.BLACK);
        con.add(title);
        boardFrame.setVisible(true);
        defaultColor = btnBlock[0][0].getBackground();
    }

    public void placeLetter(int xx, int yy, String letter) {
        con.remove(btnBlock[xx][yy]);  // <<<<<---- THE ERROR MESSAGE POINTS HERE
        btnBlock[xx][yy] = new JButton(new ImageIcon(letter + ".gif"));
        btnBlock[xx][yy].setBounds(((screenSize.width - 780) / 2) + (xx * 52),
                ((screenSize.height - 780) / 2) + (yy * 52), 47, 47);
        btnBlock[xx][yy].addActionListener(this);
        con.add(btnBlock[xx][yy]);
    }

    public void actionPerformed(ActionEvent e) {
        for (int x = 0; x < 15; x++) {
            for (int y = 0; y < 15; y++) {
                if (e.getSource() == btnBlock[x][y]) {
                    p1.boardBtnClicked(x, y, screenSize.width, screenSize.height);
                }
            }
        }
    }
}

Re: Java Newbie, Please Help - 'Renaming' a JButton

Posted: 25 Jul 2010, 18:52
by Bladerunner

Code: Select all

con.remove(btnBlock[xx][yy]);
And what are the values for xx and yy when you get this error message? What is the error message?

Re: Java Newbie, Please Help - 'Renaming' a JButton

Posted: 26 Jul 2010, 09:31
by rustypup

Code: Select all

private Board board = new Board(false);
this is from your Player class, and it is also the reason for the error... this constructor does not set the board up or retain reference to it's content pane... it is also not the board being rendered and it carries it's own array of buttons...

if i could offer some advice, don't hot-swap buttons... rather change their values... less overhead and fiddling on the fly.

better yet, dump the buttons in favour of an MVC design - you can use custom rendering and hit-detection to achieve the same result with far less overhead...

also consider replacing the player-instanced JFrames with lightweight components.... your pop-ups aren't necessarily disposed of until the next GC run... (even a JDialog would be preferable, given that it has inbuilt modal behaviour).

to avoid getting bogged down in connecting the Player pop-up and the board, consider what it is you expect the Player pop-up to return... a simple Observable/Observer setup will achieve the same ends with much less hassle...