import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;

public class JPEGDrawFrame extends JFrame implements WindowListener {
    String title;
    JButton toJPG, close;
    JPanel content;
    int scale = 1;

    public JPEGDrawFrame() {
        this("JPEGDrawFrame");
    }
  
    public JPEGDrawFrame(String t) {
        super(t);
	title = t;
        addWindowListener(this);

	toJPG = new JButton("To JPEG");
	toJPG.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
		BufferedImage bi = 
		    new BufferedImage(scale*content.getWidth(), 
				      scale*content.getHeight(), 
				      BufferedImage.TYPE_INT_RGB);
		Graphics2D big = (Graphics2D) bi.getGraphics();
		big.setTransform(AffineTransform.getScaleInstance(scale, scale));
		content.paint(big);
		JPEGWriter writer = new JPEGWriter(bi, 1f);
		try {
		    writer.write(title+".jpg");
		} catch(Exception ex) {ex.printStackTrace();}
	    }
	});
	close = new JButton("Close");
	close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
		windowClosing(null);
	    }
	});
	JPanel button = new JPanel();
	button.add(toJPG);
	button.add(close);
	super.getContentPane().add(button, BorderLayout.SOUTH);

	content = new JPanel();
	content.setLayout(new BorderLayout());
	super.getContentPane().add(content, BorderLayout.CENTER);
    }

    public Container getContentPane() {
	return content;
    }
  
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent e) { }
    public void windowClosed(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }
  
}

