Image

Imagebanana wrote in Imagejava_dev

Drawing

I've been working on some drawing classes. It's quite nice. Can you figure what this does without trying it out?


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MakeJPG
{
	private int           width    = 128;
	private int           height   = 80;
	private BufferedImage bi;
	private String        fileName = "image.jpg";

	public static void main ( String [] args )
	{
		MakeJPG ib = new MakeJPG ();

		ib.draw ();
		try
		{
			ib.save ();
		}
		catch ( Exception e )
		{
			System.err.println ( "Whoops: " + e );
		}
	}

	public MakeJPG ()
	{
		bi = new BufferedImage ( width, height, BufferedImage.TYPE_INT_RGB );
	}

	public void draw ()
	{
		Graphics2D g2d = bi.createGraphics ();

		g2d.drawOval ( 5,  2,  width - 10,  height - 4 );
		g2d.drawArc ( width / 4,  height / 4,  width / 2,  height / 2,  -30,  -120 );
		g2d.fillOval ( width / 4 + 5,  height / 4,  width / 8,  height / 8 );
		g2d.fillOval ( width * 5 / 8 - 5,  height / 4,  width / 8,  height / 8 );
	}

	public void save ()
			throws FileNotFoundException, IOException
	{
		FileOutputStream fos = new FileOutputStream ( fileName );

		JPEGCodec.createJPEGEncoder( fos ).encode ( bi );
		fos.close ();
	}
}


OK, it writes a JPG file, but what's it look like?