java - Using Action Listeners for buttons -


code: java sphere class

 import javax.swing.*;  import java.awt.*;  import java.awt.geom.ellipse2d;  public class sphere extends jpanel { private boolean flashinglights = false; private int x = 168; private int y = 75;   public void paintcomponent(graphics g) {      super.paintcomponent(g);     graphics2d g2 = (graphics2d) g;     if (flashinglights) { //this flash option. here should change between grey , orange         g2.setcolor(color.orange);         ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50);         g2.draw(ball);         g2.fill(ball);     } else {         g2.setcolor(color.gray); //this should stay grey now.         ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50);         g2.draw(ball);         g2.fill(ball);     } }  public void chooseflashinglights(){ //ignore these methods     flashinglights = false; }  public void choosesteady(){     flashinglights = true; }  public void flickerorange(int d) { y = y + d; }   public void flickergrey(int d) { y = y + d; }   public static void main(string[] args) {     jframe scframe = new animationviewer();     scframe.settitle("circle");     scframe.setsize(400, 400);     scframe.setdefaultcloseoperation((jframe.exit_on_close));     scframe.setvisible(true); } 

}

animation viewer class:

 import javax.swing.*;  import java.awt.*;  import java.awt.event.actionevent;  import java.awt.event.actionlistener;   public class animationviewer extends jframe { jbutton jbtflash = new jbutton("flash"); jbutton jbtsteady = new jbutton("steady"); jpanel bpanel = new jpanel(); sphere sphpanel = new sphere(); timer timer;  public animationviewer() {     this.add(bpanel, borderlayout.south);     bpanel.add(jbtflash);     bpanel.setlayout(new gridlayout(1,2));     bpanel.add(jbtsteady);      this.add(sphpanel, borderlayout.center);       jbtsteady.addactionlistener(new steadylights());     jbtflash.addactionlistener(new flashinglights());      timer = new timer(100, new timerlistener());     timer.start(); }    class timerlistener implements actionlistener {     public void actionperformed(actionevent e) {         sphpanel.flickerorange(0);         sphpanel.flickergrey(0);         repaint();     } } class flashinglights implements actionlistener{     public void actionperformed(actionevent e){         sphpanel.chooseflashinglights();     } } class steadylights implements actionlistener{     public void actionperformed(actionevent e){         sphpanel.choosesteady();     } }  } 

so right there sphere appears on screen. there 2 buttons showed below. flash , steady. on steady button has stay 1 colour (orange) not.

now on flash has change orange grey every 100 milli seconds.

i know has action listeners how implement this?

you have lot of code. this. write flashing logic in paintcomponent method. create 1 timer. on tick call paintcomponent method every 100ms. on flash button click start timer, on steady button click stop timer , call paintcomponent once.

sphere class:

public class sphere extends jpanel { private boolean flashinglights = false; private int x = 168; private int y = 75; private color[] colors = new color[] {color.orange, color.gray }; private int colorindex = 0;  public void paintcomponent(graphics g) {    super.paintcomponent(g);    graphics2d g2 = (graphics2d) g;    if (!flashinglights) {        g2.setcolor(color.orange);        ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50);        g2.draw(ball);        g2.fill(ball);    } else {        if(colorindex > colors.length - 1)            colorindex = 0;         g2.setcolor(colors[colorindex++]);        ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50);        g2.draw(ball);        g2.fill(ball);    } }  public void chooseflashinglights(){ //ignore these methods    flashinglights = true; }  public void choosesteady(){    flashinglights = false; }  public static void main(string[] args) {    jframe scframe = new animationviewer();    scframe.settitle("circle");    scframe.setsize(400, 400);    scframe.setdefaultcloseoperation((jframe.exit_on_close));    scframe.setvisible(true); }  } 

animationviewer class:

public class animationviewer extends jframe { jbutton jbtflash = new jbutton("flash"); jbutton jbtsteady = new jbutton("steady"); jpanel bpanel = new jpanel(); sphere sphpanel = new sphere(); timer timer;  public animationviewer() {    this.add(bpanel, borderlayout.south);    bpanel.add(jbtflash);    bpanel.setlayout(new gridlayout(1,2));    bpanel.add(jbtsteady);     this.add(sphpanel, borderlayout.center);     timer = new timer(100, new timerlistener());    jbtsteady.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent e) {             sphpanel.choosesteady();             timer.stop();             sphpanel.repaint();         }     });    jbtflash.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent e) {             sphpanel.chooseflashinglights();             timer.start();         }     });  }  class timerlistener implements actionlistener {    public void actionperformed(actionevent e) {        sphpanel.repaint();    } }  } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -