Java Newbie, Please Help - 'Renaming' a JButton

Get help on programming - C++, Java, Delphi, etc.
Post Reply
pienkie
Registered User
Posts: 1168
Joined: 17 Apr 2007, 02:00
Contact:

Java Newbie, Please Help - 'Renaming' a JButton

Post 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);
                }
            }
        }
    }
}
AMD Tri-Core 720BE @ 3.5Ghz
Biostar TA790GX A3+
4gig OCZ Platinum DDR3-1333
ATI HD4890 @ 1050Mhz Mem / 900Mhz Core

3DMark06: 16 529
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

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

Post 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?
If I weren't insane: I couldn't be so brilliant! - The Joker
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

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

Post 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...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Post Reply