java - I am having trouble calling code from a class -
so new java , working on graphical user interface simple game of minesweeper. had code integrated 1 class , instantiated panel , frame in same class, professor insisted place frame , panel in separate classes. stuck on how implement panel in frame. attempted creating instance of class in main method, when ran program displayed empty frame. before set separate classes code ran panel not being added frame. missing in panel? here code far:
import java.awt.event.inputevent; import java.awt.*; import java.util.random; import javax.swing.*; public class minesweeper { public static void main(string[] args) { frame frame = new frame(); frame.getcontentpane().add(new panel()); frame.setvisible(true); } }
this frame.
class frame extends jframe { public frame() { settitle("minesweeper"); setextendedstate(jframe.maximized_both); setlocationrelativeto(null); setdefaultcloseoperation(exit_on_close); } }
and panel class having trouble with.
class panel extends jpanel { public panel() { super(); this.setlayout(new borderlayout()); add(getjpanel()); add(getjcontentpane()); } private int columns = 8; private int rows = 8; boolean jbombs[][] = new boolean[rows][columns]; boolean jshown[][] = new boolean[rows][columns]; int jcells[][] = new int[rows][columns]; private int currx, curry = 0; jtogglebutton jbuttons[] = new jtogglebutton[columns*rows]; private jpanel jpanel = null; private jtoolbar jtoolbar = null; private jpanel jcontentpane = null; private jbutton jbtnnewgame = null; private jprogressbar jprogressbar = null; private jpanel getjpanel() { if (jpanel == null) { jpanel = new jpanel(); jpanel.setlayout(new borderlayout()); jpanel.add(getjcontentpane(), borderlayout.center); jpanel.add(getjprogressbar(), borderlayout.south); } return jpanel; } private jpanel getjcontentpane() { if (jcontentpane == null) { gridlayout gridlayout = new gridlayout(); gridlayout.setrows(rows); gridlayout.setcolumns(columns); jcontentpane = new jpanel(); jcontentpane.setlayout(gridlayout); buildboard(); } return jcontentpane; } private void buildboard() { if(jprogressbar != null) { jprogressbar.setvalue(0); } jcontentpane.removeall(); int = 0; for(int x = 0; x < rows; x++) { for(int y = 0; y < columns; y++) { currx = x; curry = y; random randbomb = new random(); jbombs[x][y] = randbomb.nextboolean() && randbomb.nextboolean() && randbomb.nextboolean(); jbuttons[i] = new jtogglebutton("?"); jbuttons[i].addmouselistener(new java.awt.event.mouseadapter(){ public void mousereleased(java.awt.event.mouseevent e) { if(e.getmodifiers() == inputevent.button3_mask) { markcell(e); } else if(e.getmodifiers() == inputevent.button1_mask) { showcell(e); } } }); jcontentpane.add(jbuttons[i]); i++; } } for(int x = 0; x < rows; x++) { for(int y = 0; y < columns; y++) { jcells[x][y] = bombcount(x, y); jshown[x][y] = false; } } jcontentpane.setenabled(true); this.repaint(); this.validate(); } private jprogressbar getjprogressbar() { if (jprogressbar == null) { jprogressbar = new jprogressbar(); jprogressbar.setmaximum(columns * rows); } return jprogressbar; } private void showallbombs() { for(int x = 0; x < rows; x++) { for(int y = 0; y < columns; y++) { if(jbombs[x][y] == true) { jtogglebutton jbutton = findbutton(x,y); if(jbutton.isenabled()) { jprogressbar.setvalue(jprogressbar.getvalue() + 1); } jbutton.settext("x"); jbutton.setselected(true); jbutton.setenabled(false); } } } } private void clearcells(int x, int y) { if(inbounds(x, y)) { if(!jshown[x][y] && jbombs[x][y] == false) { jshown[x][y] = true; jtogglebutton jbutton = findbutton(x,y); if(jcells[x][y] > 0) { jbutton.settext(integer.tostring(jcells[x][y])); } else { jbutton.settext(""); } if(jbutton.isenabled()) { jprogressbar.setvalue(jprogressbar.getvalue() + 1); } jbutton.setselected(true); jbutton.setenabled(false); if(jcells[x][y] == 0) { for(int r = -1; r <= 1; r++) { for(int c = -1; c <= 1; c++) { clearcells(x + r, y + c); } } } } } } private boolean inbounds(int x, int y) { return 0 <= x && x < jcells.length && 0 <= y && y < jcells[x].length; } private boolean isbomb(jtogglebutton jbutton) { int = 0; for(int x = 0; x < rows; x++) { for(int y = 0; y < columns; y++) { if(jbutton == jbuttons[i]) { currx = x; curry = y; return jbombs[x][y]; } i++; } } return false; } private void disableboard() { for(int x = 0; x < rows; x++) { for(int y = 0; y < columns; y++) { jtogglebutton jbutton = findbutton(x,y); jbutton.setenabled(false); } } } private jtogglebutton findbutton(int x, int y) { return jbuttons[(x*rows+y)]; } private void showcell(java.awt.event.mouseevent e) { jtogglebutton jbutton = (jtogglebutton)e.getsource(); if(jbutton.isenabled()) { jprogressbar.setvalue(jprogressbar.getvalue() + 1); jbutton.setenabled(false); if(isbomb(jbutton)) { showallbombs(); jbutton.setenabled(false); joptionpane.showmessagedialog(null,"you lost " + math.round((jprogressbar.getpercentcomplete() * 100)) + "% through.", "you lost!", joptionpane.information_message); disableboard(); system.exit(0); } else { if(jcells[currx][curry] > 0) { jbutton.settext(integer.tostring(jcells[currx][curry])); } else if(jcells[currx][curry] == 0) { clearcells(currx, curry); } } } } private int bombcount(int x, int y) { int bombcount = 0; for(int r = -1; r <= 1; r++) { for(int c = -1; c <= 1; c++) { int newx = x + r; int newy = y + c; if(inbounds(newx, newy)) { if(jbombs[newx][newy] == true) { bombcount++; } } } } return bombcount; } private void markcell(java.awt.event.mouseevent e) { jtogglebutton jbutton = (jtogglebutton)e.getsource(); if(jbutton.isenabled()) { if(jbutton.gettext() != "!") { jbutton.settext("!"); } else { jbutton.settext(""); } } } }
you never call add (or generate) content of panel
class.
in panel
constuctor, try calling getjpanel
, getjcontentpane
, add panels panel
class.
also, this...
panel panel = new panel(); frame frame = new frame(); frame.getcontentpane().add(new panel());
makes no sense, you're creating 2 instances panel
, using one. use
public static void main(string[] args) { frame frame = new frame(); frame.getcontentpane().add(new panel()); //... }
instead.
also, instead of using...
dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); int width = screensize.width; int height = screensize.height; setsize(width, height);
which result in frame been displayed beneath other os elements (ie task bar), should use...
setextendedstate(jframe.maximized_both);
instead.
Comments
Post a Comment