java - paint method causing other components not to show up -


i have simple gui program i'm trying work. when user presses bottom button i'm trying shapes paint. when rid of if(buttonclicked) in paint() shows fine paint() seems auto-executed , shapes appear without button click. when add surround paint() body if(buttonclicked), regardless of how buttonhandler class handles it, rest of components not show in frame. have no idea why happening. test code , without if logic in paint() , see what's going on.

 public class gui extends jframe {        //declare components      container container;      jpanel centerpanel, northpanel, southpanel, eastpanel, westpanel, mouseclickpanel;      jlabel toplabel;      jtextarea textarea;      jbutton buttona, buttonb, drawbutton;      boolean buttonclicked;       public gui(string title) {           super(title); // invoke jframe constructor           container = getcontentpane();          container.setlayout(new borderlayout());           centerpanel = new jpanel();          centerpanel.setbackground(color.cyan);          northpanel = new jpanel();          northpanel.setbackground(color.green);          southpanel = new jpanel();          southpanel.setbackground(color.magenta);          eastpanel = new jpanel();          eastpanel.setbackground(color.orange);          westpanel = new jpanel();          westpanel.setbackground(color.yellow);          mouseclickpanel = new jpanel();          mouseclickpanel.setbackground(color.gray);          mouseclickpanel.setpreferredsize(new dimension(400, 400));           toplabel = new jlabel("press either button make happen");          toplabel.setfont(new font("calibri", font.plain, 16));          toplabel.sethorizontalalignment(jlabel.center);           textarea = new jtextarea(3, 20);          textarea.seteditable(false);          buttona = new jbutton("press here");          buttonb = new jbutton("press here");          drawbutton = new jbutton("press here");           container.add(centerpanel, borderlayout.center);          container.add(northpanel, borderlayout.north);          container.add(southpanel, borderlayout.south);          container.add(eastpanel, borderlayout.east);          container.add(westpanel, borderlayout.west);           northpanel.add(toplabel, borderlayout.north);          centerpanel.add(buttona);          centerpanel.add(textarea);          centerpanel.add(buttonb);          centerpanel.add(mouseclickpanel);          centerpanel.add(drawbutton);           buttonclicked = false;          buttonhandler buttonhandler = new buttonhandler(buttona, drawbutton, textarea, buttonclicked);             buttona.addactionlistener(buttonhandler); // add actionlisteners buttona , b          buttonb.addactionlistener(buttonhandler);          drawbutton.addactionlistener(buttonhandler);           setsize(525, 600);          setvisible(true);      }         public void paint(graphics g) {           if (buttonclicked) {             super.paint(g);              g.setcolor(color.red);             g.filloval(150, 150, 50, 50);             g.draw3drect(200, 200, 50, 50, true);         }      }  } 

handler class:

 public class buttonhandler extends jframe implements actionlistener {       private jbutton buttona, drawbutton;      private jtextarea textarea;      boolean buttonclicked;   public buttonhandler(jbutton buttona, jbutton drawbutton, jtextarea textarea, boolean buttonclicked) {     this.buttona = buttona;     this.textarea = textarea;     this.drawbutton = drawbutton;     this.buttonclicked = buttonclicked;  }  @override public void actionperformed(actionevent e) {      if (e.getsource() == buttona) {         textarea.settext(" <- pressed left button");     }     else if (e.getsource() == drawbutton) {          textarea.settext("you pressed button draw rectangle");         buttonclicked = true;         repaint();     }     else {         textarea.settext("you pressed right button ->");     } } } 

take super.paint(g); out of if statement. have first line. otherwise, no painting @ (including jpanel internals such background) happen unless button clicked.


Popular posts from this blog