Difference between revisions of "Animation"

From Progzoo
Jump to: navigation, search
(Lots of squares - lots of colours)
Line 149: Line 149:
 
</question>
 
</question>
  
==Lots of squares - lots of colours==
+
==Images==
 
<question copyFile="classes.jar doge.jpg" imgOut='anim.gif'
 
<question copyFile="classes.jar doge.jpg" imgOut='anim.gif'
 
  width='150' height='100' className="Anim"
 
  width='150' height='100' className="Anim"
  classpath="classes.jar">
+
  classpath="classes.jar" shell="QcQ">
 
More is more.
 
More is more.
 
<prog>
 
<prog>
<![CDATA[static void drawFrame(Graphics2D g, int frame){
+
<![CDATA[
 +
import java.awt.image.BufferedImage;
 +
import java.awt.*;
 +
import java.io.FileOutputStream;
 +
import javax.imageio.ImageIO;
 +
import net.jmge.gif.Gif89Encoder;
 +
 
 +
public class Anim{
 +
 
 +
static void drawFrame(Graphics2D g, int frame){
 
   g.setColor(Color.white);
 
   g.setColor(Color.white);
 
   g.fillRect(0,0,150,100);
 
   g.fillRect(0,0,150,100);
Line 166: Line 175:
 
     g.setColor(Color.black);
 
     g.setColor(Color.black);
 
     g.drawRect(40,0,100/20,100/20);
 
     g.drawRect(40,0,100/20,100/20);
 +
  }
 +
}
 +
 +
  public static void main(String argv []){
 +
    try {
 +
        Gif89Encoder genc = new Gif89Encoder();
 +
        for (int i=0;i<20;i++){
 +
          BufferedImage image = new BufferedImage(QwidthQ,QheightQ, BufferedImage.TYPE_INT_ARGB);
 +
          Graphics2D g = image.createGraphics();
 +
          drawFrame(g,i);
 +
          genc.addFrame(image);
 +
          g.dispose();
 +
        }
 +
        genc.setUniformDelay(10);
 +
        genc.setLoopCount(0);
 +
        FileOutputStream out = new FileOutputStream("anim.gif");
 +
        genc.encode(out);
 +
    }catch(Exception e){
 +
    System.err.println(e);
 +
    }
 
   }
 
   }
 
}
 
}

Revision as of 20:03, 6 December 2015

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]

Lots of squares - one is a different colour

We draw 20 squares.

Each of them is yellow - except one.


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

Lots of squares - lots of colours

More is more.


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

Images

More is more.


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

The animations here rely on Gif89Encoder available from http://jmge.net/java/gifenc/ developed by JMG Elliot. That, in turn was developed using Jef Poskanzer's GifEncoder.java Read Me from Gif89Encoder