Java AWT BufferedImage save to GIF image file

Description

Java AWT BufferedImage save to GIF image file


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
  public static void main(String[] args) {
    // draw 2D rounded rectangle with a buffered background
    BufferedImage buffImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);

    // obtain Graphics2D from buffImage and draw on it
    Graphics2D gg = buffImage.createGraphics();
    gg.setColor(Color.YELLOW);/*from   ww  w . j a v a  2s.com*/
    gg.fillRect(0, 0, 10, 10);
    gg.setColor(Color.BLACK);
    gg.drawRect(1, 1, 6, 6);

    try {
      ImageIO.write(buffImage, "gif", new File( "image.gif"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related