java - How to move shape across JPanel? -


right i'm working on practice problems computer science test , i've run 1 giving me nothing trouble. understand swing part don't understand how create , move shape across panel. have far:

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class swingstarting extends jframe {      public jpanel innerpanel; // panel containing moving shape     public jbutton pauseresumebutton;     public static final int left = 0;     public static final int right = 1;     public int direction = left;     // dimensions of inner panel. simplify problem,     // assume panel have these dimensions.     public static final int panel_width = 600;     public static final int panel_height = 400;      public timer movementtimer = new timer(10,new timerlistener());      public swingstarting() {        innerpanel = new shapepanel();      innerpanel.setpreferredsize(      new dimension(panel_width,panel_height));      innerpanel.setborder(      borderfactory.createlineborder(color.black, 2));      pauseresumebutton = new jbutton("pause");       add(innerpanel, borderlayout.center);      jpanel buttonpanel = new jpanel(new flowlayout());      buttonpanel.add(pauseresumebutton);      add(buttonpanel, borderlayout.south);       setdefaultcloseoperation(exit_on_close);      pack();       setvisible(true);      movementtimer.start();     } // end constructor      public class shapepanel extends jpanel {        public void paint(graphics gc) {          super.paintcomponent(gc);          int circlex = 0;          int circley = 100;          gc.setcolor(color.red);          gc.filloval(circlex,circley,20,20);         }    } // end inner class       public class timerlistener implements actionlistener {      public void actionperformed(actionevent e) {         } // end actionperformed    } // end inner class     public static void main(string args[]) {      new swingstarting();     } // end main  }// end class 

so far i've created small red circle. how make cross screen horizontally? appreciated.

in panel class why not make timer action listener?

    // make shape here or earlier whenever want.     // way, change shapepanel's paint method paintcomponent because extends jpanel not jframe     // create object giving shapepanel constructor     shapepanel s = new shapepanel();     actionlistener listener = new actionlistener()     {         public void actionperformed(actionevent event)         {             // in here move shape             s.moveright();             // use methods movement well.             repaint();         }     };     timer timer = new timer(5, listener);     timer.start(); 

also, because using swing want make sure motions , things on 1 edt. try using in main method instead of making new swingstarting

public static void main(string[] args) {     javax.swing.swingutilities.invokelater(new runnable() {         public void run()          {            createandshowgui();         }     }); } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -