jpanel - How to add a button to freeze and resume the bouncing ball JAVA -


i want add 2 buttons following code let me "freeze" , "start" ball move bounces on window. i've been trying last hour cant figure out. did work crap, if wants please let me know post it(avoided in order not extend coding). can me this? open suggestions. thanks

import java.awt.*; import javax.swing.*;  public class ballmoves extends jpanel implements runnable {     color color = color.red;     int dia = 30;     long delay = 40;     private int x = 1;     private int y = 1;     private int dx = 3;     private int dy = 7;      protected void paintcomponent(graphics g){         super.paintcomponent(g);         graphics2d g2 = (graphics2d)g;         g2.setrenderinghint(renderinghints.key_antialiasing,                 renderinghints.value_antialias_on);         g.setcolor(color);         g.filloval(x,y,30,30);   // adds color circle         g.setcolor(color.red);         g2.drawoval(x,y,30,30);   // draws circle     }        public void run() {         while(isvisible()) {             try {                 thread.sleep(delay);             } catch(interruptedexception e) {                 system.out.println("interrupted");             }             move();             repaint();         }     }      public void move() {         if(x + dx < 0 || x + dia + dx > getwidth()) {             dx *= -1;             color = getcolor();         }         if(y + dy < 0 || y + dia + dy > getheight()) {             dy *= -1;             color = getcolor();         }         x += dx;         y += dy;     }      private color getcolor() {         int rval = (int)math.floor(255);         int gval = (int)math.floor(0);         int bval = (int)math.floor(0);         return new color(rval, gval, bval);     }      private void start() {         while(!isvisible()) {             try {                 thread.sleep(25);             } catch(interruptedexception e) {                 system.exit(1);             }         }         thread thread = new thread(this);         thread.setpriority(thread.norm_priority);         thread.start();     }      public static void main(string[] args) {         ballmoves test = new ballmoves();         jframe f = new jframe();         f.setdefaultcloseoperation(jframe.exit_on_close);         f.getcontentpane().add(test);         f.setsize(400,400);         f.setlocation(200,200);         f.setvisible(true);         test.start();     } } 

update version

import java.awt.*; import java.awt.event.actionevent; import javax.swing.*;  public class ballmoves extends jpanel implements runnable {     color color = color.red;     int dia = 30;     long delay = 40;     private int x = 1;     private int y = 1;     private int dx = 3;     private int dy = 7;     private boolean isrunning;      protected void paintcomponent(graphics g){         super.paintcomponent(g);         graphics2d g2 = (graphics2d)g;         g2.setrenderinghint(renderinghints.key_antialiasing,                 renderinghints.value_antialias_on);         g.setcolor(color);         g.filloval(x,y,30,30);   // adds color circle         g.setcolor(color.red);         g2.drawoval(x,y,30,30);   // draws circle     }      public void run() {         while(isvisible()) {             try {                 thread.sleep(delay);             } catch(interruptedexception e) {                 system.out.println("interrupted");             }             move();             repaint();         }     }      public void move() {         if (isrunning) {         if(x + dx < 0 || x + dia + dx > getwidth()) {             dx *= -1;             color = getcolor();         }         if(y + dy < 0 || y + dia + dy > getheight()) {             dy *= -1;             color = getcolor();         }         x += dx;         y += dy;     }     }      private color getcolor() {         int rval = (int)math.floor(255);         int gval = (int)math.floor(0);         int bval = (int)math.floor(0);         return new color(rval, gval, bval);     }      private void start() {         while(!isvisible()) {             try {                 thread.sleep(25);             } catch(interruptedexception e) {                 system.exit(1);             }         }         thread thread = new thread(this);         thread.setpriority(thread.norm_priority);         thread.start();     }      public static void main(string[] args) {      eventqueue.invokelater(new runnable() {          @override         public void run() {             final ballmoves test = new ballmoves();             jframe f = new jframe();              jbutton start = new jbutton("start");             start.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent arg0) {                 test.isrunning = true;             }         });              jbutton stop = new jbutton("stop");             stop.addactionlistener(new actionlistener() {                  @override                 public void actionperformed(actionevent arg0) {                     test.isrunning = false;                 }             });              jpanel panel = new jpanel();             panel.add(start);             panel.add(stop);              f.add(panel, java.awt.borderlayout.north);             f.getcontentpane().add(test);              f.setdefaultcloseoperation(jframe.exit_on_close);             f.setsize(new dimension(400, 400));             f.setlocationrelativeto(null);             f.setvisible(true);             test.start();         }      });    } } 

create flag , switch on button click.

private volatile boolean isrunning;  public void move() {     if (isrunning) {        // existing code         ...     } } 

on start button click

 isrunning = true; 

on stop button click

 isrunning = false; 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -