Draw and save to GIF image in Java

Description

The following code shows how to draw and save to GIF image.

Example


  /*ww  w .  ja  va  2  s  .c o  m*/

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
  final static int WIDTH = 50;
  final static int HEIGHT = 50;
  final static int NUM_ITER = 1500;

  public static void main(String[] args) throws Exception {
    BufferedImage bi;
    bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    for (int i = 0; i < NUM_ITER; i++) {
      g.setColor(Color.RED);
      g.drawLine(1, 2, i, i + 1);
    }
    g.dispose();
    ImageIO.write(bi, "gif", new File("image.gif"));
  }
}




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform