java - How to make a JButton move along with drawn lines -


image

i have jbutton (represented envelope) , arraylist of lines envelope go along. can me? ideas or tutorial appreciated.

i assume goal animate envelope along line provide animation of data being transferred.

for every line need first find line equation. line equation in form y = * x + b. if have in list mention that's fine. if on other hand have 2 points instead (start , end positions), use above equation find a, b each line.

after have equations lines can use animation (probably swingworker or swingtimer) increase x coordinate of envelope @ regular intervals , use line equation calculate new y coordinate.

i hope helps

update: added code snippet moving box using swingtimer demonstration

package keymovement;  import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.timer; import javax.swing.uimanager;  public class testmovingobject {      static class envelope {         double x;         double y;          void setposition(double x, double y) {             this.x = x;             this.y = y;         }     }      static class drawarea extends jpanel {          envelope envelope = new envelope();          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             g.setcolor(color.red);             int x = (int) math.round(envelope.x);             int y = (int) math.round(envelope.y);             g.drawrect(x, y, 20, 20);         }          public envelope getenvelope() {             return envelope;         }     }      /**      * @param args      */     public static void main(string[] args) {          swingutilities.invokelater( new runnable() {              @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (exception e) {                     throw new runtimeexception(e);                 }                  jframe window = new jframe("moving box");                 window.setpreferredsize(new dimension(500, 400));                 window.setdefaultcloseoperation(jframe.exit_on_close);                  final drawarea mainarea = new drawarea();                 window.add(mainarea);                 window.pack();                  window.setlocationrelativeto(null);                 window.setvisible(true);                  int delay = 20; // execute every 20 milliseconds i.e. 50 times per second                    timer timer = new timer(delay, new actionlistener() {                     @override                     public void actionperformed(actionevent e) {                         envelope envelope = mainarea.getenvelope();                         double x = envelope.x + 1;  // use smaller increments depending on desired speed                         double y = 0.5 * x + 2;     // moving along line y = 0.5 * x + 2                         envelope.setposition(x, y);                         mainarea.repaint();                     }                 });                 timer.setrepeats(true);                 timer.start();              }         });     }  } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -