java - Animating a painted object -


i've gone through , made classes objects , calls them in paint component, cant them move. here move , check walls program in object class:

public void move() {     ballx += dx;     bally += dy;      checkwalls(); }  //bounce of top/bottom, pass through left/right public void checkwalls() {     //left-right     if(ballx > w) {         ballx = -diam;     }     else if(ballx < -diam) {         ballx = w;     }      //top-bottom     if(bally < 0) {         dy *= -1;     }     else if(bally + diam > h) {         dy *= -1;     } } 

and here call them:

while(true) // infinite loop  {     jelly1.move();      frame.repaint();      try     {         thread.sleep(10);     }     catch(exception e){} } 

also feel need mention have background , background component. while(true) in background component because that's objects created. , frame set visible in background main method is.

paint component follows:

public class backgroundcomponent extends jcomponent {     jellyfish jelly1;     jellyfish jelly2;     jellyfish jelly3;     diver diver;      public backgroundcomponent() {         diver = new diver(100, 300);         jelly1 = new jellyfish(450, 450);         jelly2 = new jellyfish(150, 300);         jelly3 = new jellyfish(350, 75);         diver = new diver(100, 300);     }       public void paintcomponent(graphics g){         //drawing instructions go here         //recover graphics2d         graphics2d g2 = (graphics2d)g;         //make gradient         g2.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality);         int w = getwidth();         int h = getheight();         color color1 = color.cyan;         color color2 = color.black;         gradientpaint gp = new gradientpaint(0, 0, color1, 0, h, color2);         g2.setpaint(gp);         g2.fillrect(0, 0, w, h);         //constructs rectangles on edge of screen , draws them         rectangle box = new rectangle(0,0,75,700);         g.setcolor(color.light_gray);         g2.fill(box);         rectangle box2 = new rectangle(625, 0, 75, 700);         g.setcolor(color.light_gray);         g2.fill(box2);         //draws lines, stroke of 5, on rectangles         line2d.double segment = new line2d.double(10, 0, 10, 700);         g2.setstroke(new basicstroke(5));         g.setcolor(color.gray);         g2.draw(segment);         line2d.double segment2 = new line2d.double(30, 0, 30, 700);         g.setcolor(color.gray);         g2.draw(segment2);         line2d.double segment3 = new line2d.double(50, 0, 50, 700);         g.setcolor(color.gray);         g2.draw(segment3);         line2d.double segment4 = new line2d.double(70, 0, 70, 700);         g.setcolor(color.gray);         g2.draw(segment4);         line2d.double segment5 = new line2d.double(690, 0, 690, 700);         g.setcolor(color.gray);         g2.draw(segment5);         line2d.double segment6 = new line2d.double(670, 0, 670, 700);         g.setcolor(color.gray);         g2.draw(segment6);         line2d.double segment7 = new line2d.double(650, 0, 650, 700);         g.setcolor(color.gray);         g2.draw(segment7);         line2d.double segment8 = new line2d.double(630, 0, 630, 700);         g.setcolor(color.gray);         g2.draw(segment8);         //draws rectangle around title thick boarder         rectangle box3 = new rectangle(40,40,620,75);         g.setcolor(color.white);         g2.setstroke(new basicstroke(5));         g2.draw(box3);         //drawing text         string title = "through depths";         //sets font, font size, , color         g.setfont(new font("purisa", font.bold, 50));         g.setcolor(color.dark_gray);         g2.drawstring(title, (50), 100);         //places same text , on         g.setfont(new font("purisa", font.bold, 50));         g.setcolor(color.white);         g2.drawstring(title, 53, 97);         //draws ellipses stroke of 2 (these bubbles)         ellipse2d.double ellipse = new ellipse2d.double(450, 200, 150, 150);         g2.setstroke(new basicstroke(2));         g2.draw(ellipse);         ellipse2d.double ellipse2 = new ellipse2d.double(510, 375, 90, 90);         g2.draw(ellipse2);         ellipse2d.double ellipse3 = new ellipse2d.double(470, 485, 70, 70);         g2.draw(ellipse3);         ellipse2d.double ellipse4 = new ellipse2d.double(510, 580, 45, 45);         g2.draw(ellipse4);         // draws curves bubbles         quadcurve2d q = new quadcurve2d.float();         q.setcurve(548, 210, 607, 240, 590, 295);         g2.setstroke(new basicstroke(3));         g2.draw(q);         quadcurve2d q2 = new quadcurve2d.float();         q2.setcurve(575, 387, 607, 415, 585, 445);         g2.draw(q2);         quadcurve2d q3 = new quadcurve2d.float();         g2.setstroke(new basicstroke(2));         q3.setcurve(515, 493, 545, 511, 528, 540);               g2.draw(q3);         quadcurve2d q4 = new quadcurve2d.float();         g2.setstroke(new basicstroke(1));         q4.setcurve(538, 585, 558, 595, 545, 617);         g2.draw(q4);         // sets color pink before drawing jellyfish         g.setcolor(color.pink);         //draws jellyfish         jelly1.draw(g);         jelly2.draw(g);         jelly3.draw(g);         // draws diver         diver.draw(g);         while(true) // infinite loop          {         jelly1.move();           repaint();          try         {         thread.sleep(10);         }         catch(exception e){}         }         } } 

while(true){ ... } , thread.sleep inside paintcomponent implementation wrong way of doing things. doing this, blocking event dispatching thread means ui no longer updated , ui no longer responsive.

what should do:

  • remove while(true){ ... } loop paintcomponent override
  • create javax.swing.timer, set run every 10 milliseconds
  • in actionlistener.actionperformed implementation - action performed each 10 ms - move jelly , call repaint
  • start timer after initialization done, eg @ end of constructor or initialization method (not paintcomponent).
  • stop timer when no longer needed

simplified example timer, based on snippet:

new javax.swing.timer( 10, new actionlistener( ) {     @override     public void actionperformed( actionevent e ) {         jelly1.move();         repaint();     } }).start(); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -