i trying build gui game class in java. keep getting nullpointerexception , don't know why, thought passing object "cplayer" applet passed jpanel portion (where should compare cplayer hits or misses) have no idea doing wrong. there other way pass cplayer object applet , jpanel?
public class gameplay { public final static computerboard cplayer = new computerboard(); public static void main(string args){ battleshipapplet play = new battleshipapplet(); play.setboard(cplayer); } } public class battleshipapplet extends japplet { private final jbutton playbutton = new jbutton("play"); private final jlabel msgbar = new jlabel("click play start game"); private boardpanel panel; private computerboard game; public battleshipapplet(){ playbutton.addactionlistener(this::playbuttonclicked); } public void init(){ configuregui(); } private void configuregui(){ setlayout(new borderlayout()); jpanel buttons = new jpanel(new flowlayout(flowlayout.left)); buttons.setborder(borderfactory.createemptyborder(0,5,0,0)); buttons.add(playbutton); add(buttons, borderlayout.north); msgbar.setborder(borderfactory.createemptyborder(10,10,5,5)); add(createboardpanel(), borderlayout.center); add(msgbar, borderlayout.south); } private boardpanel createboardpanel(){ panel = new boardpanel(game); return panel; } private void displaymessage(string msg){ msgbar.settext(msg); } private void playbuttonclicked(actionevent event){ displaymessage("game has started!"); } void setboard(computerboard b){ game = b; } } public class boardpanel extends jpanel { private static final int rows = 10; private static final int cell_width = 28; private static final int pad = 20; private static final color grid_color = color.blue; private static final color circle_color_hit = color.red; private static final color circle_color_miss = color.white; private static final color text_color = color.blue; private static final int sml_gap = 2; private boolean[][] grid = new boolean[rows][rows]; private int counter; private int x, y; private boolean gameboardpiece; computerboard gameboard; public boardpanel(computerboard b) { addmouselistener((mouselistener) new mymouse()); counter =0; gameboard = b; gameboardpiece = false; } public void reset() { grid = new boolean[rows][rows]; // fills grid false repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); // draws grid g.setcolor(grid_color); (int = 0; <= rows; i++) { int x1 = pad + * cell_width; int y1 = pad; int x2 = x1; int y2 = pad + cell_width * rows; g.drawline(x1, y1, x2, y2); g.drawline(y1, x1, y2, x2); } // iterate through grid boolean array // draw circles if grid value true. int w = cell_width - 2 * sml_gap; // width of circle draw int h = w; // nested loop go through grid array (int r = 0; r < grid.length; r++) { (int c = 0; c < grid[r].length; c++) { if (grid[r][c]) { //if hit if(gameboardpiece == false){ g.setcolor(circle_color_hit); } //shot miss else{ g.setcolor(circle_color_miss); } int x = pad + c * cell_width + sml_gap; int y = pad + r * cell_width + sml_gap; g.filloval(x, y, w, h); } } } //states number of shots in game g.setcolor(text_color); g.drawstring("shots: "+counter, 305,300 ); } private class mymouse extends mouseadapter { public void mousepressed(mouseevent e) { x = e.getx(); y = e.gety(); if (x < pad || y < pad ) { // clicked above or right of grid return; } int r = (y - pad) / cell_width; int c = (x - pad) / cell_width; // if clicked right or below grid. if (r >= rows || c >= rows) { return; } if(gameboard.matchboard(r,c) == 0){ gameboardpiece = true; } if(gameboard.matchboard(r,c) == 1){ gameboardpiece = false; } counter++; grid[r][c] = true; repaint(); } } }
the error message receive is:
exception in thread "awt-eventqueue-1" java.lang.nullpointerexception @ battleship.boardpanel$mymouse.mousepressed(boardpanel.java:113) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$500(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
the life cycle of applet different life cycle of application. if applet run in environment applet run in, main()
method not called. after context calls constructor of applet, life cycle begin call applet's init()
method.
thus following work better.
public void init(){ computerboard cplayer = new computerboard(); setboard(cplayer); configuregui(); }
or perhaps doing away setboard
method altogether , writing
public void init(){ game = new computerboard(); configuregui(); }
Comments
Post a Comment