java - Calling Graphics function from another functionJava -
i trying button call graphics function in java take lives left , draw appropriate parts hang man game. working except drawing. believe because of way calling function i'm not sure. thanks
code button:
if(temptext == label.gettext()){ lives--; panel2.paintcomponent(); if (lives == 0){ joptionpane.showmessagedialog(null,"you lose"); buttona.setenabled(false); buttonb.setenabled(false); buttonc.setenabled(false); buttond.setenabled(false); buttone.setenabled(false); buttonf.setenabled(false); buttong.setenabled(false); buttonh.setenabled(false); buttoni.setenabled(false); buttonj.setenabled(false); buttonk.setenabled(false); buttonl.setenabled(false); buttonm.setenabled(false); buttonn.setenabled(false); buttono.setenabled(false); buttonp.setenabled(false); buttonq.setenabled(false); buttonr.setenabled(false); buttons.setenabled(false); buttont.setenabled(false); buttonu.setenabled(false); buttonv.setenabled(false); buttonw.setenabled(false); buttonx.setenabled(false); buttony.setenabled(false); buttonz.setenabled(false); } }
code drawing
class colorpanel extends jpanel { final hangman man = new hangman(); graphics g; public void paintcomponent(){ super.paintcomponent(g); this.setbackground(color.white); if(man.lives == 5){ g.drawoval(175, 100, 50, 50); } if(man.lives == 4){ g.drawline(200, 200, 200, 150); } if(man.lives == 3){ g.drawline(200, 200, 250, 250); } if(man.lives == 2){ g.drawline(200, 200, 150, 250); } if(man.lives == 1){ g.drawline(150, 150, 200, 175); } if(man.lives == 0){ g.drawline(250, 150, 200, 175); } g.drawline(100, 300, 100, 50); g.drawline(200, 50, 100, 50); g.drawline(200, 100, 200, 50); g.drawoval(50, 300, 100, 50); } }
errors
at colorpanel.paintcomponent(hangman.java:2573)
at hangman$3.actionperformed(hangman.java:445)
you should never call paintcomponent or super.paintcomponent directly, , should not using appears null graphics object. i'm frankly surprised you're not getting nullpointerexception
code. , in fact, maybe -- never show enough of exception's stacktrace know type of error getting (suggestion -- show entire exception stacktrace).
instead have button press change state of drawnig jpanek, change 1 of key fields, , call repaint()
on drawing jpanel. inside of drawing jpanel's protected void paintcomponent(graphics g)
method, call super method, , use field states decide draw.
for instance, field simple int gets incremented or decremented every time wrong letter picked, , paintcomponent uses int decide , how many body parts draw.
check out tutorial, lesson: performing custom painting see details of how want draw in gui.
i want second eddyg's recommendation -- use array of jbutton or hashmap<character, jbutton>
organize gui code better. way create jbuttons in simple loop, looping int (char c =
a; c <= 'z'; c++) {...}
. make code more compact, easier understand , easier debug.
for example,
import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.util.hashmap; import java.util.map; import javax.swing.*; @suppresswarnings("serial") public class letterselectiongrid extends jpanel { public static final string selected_letter = "selected letter"; private map<character, abstractbutton> buttonmap = new hashmap<>(); public letterselectiongrid() { setborder(borderfactory.createtitledborder("select letters")); buttonlistener buttonlistener = new buttonlistener(); setlayout(new gridlayout(3, 0)); (char c = 'a'; c <= 'z'; c++) { string text = string.valueof(c); jbutton button = new jbutton(text); button.addactionlistener(buttonlistener); add(button); // add gui buttonmap.put(c, button); } } private class buttonlistener implements actionlistener { @override public void actionperformed(actionevent e) { abstractbutton btn = (abstractbutton) e.getsource(); char selectedchar = e.getactioncommand().charat(0); btn.setenabled(false); firepropertychange(selected_letter, null, selectedchar); } } public void setallenabled(boolean enabled) { (abstractbutton btn : buttonmap.values()) { btn.setenabled(enabled); } } private static void createandshowgui() { final letterselectiongrid letterselectiongrid = new letterselectiongrid(); jbutton resetbutton = new jbutton(new abstractaction("reset") { @override public void actionperformed(actionevent e) { letterselectiongrid.setallenabled(true); } }); jpanel southpanel = new jpanel(); southpanel.add(resetbutton); letterselectiongrid.addpropertychangelistener(letterselectiongrid.selected_letter, new propertychangelistener() { @override public void propertychange(propertychangeevent evt) { system.out.println("selected letter is: " + evt.getnewvalue()); } }); jframe frame = new jframe("buttongrid"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(letterselectiongrid); frame.getcontentpane().add(southpanel, borderlayout.page_end); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment