(JAVA battleships) button/image grid works fine but when I add a second grid next to it they both get messed up -
i'm creating battleships program java. using gridlayout
want have player board in west panel , npc board in east panel (as other info , means of input in centre panel , south panel).
i started adding players board follows (full code can found @ bottom of post well):
public void placeplyrgrids() { plyrboard.add(cornergridlabel); //add empty grid top left for(n = 0; n < 10; n++) plyrboard.add(lettergridlabels[n]); //the first row first 10 letters of alphabet (y = 1; y < 11; y++) //for every (y) row... { (x = 0; x < 11; x++) //...add ten (x) grids make columns { if (x == 0) //to start of each row, add number (image jlabel) { plyrboard.add(numbergridlabels[numbercounter]); numbercounter++; } else //for rest of each row, add buttons { plyrboard.add(buttongrids[y-1][x-1]); } } } }
this worked fine.
then tried adding dealer board in 2 ways, none of seemed work, both got messed up, , seemed mess player board had seemed work fine on own.
1: placing both boards simultaneously in same method.
public void placeboards() { plyrboard.add(cornergridlabel); //add empty grid top left npcboard.add(cornergridlabel); for(n = 0; n < 10; n++) plyrboard.add(lettergridlabels[n]); //the first row first 10 letters of alphabet npcboard.add(lettergridlabels[n]); (y = 1; y < 11; y++) //for every (y) row... { (x = 0; x < 11; x++) //...add ten (x) grids make columns { if (x == 0) //to start of each row, add number (image jlabel) { plyrboard.add(numbergridlabels[numbercounter]); npcboard.add(numbergridlabels[numbercounter]); numbercounter++; } else //for rest of each row, add buttons { plyrboard.add(buttongrids[y-1][x-1]); npcboard.add(emptygridlabel); } } } }
2: placing both each in different method (used in combination pladeplyrgrids
method @ top)
public void placenpcgrids() { npcboard.add(cornergridlabel); for(n = 0; n < 10; n++) npcboard.add(lettergridlabels[n]); (y = 1; y < 11; y++) { (x = 0; x < 11; x++) { if (x == 0) { npcboard.add(numbergridlabels[numbercounter]); numbercounter++; } else { npcboard.add(emptygridlabel); } } } }
my full code:
import java.awt.container; import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; public class battleships { public static void main(string[] args) { interface win = new interface (); } } class interface extends jframe implements actionlistener { //identify variables int n; int numbercounter = 0; int y; int x; // lengths of various ships in game //add image components //coordinate axes , empty grids imageicon emptygrid = new imageicon("emptygrid.png"); imageicon cornergrid = new imageicon("cornergrid.png"); imageicon [] numbergrids = new imageicon[11]; { (n = 0; n < 11; n ++) numbergrids[n] = new imageicon("grid" + (n + 1) + ".png"); } imageicon [] lettergrids = new imageicon [11]; { (n = 0; n < 11; n ++) lettergrids[n] = new imageicon("grida" + (n + 1)+ ".png"); } //ship parts //clickable ships (for placement) imageicon fullbattleship = new imageicon("fullbattleship.png"); imageicon fullcruiser = new imageicon("fullcruiser.png"); imageicon fullpatrolboat1 = new imageicon("fullpatrolboat.png"); imageicon fullpatrolboat2 = new imageicon("fullpatrolboat.png"); //jlabels jlabel cornergridlabel = new jlabel(cornergrid); jlabel [] numbergridlabels = new jlabel[10]; { //the first 11 grids empty grid followed letters a-j (n = 0; n < 10; n++) { numbergridlabels[n] = new jlabel(numbergrids[n]); } } jlabel [] lettergridlabels = new jlabel [10]; { (n = 0; n < 10; n ++) { lettergridlabels[n] = new jlabel (lettergrids[n]); } } jlabel emptygridlabel = new jlabel(emptygrid); jbutton [][] buttongrids = new jbutton [10][10]; { (y = 0; y < 10; y++) { (x = 0; x < 10; x++) { buttongrids[x][y] = new jbutton(emptygrid); buttongrids[x][y].setpreferredsize(new dimension(50,50)); } } } jlabel [] npcgrids = new jlabel[121]; //grid placements dealer board { (n = 0; n < 121; n ++) npcgrids[n] = new jlabel (emptygrid); } jlabel [] clickableboats = new jlabel [4]; { clickableboats[0] = new jlabel (fullbattleship); clickableboats[1] = new jlabel (fullcruiser); clickableboats[2] = new jlabel (fullpatrolboat1); clickableboats[3] = new jlabel (fullpatrolboat2); } jbutton [] plyrclickablegrids = new jbutton [100]; { (n = 0; n < 99; n++); plyrclickablegrids[n] = new jbutton(emptygrid); } //add interface components jtextarea playerinformation = new jtextarea(20,5); jscrollpane textpane1 = new jscrollpane (playerinformation); jbutton playturnbtn = new jbutton ("play turn"); //add jpanels jpanel plyrboard = new jpanel (new gridlayout(11,11)); jpanel infopanel = new jpanel(new gridlayout(1,1)); jpanel npcboard = new jpanel(new gridlayout(11,11)); jpanel inputpanel = new jpanel(new gridlayout(1,10)); public interface () { super ("battleships"); setsize (1367,729); setdefaultcloseoperation (jframe.exit_on_close); //set background color container contentarea = getcontentpane(); //set each panel "opaque", background becomes visible plyrboard.setopaque(false); npcboard.setopaque(false); inputpanel.setopaque(false); setvisible (true); playerinformation.seteditable(false); //add panels different compass points on content area contentarea.add("west", plyrboard); contentarea.add("center", infopanel); contentarea.add("east", npcboard); contentarea.add("south", inputpanel); //add imagelabels , buttons panels placeplyrgrids(); numbercounter = 0; placenpcgrids(); infopanel.add(playerinformation); (n = 0; n < 4; n++) { inputpanel.add(clickableboats[n]); } inputpanel.add(playturnbtn); // todo vanity //button format playturnbtn.setpreferredsize(new dimension(1, 141)); } public void placeplyrgrids() { plyrboard.add(cornergridlabel); //add empty grid top left //npcboard.add(cornergridlabel); for(n = 0; n < 10; n++) plyrboard.add(lettergridlabels[n]); //the first row first 10 letters of alphabet //npcboard.add(lettergridlabels[n]); (y = 1; y < 11; y++) //for every (y) row... { (x = 0; x < 11; x++) //...add ten (x) grids make columns { if (x == 0) //to start of each row, add number (image jlabel) { plyrboard.add(numbergridlabels[numbercounter]); //npcboard.add(numbergridlabels[numbercounter]); numbercounter++; } else //for rest of each row, add buttons { plyrboard.add(buttongrids[y-1][x-1]); //npcboard.add(emptygridlabel); } } } } public void placenpcgrids() { npcboard.add(cornergridlabel); for(n = 0; n < 10; n++) npcboard.add(lettergridlabels[n]); (y = 1; y < 11; y++) { (x = 0; x < 11; x++) { if (x == 0) { npcboard.add(numbergridlabels[numbercounter]); numbercounter++; } else { npcboard.add(emptygridlabel); } } } } public void actionperformed(actionevent e) { } }
as i'm sure understand confused , don't understand @ why happening. after all, first method works fine on own. why shouldn't second 1 , why destroying each other?
any suggestions or tips put me in right direction appreciated.
edit:
solution:
import java.awt.container; import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; public class battleships { public static void main(string[] args) { interface win = new interface (); } } class interface extends jframe implements actionlistener { //identify variables int n; int numbercounter = 0; int y; int x; // lengths of various ships in game //add image components //coordinate axes , empty grids imageicon emptygrid = new imageicon("emptygrid.png"); imageicon cornergrid = new imageicon("cornergrid.png"); imageicon [] numbergrids = new imageicon[11]; { for (n = 0; n < 11; n ++) numbergrids[n] = new imageicon("grid" + (n + 1) + ".png"); } imageicon [] lettergrids = new imageicon [11]; { for (n = 0; n < 11; n ++) lettergrids[n] = new imageicon("grida" + (n + 1)+ ".png"); } //ship parts //clickable ships (for placement) imageicon fullbattleship = new imageicon("fullbattleship.png"); imageicon fullcruiser = new imageicon("fullcruiser.png"); imageicon fullpatrolboat1 = new imageicon("fullpatrolboat.png"); imageicon fullpatrolboat2 = new imageicon("fullpatrolboat.png"); //jlabels jlabel plyrcornergridlabel = new jlabel(cornergrid); jlabel [] plyrnumbergridlabels = new jlabel[10]; { //the first 11 grids empty grid followed letters a-j for (n = 0; n < 10; n++) { plyrnumbergridlabels[n] = new jlabel(numbergrids[n]); } } jlabel [] plyrlettergridlabels = new jlabel [10]; { for (n = 0; n < 10; n ++) { plyrlettergridlabels[n] = new jlabel (lettergrids[n]); } } jlabel npccornergridlabel = new jlabel(cornergrid); jlabel [] npcnumbergridlabels = new jlabel[10]; { //the first 11 grids empty grid followed letters a-j for (n = 0; n < 10; n++) { npcnumbergridlabels[n] = new jlabel(numbergrids[n]); } } jlabel [] npclettergridlabels = new jlabel [10]; { for (n = 0; n < 10; n ++) { npclettergridlabels[n] = new jlabel (lettergrids[n]); } } jlabel[][] emptygridlabels = new jlabel [10][10]; { for (y = 0; y < 10; y++) { for (x = 0; x < 10; x++) { emptygridlabels[x][y] = new jlabel(emptygrid); } } } jbutton [][] buttongrids = new jbutton [10][10]; { for (y = 0; y < 10; y++) { for (x = 0; x < 10; x++) { buttongrids[x][y] = new jbutton(emptygrid); buttongrids[x][y].setpreferredsize(new dimension(50,50)); } } } jlabel [] npcgrids = new jlabel[121]; //grid placements dealer board { for (n = 0; n < 121; n ++) npcgrids[n] = new jlabel (emptygrid); } jlabel [] clickableboats = new jlabel [4]; { clickableboats[0] = new jlabel (fullbattleship); clickableboats[1] = new jlabel (fullcruiser); clickableboats[2] = new jlabel (fullpatrolboat1); clickableboats[3] = new jlabel (fullpatrolboat2); } jbutton [] plyrclickablegrids = new jbutton [100]; { for (n = 0; n < 99; n++); plyrclickablegrids[n] = new jbutton(emptygrid); } //add interface components jtextarea playerinformation = new jtextarea(20,5); jscrollpane textpane1 = new jscrollpane (playerinformation); jbutton playturnbtn = new jbutton ("play turn"); //add jpanels jpanel plyrboard = new jpanel (new gridlayout(11,11)); jpanel infopanel = new jpanel(new gridlayout(1,1)); jpanel npcboard = new jpanel(new gridlayout(11,11)); jpanel inputpanel = new jpanel(new gridlayout(1,10)); public interface () { super ("battleships"); setsize (1367,729); setdefaultcloseoperation (jframe.exit_on_close); //set background color container contentarea = getcontentpane(); //set each panel "opaque", background becomes visible plyrboard.setopaque(false); npcboard.setopaque(false); inputpanel.setopaque(false); setvisible (true); playerinformation.seteditable(false); playturnbtn.setpreferredsize(new dimension(1, 141)); //add panels different compass points on content area contentarea.add("west", plyrboard); contentarea.add("center", infopanel); contentarea.add("east", npcboard); contentarea.add("south", inputpanel); //add imagelabels , buttons panels infopanel.add(playerinformation); for (n = 0; n < 4; n++) { inputpanel.add(clickableboats[n]); } inputpanel.add(playturnbtn); placeplyrgrids(); numbercounter = 0; placenpcgrids(); // todo vanity //button format } public void placeplyrgrids() { plyrboard.add(plyrcornergridlabel); //add empty grid top left //npcboard.add(cornergridlabel); for(n = 0; n < 10; n++) plyrboard.add(plyrlettergridlabels[n]); //the first row first 10 letters of alphabet //npcboard.add(npclettergridlabels[n]); for (y = 1; y < 11; y++) //for every (y) row... { for (x = 0; x < 11; x++) //...add ten (x) grids make columns { if (x == 0) //to start of each row, add number (image jlabel) { plyrboard.add(plyrnumbergridlabels[numbercounter]); //npcboard.add(npcnumbergridlabels[numbercounter]); numbercounter++; } else //for rest of each row, add buttons { plyrboard.add(buttongrids[y-1][x-1]); //npcboard.add(emptygridlabel); } } } } public void placenpcgrids() { npcboard.add(npccornergridlabel); for(n = 0; n < 10; n++) npcboard.add(npclettergridlabels[n]); for (y = 1; y < 11; y++) { for (x = 0; x < 11; x++) { if (x == 0) { npcboard.add(npcnumbergridlabels[numbercounter]); numbercounter++; } else { npcboard.add(emptygridlabels[x-1][y-1]); } } } } public void actionperformed(actionevent e) { } }
the issue in code referring same object, e.g.
public void placeboards() { //both player board , npc board going have same cornergirdlabel, should creating 2 separate instances of plyrboard.add(cornergridlabel); //add empty grid top left npcboard.add(cornergridlabel); //rest of code }
all way through code, keep using reference same objects, not going when want build 2 boards own grids!
it might better, readability separate board building different classes , have them create own instances of these objects, rather trying fit in 1 class main method. have @ best design practice documents. example, have nice tips here
Comments
Post a Comment