Difference between revisions of "Animation"

From Progzoo
Jump to: navigation, search
(Shooting Star)
Line 71: Line 71:
 
   g.scale(100.0/r,100.0/r);
 
   g.scale(100.0/r,100.0/r);
 
   g.translate(-frame*10,-frame*10);
 
   g.translate(-frame*10,-frame*10);
 +
}
 +
 +
</prog>
 +
</question>
 +
 +
==Rotating Star==
 +
<question copyFile="classes.jar" imgOut='anim.gif'
 +
width='150' height='100' className="Anim"
 +
classpath="classes.jar">
 +
<prog>
 +
static void drawFrame(Graphics2D g, int frame){
 +
  int r = 20;
 +
  Polygon star = new Polygon(
 +
  new int[]{0,22,95,36,59,0,-59,-36,-95,-22},
 +
  new int[]{-100,-31,-31,12,81,38,81,12,-31,-31},10);
 +
  g.setColor(Color.red);
 +
  g.fillRect(0,0,300,200);
 +
  g.setColor(Color.yellow);
 +
  g.translate(10*frame,10*frame);
 +
  g.scale(r/100.0,r/100.0);
 +
  g.rotate(2*i*Math.PI/5/20);
 +
  g.fillPolygon(star);
 
}
 
}
  
 
</prog>
 
</prog>
 
</question>
 
</question>

Revision as of 10:24, 1 December 2015

The animations here rely on Gif89Encoder available from http://jmge.net/java/gifenc/ developed by JMG Elliot.

Moving block

You can click on "Show" to see the full program.

The method drawFrame is called 20 times; frame is a different value each time, the graphics object is new each time. Draws a yellow rectangle at coordinates (50,8*frame-20) which is (50,-20), (50,-12), (50,-2), (50,6) ... (50,172) So the yellow box appears at the top of the image and moves down a little with each frame.


[Font] [Default] [Show] [Resize] [History] [Profile]

Shooting Star

In this example both the x and the y coordinates change at each frame and so the star moves diagonally.


[Font] [Default] [Show] [Resize] [History] [Profile]

Rotating Star


[Font] [Default] [Show] [Resize] [History] [Profile]