java - Change JButton Color in a Thread -


i trying make game simon: http://www.freegames.ws/games/kidsgames/simon/simon.htm#

i making smaller scale 2 buttons. want color switch between 2 buttons, button1 , button2. in thread because need buttons clicked while happening. when open program button color stays as-is.

thanks in advance!

import java.awt.borderlayout; import java.awt.color; import java.awt.container; import java.awt.flowlayout;  import javax.swing.*;  public class testframe extends jframe{      public jbutton button1;      public jbutton button2;      boolean istrue = true;     boolean switchcolor = true;      testframe(){         super("simon");         initialize();          this.setsize(200, 400);         this.setvisible(true);     }      private void initialize() {          this.setlayout(new borderlayout());           button1 = new jbutton();         button1.setbackground(color.green);         button1.setsize(200,200);          button2 = new jbutton();         button2.setsize(200, 200);         button2.setbackground(color.blue);          this.add(button1, borderlayout.north);         this.add(button2, borderlayout.south);         thread t = new thread(r1);         t.start();      }     runnable r1 = new runnable() {         public void run() {             while(istrue){             if(switchcolor = true){                 button1.setbackground(color.blue);                 button2.setbackground(color.green);                 try {                     thread.sleep(500);                 } catch (interruptedexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 refresh();                 switchcolor = false;             } else {                 button1.setbackground(color.green);                 button2.setbackground(color.blue);                 try {                     thread.sleep(500);                 } catch (interruptedexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 refresh();                  switchcolor = true;                 }             }          }     };      public void refresh(){         this.invalidate();         this.validate();         this.repaint();     }  } 

a number of issues stand out (shazin has addressed one), other scares me violating single thread requirements of swing. changes ui must made within context of event dispatching thread.

instead of using thread, should using javax.swing.timer. save need have resync updates edt.

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class flashybuttons {      public static void main(string[] args) {         new flashybuttons();     }      public flashybuttons() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private jbutton btn1;         private jbutton btn2;         private int count = 0;          public testpane() {             setlayout(new gridbaglayout());             btn1 = new flashbutton();             btn2 = new flashbutton();             gridbagconstraints gbc = new gridbagconstraints();             gbc.gridwidth = gridbagconstraints.remainder;             add(btn1, gbc);             add(btn2, gbc);              btn1.setbackground(color.green);             btn2.setbackground(color.blue);              timer timer = new timer(500, new actionlistener() {                 @override                 public void actionperformed(actionevent e) {                     count++;                     if (count % 2 == 0) {                         btn1.setbackground(color.blue);                         btn2.setbackground(color.green);                     } else {                         btn1.setbackground(color.green);                         btn2.setbackground(color.blue);                     }                 }             });             timer.start();         }      }      public class flashbutton extends jbutton {          @override         public dimension getpreferredsize() {             return new dimension(200, 200);         }      }  } 

take @ concurrency in swing more details


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -