import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import javax.imageio.*;


public class BufferedImageCanvas {

    private Graphics2D ig;
    private BufferedImage image;
    private int width;
    private int height;  
    private Font orig_font;
 
    // Constructor

    public BufferedImageCanvas (int w, int h) {

        width = w;
        height = h;

        // the image

        image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
        ig = image.createGraphics ();
        ig.setColor (Color.white);
        ig.fillRect (0, 0, width, height);
        ig.setColor (Color.black);

        orig_font = ig.getFont ();
    }


    public void write (String filename) {
        try {
            System.out.println ("writing to: " + filename);
            boolean b = ImageIO.write (image, "jpg", new File (filename));
            if (! b) {
                System.out.println ("ImageIO.write of " + filename + " returns false");
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }


    public int stringLength (String s) {

        FontMetrics fm = ig.getFontMetrics ();
        Rectangle2D rect = fm.getStringBounds (s, ig);
        return (int)(rect.getWidth());
    }


    public void drawLine (int x1, int y1, int x2, int y2, int t, Color c) {
        ig.setColor (c);
        BasicStroke stroke = new BasicStroke (t);
        ig.setStroke (stroke);
        Line2D line = new Line2D.Double (x1, y1, x2, y2);
        ig.draw (line);
    }


    public void drawRect (int x1, int y1, int w, int h, int t, Color c) {
        ig.setColor (c);
        BasicStroke stroke = new BasicStroke (t);
        ig.setStroke (stroke);
        Rectangle2D rect = new Rectangle2D.Double (x1 - w / 2, y1 - h / 2, w, h);
        ig.draw (rect);
    }


    public void drawCircle (int cx, int cy, int r, int t, Color c) {
	ig.setColor (c);
        BasicStroke stroke = new BasicStroke (t);
        ig.setStroke (stroke);
	ig.drawArc (cx - r, cy - r, r * 2, r * 2, 0, 360);
    }


    public void fillArc (int cx, int cy, int r, int startAng, int ang, Color c) {
	ig.setColor (c);
	ig.fillArc (cx - r, cy - r, r * 2, r * 2, startAng, ang);
    }


    public void drawString (String s, int x1, int y1, Color c) {
        ig.setColor (c);
        ig.drawString (s, x1, y1);
    }


    public void drawVertString (String s, int x1, int y1, Color c) {
        AffineTransform old_at = ig.getTransform ();
        AffineTransform new_at = AffineTransform.getRotateInstance (-Math.PI / 2.0, x1, y1);
        ig.setTransform (new_at);
        ig.setColor (c);
        ig.drawString (s, x1, y1);
        ig.setTransform (old_at);
    }


    public void setFontStyle (int style) {

        Font old_font = ig.getFont ();
        String old_font_name = old_font.getName ();
        int old_font_size = old_font.getSize ();
        int old_font_style = old_font.getStyle ();
        Font new_font = new Font (old_font_name, style, old_font_size);
        ig.setFont (new_font);
    }


    public void setFontSize (int size) {

        Font old_font = ig.getFont ();
        String old_font_name = old_font.getName ();
        int old_font_size = old_font.getSize ();
        int old_font_style = old_font.getStyle ();
        Font new_font = new Font (old_font_name, old_font_style, size);
        ig.setFont (new_font);
    }


    public int getFontSize () {

        Font old_font = ig.getFont ();
        return (old_font.getSize ());
    }



    public void restoreFont () {
        ig.setFont (orig_font);
    }


}
