
import java.io.*;
import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Acme.JPM.Encoders.*;

public class GifTest extends HttpServlet
{
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
	}


	public void doGet(HttpServletRequest req, HttpServletResponse res)
			 throws ServletException, IOException
	{
		ServletOutputStream out = res.getOutputStream();
		Frame frame = null;
		Graphics g = null;
		try
		{
			frame = new Frame();
			frame.addNotify();
			Image image = frame.createImage(320, 250);
			g = image.getGraphics();
			paintGif(g);
			res.setContentType("image/gif");
			GifEncoder encoder = new GifEncoder(image, out);
			encoder.encode();
		}
		finally
		{
			if (g != null)
			{
				g.dispose();
			}
			if (frame != null)
			{
				frame.removeNotify();
			}
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			 throws ServletException, IOException
	{
		doGet(req, res);
	}

	public void paintGif(Graphics _g)
	{
		String svgMessage = "Hello GIF World";
		int width=320, height=250;
		Shape diamond = new Polygon(new int[]{120, 210, 300, 210},
							 new int[]{120, 30, 120,  210},
							 4);
		Graphics2D g = (Graphics2D)_g;

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

		// Paint white background
		g.setPaint(Color.white);
		g.fillRect(0, 0, width, height);

		// Paint a circle in blue
		g.setPaint(new Color(102, 102, 153));
		g.fillOval(30, 30, 180, 180);

		// Paint a diamond in black
		g.setPaint(Color.black);
		g.fill(diamond);

		// Draw a string
		Font font = new Font("Verdana", Font.BOLD, 20);
		g.setFont(font);
		g.setPaint(Color.white);

		g.drawString(svgMessage, 60, 120);
	}

}


