java - Why isn't this program repainting a certain part of an applet even when repaint() is called -


this simple rock, paper, scissors appelet. there 3 variables keeping track of wins, losses, , ties. there label meant display them. when used debugger repaint method being called don't understand why portion of applet isn't updating.

import java.awt.font; import java.awt.graphics;  import javax.swing.japplet; import javax.swing.jlabel; import javax.swing.jbutton;  import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.util.random;  public class jrockpaperscissors extends japplet {     jlabel lbldescision = new jlabel("descision");     jlabel lblwinner = new jlabel("winner");     jlabel lbltally = new jlabel("tally");     int tally_user = 0;     int tally_comp = 0;     int tally_ties = 0;    /**  * create applet.  */ public jrockpaperscissors() {         setsize(500,500);         getcontentpane().setlayout(null);        jlabel lblrockpaperscissors = new jlabel("rock, paper, scissors");     lblrockpaperscissors.setbounds(95, 50, 280, 48);     getcontentpane().add(lblrockpaperscissors);     font arial_1 = new font("arial", font.bold, 25);     lblrockpaperscissors.setfont(arial_1);      jlabel lblchooseonebutton = new jlabel("choose 1 button");     lblchooseonebutton.setbounds(10, 93, 146, 25);     getcontentpane().add(lblchooseonebutton);     font arial_2 = new font("arial", font.bold, 15);     lblchooseonebutton.setfont(arial_2);      jbutton btnrock = new jbutton("rock");     btnrock.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             determine_winner(0);             repaint();         }     });     btnrock.setbounds(166, 95, 89, 23);     getcontentpane().add(btnrock);      jbutton btnpaper = new jbutton("paper");     btnpaper.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             determine_winner(1);         }     });     btnpaper.setbounds(265, 95, 89, 23);     getcontentpane().add(btnpaper);      jbutton btnscissors = new jbutton("scissors");     btnscissors.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             determine_winner(2);         }     });     btnscissors.setbounds(361, 95, 89, 23);     getcontentpane().add(btnscissors);      jlabel lblresults = new jlabel("------results------");     lblresults.setbounds(20, 114, 126, 25);     getcontentpane().add(lblresults);     font arial_3 = new font("arial", font.bold, 15);     lblresults.setfont(arial_3);      lbldescision.setbounds(30, 150, 311, 14);     getcontentpane().add(lbldescision);      lblwinner.setbounds(20, 175, 146, 14);     getcontentpane().add(lblwinner);      jlabel lbltally = new jlabel("you:" + tally_user + "    computer:" + tally_comp +              "    ties:"+ tally_ties);     lbltally.setbounds(20, 214, 201, 30);     getcontentpane().add(lbltally);   }  public void determine_winner(int user_choice){     random random = new random();     int computer_choice = random.nextint(3);     if(user_choice == 0 ){         if(computer_choice ==0){             lbldescision.settext("you picked rock --- computer picked rock");             lblwinner.settext("winner: tie");             tally_ties +=1;         }         else if(computer_choice ==1){             lbldescision.settext("you picked rock --- computer picked paper");             lblwinner.settext("winner: computer");             tally_comp +=1;          }         else if(computer_choice ==2){             lbldescision.settext("you picked rock --- computer picked scissors");             lblwinner.settext("winner: you");             tally_user +=1;          }     }     else if(user_choice == 1){         if(computer_choice ==0){             lbldescision.settext("you picked paper --- computer picked rock");             lblwinner.settext("winner: you");             tally_user +=1;         }         else if(computer_choice ==1){             lbldescision.settext("you picked paper --- computer picked paper");             lblwinner.settext("winner: tie");             tally_ties +=1;         }         else if(computer_choice ==2){             lbldescision.settext("you picked paper --- computer picked scissors");             lblwinner.settext("winner: computer");             tally_comp +=1;         }     }     else if(user_choice == 2){         if(computer_choice ==0){             lbldescision.settext("you picked scissors --- computer picked rock");             lblwinner.settext("winner: computer");             tally_comp +=1;         }         else if(computer_choice ==1){             lbldescision.settext("you picked scissors --- computer picked paper");             lblwinner.settext("winner: you");             tally_user +=1;         }         else if(computer_choice ==2){             lbldescision.settext("you picked scissors --- computer picked scissors");             lblwinner.settext("winner: tie");             tally_ties +=1;         }     } revalidate(); repaint(); }  } 

  • issue number 1: never change text in tally jlabel, lbltally. in other words, text in jlabel , similar components change must call, lbltally.settext(somenewstring);
  • issue number 2: you're shadowing lbltally variable -- you're re-declaring , re-initializing variable in constructor, meaning jlabel object displayed in gui not same 1 class field refers -- don't this. declare variable , initialize valid reference once.

other side issues (issues not directly related problem) are:

  • you're using null layout. want avoid use of null layout , use of setbounds(...) component placement makes inflexible gui's while might on 1 platform terrible on other platforms or screen resolutions , difficult update , maintain.
  • your applet has no init() method, method should applet , running.
  • there no need call revalidate() or repaint() after changing text in jlabel.

in nut shell, you're doing this:

import java.awt.event.actionevent; import javax.swing.*;  public class jrockpaperetc extends japplet {    private static final string format_txt = "tally: %03d";    private int tally = 0;    private jlabel lbltally = new jlabel(string.format(format_txt, tally));     @override    public void init() {       jpanel mainpanel = new jpanel();        // *** variable shadowing here       jlabel lbltally = new jlabel("tally: 000");       mainpanel.add(lbltally);        jbutton button = new jbutton(new abstractaction("push") {           @override          public void actionperformed(actionevent e) {             tally++;             // *** lbltally's text never changed          }       });       mainpanel.add(button);        add(mainpanel);    } } 

when should doing this:

import java.awt.event.actionevent; import javax.swing.*;  public class jrockpaperetc2 extends japplet {    private static final string format_txt = "tally: %03d";    private int tally = 0;    private jlabel lbltally = new jlabel(string.format(format_txt, tally));     @override    public void init() {       jpanel mainpanel = new jpanel();        // lbltally = new jlabel("tally: 000"); // *** no shadowing       mainpanel.add(lbltally);        jbutton button = new jbutton(new abstractaction("push") {           @override          public void actionperformed(actionevent e) {             tally++;             lbltally.settext(string.format(format_txt, tally));             // *** lbltally's text changed          }       });       mainpanel.add(button);        add(mainpanel);    } } 

also, ask:

so instead of set bounds? i'm starting learn gui apps excuse me if in depth question.

instead of setbounds(), use layout managers handle layout heavy lifting you. google swing layout manager tutorial , have look.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -