tech,

Save an applet to an image in Java

Mahesh Mahesh Follow Jun 27, 2012 · 4 mins read
Share this

Have you ever wanted to save a frame from a Java applet to an image file? In this quick piece of code, I will show you how to save an image produced by an applet to a file. Although it does a different thing, I pretty much wrote this code based on Evo’s reply in StackOverflow. This will be helpful if you want to save a series of frames to images and then animate by making a video, which is what I did to generate this video. I will explain in a later tutorial, how I made that video.

package com.smahesh;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


/**
 * @author Maheswaran Sathiamoorthy
 *
 */
public class ImageMaker extends Applet{

  int circleRadius = 10;
  private final int MAX_X = 600;
  private final int MAX_Y = 350;
  private BufferedImage bufferedImage;
  private final GraphicsConfiguration gConfig = GraphicsEnvironment
      .getLocalGraphicsEnvironment().getDefaultScreenDevice()
      .getDefaultConfiguration();


  // If you plan to show on the screen on an applet, apart from saving as an image
  @Override
  public void start() {
    setSize(MAX_X, MAX_Y);
    bufferedImage = create(MAX_X, MAX_Y, true);
  }


  // If you plan to show on the screen on an applet, apart from saving as an image
  @Override
  public void paint(Graphics g) {
    drawCircles(g);
    storeImage();
  }


  public void storeImage() {
    BufferedImage image = create(MAX_X, MAX_Y, true);
    Graphics2D g = image.createGraphics();
    // you can disable this if you don't want smooth graphics
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    drawCircles(g);
    g.dispose();
    try {
      ImageIO.write(image, "png", new File("/Users/Yourname/Documents/file.png"));
    } catch (IOException e) {
   }
  }

  void drawCircles(Graphics g) {
    int count = 0;
    for(int r = 10; r <=200; r+=5) {
      int x = (int) (MAX_X/2 + r*Math.cos(count*Math.PI/180)) ;
      int y = (int) (MAX_Y/2 + r*Math.sin(count*Math.PI/180)) ;
      count = count + 20;
      g.setColor(Color.BLACK);
      g.drawOval(x, y, circleRadius, circleRadius);
      g.setColor(new Color((float)235/255, (float)173/255, (float)96/255, 1f));
      g.fillOval(x, y, circleRadius, circleRadius);
    }
  }

  private BufferedImage create(final int width, final int height,
      final boolean alpha) {
    BufferedImage buffer = gConfig.createCompatibleImage(width, height,
              alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
    return buffer;
  }
} 

The output file that was saved by the above program:

The output image of the applet generated by the program

Thats it!

Join Newsletter
Subscribe to receive updates.
Mahesh
Written by Mahesh Follow
I think and read about Technology and the Human Mind. I am currently an ML engineer at Google, but the opinions here are my own and do not reflect that of my employer. Read More »