Hi, first time poster, and fairly new to java swing.Im trying to make a scrabble game and am stuck while trying to place a letter on the board, or even removing one for that matter. The playing board is just an array of JButtons all derived from a ScrabbleButton object, some are subclassed for double word scores or player tiles. The problem is, I cant place a tile on the board by overwriting one of the buttons in the array with a new ScrabbleButton object
here is the code for the board being initialised
public void buildBoard()
{
layout= new GridLayout(15,15);
board.setLayout(layout);
for(int i=0;i<button.length;i++)
{
button[i] = new ScrabbleButton();
}
//top left double word placement
button[16]= new DoubleWordButton();
button[32]= new DoubleWordButton();
button[48]= new DoubleWordButton();
button[64]= new DoubleWordButton();
//top right double word placement
button[28]= new DoubleWordButton();
button[42]= new DoubleWordButton();
button[56]= new DoubleWordButton();
button[70]= new DoubleWordButton();
//lower left double word placement
button[196]= new DoubleWordButton();
button[182]= new DoubleWordButton();
button[168]= new DoubleWordButton();
button[154]= new DoubleWordButton();
//lower right double word placement
button[208]= new DoubleWordButton();
button[192]= new DoubleWordButton();
button[176]= new DoubleWordButton();
button[160]= new DoubleWordButton();
//Manual Placement of Triple Word Scores
button[0]= new TripleWordButton();
button[14]= new TripleWordButton();
button[105]= new TripleWordButton();
button[119]= new TripleWordButton();
button[210]= new TripleWordButton();
button[224]= new TripleWordButton();
button[7]= new TripleWordButton();
button[217]= new TripleWordButton();
button[112]= new CenterButton();
//placement of double letter buttons
button[3]= new DoubleLetterButton();
button[11]= new DoubleLetterButton();
button[36]= new DoubleLetterButton();
button[38]= new DoubleLetterButton();
button[45]= new DoubleLetterButton();
button[52]= new DoubleLetterButton();
button[59]= new DoubleLetterButton();
button[92]= new DoubleLetterButton();
button[96]= new DoubleLetterButton();
button[98]= new DoubleLetterButton();
button[102]= new DoubleLetterButton();
button[108]= new DoubleLetterButton();
button[116]= new DoubleLetterButton();
button[122]= new DoubleLetterButton();
button[126]= new DoubleLetterButton();
button[128]= new DoubleLetterButton();
button[132]= new DoubleLetterButton();
button[165]= new DoubleLetterButton();
button[179]= new DoubleLetterButton();
button[172]= new DoubleLetterButton();
button[186]= new DoubleLetterButton();
button[188]= new DoubleLetterButton();
button[213]= new DoubleLetterButton();
button[221]= new DoubleLetterButton();
//placement of triple letter buttons
button[20]= new TripleLetterButton();
button[24]= new TripleLetterButton();
button[76]= new TripleLetterButton();
button[80]= new TripleLetterButton();
button[84]= new TripleLetterButton();
button[88]= new TripleLetterButton();
button[136]= new TripleLetterButton();
button[140]= new TripleLetterButton();
button[144]= new TripleLetterButton();
button[148]= new TripleLetterButton();
button[200]= new TripleLetterButton();
button[204]= new TripleLetterButton();
button[207]= new TripleLetterButton();
for(int i=0;i<button.length;i++)
{
button[i].addActionListener(this);
}
for(int i=0;i<button.length;i++){
board.add(button[i]);
}
at the moment i have a separate button to trigger a change, here is the listener:
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== actionButton)
{
this.button[7]= null;
this.button[7]= new PlayerTile("Z");
this.validate();
this.repaint();
}
}
thats just a snippet but its the spot where i think im going wrong. The button listener is working because i can use it to alter border colour etc. Until i solve the problem Im only trying to replace the 8th square in the scrabble board. Ive seen similar posts that solve the problem with the validate() and repaint() functions but no joy! Any help would be appreciated because it has me stumped and i think its just some step ive overlooked on the swing side of things.
Thanks :)