Paint program with save option, can't get saving to work. HELPPPP!!!!!!!
Here is my paint program, you can draw cicrcles and squares, filled or unfilled. The problem is that I can't save the image properly. When save is clicked it only save the last shape drawn.
--
//JenP Drawing Class with open,and save button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class Drawing extends JFrame {
ObjectInputStream input;
ObjectOutputStream output;
private int topX, topY, width,
height, bottomX, bottomY, shape;
private boolean clear, filled;
private Color drawingColor;
private JPanel panel1, panel2, panel3, panel4;
private JRadioButton ovalBox, rectBox;
private ButtonGroup shapeGroup;
private JCheckBox fillBox;
private JComboBox colorList;
private JButton clearButton, saveButton, openButton;
private String colorNames[] = { "Black", "Orange", "Blue",
"Pink" };
private Color colors[] = { Color.black, Color.orange, Color.blue,
Color.pink};
private final int OVAL = 1, LINE = 2, RECT = 3;
private ToolWindow tools;
// Drawing constructor
public Drawing()
{
super( "Drawing" );
addMouseListener( new MouseHandler() );
drawingColor = Color.black;
shape = OVAL;
setDefaultCloseOperation( EXIT_ON_CLOSE );
setBackground( Color.white );
setSize( 300, 300 );
show();
// create a new ToolWindow
tools = new ToolWindow();
}//End constructor
public void paint( Graphics g )
{
g.setColor( drawingColor );
// set shape's top left coordinates
if ( shape != LINE ) {
topX = Math.min( topX, bottomX );
topY = Math.min( topY, bottomY );
}
// drawing a filled shape
if ( filled && shape != LINE )
switch ( shape ) {
case OVAL:
g.fillOval( topX, topY, width, height );
break;
case RECT:
g.fillRect( topX, topY, width, height );
break;
}
// drawing unfilled shapes
else
switch ( shape ) {
case OVAL:
g.drawOval( topX, topY, width, height );
break;
case RECT:
g.drawRect( topX, topY, width, height );
break;
}
// clears the background
if ( clear == true ) {
g.setColor( Color.white );
g.fillRect( 0, 0, getSize().width, getSize().height );
clear = false;
}
}
private class ToolWindow extends JFrame {
// ToolWindow constructor
public ToolWindow()
{
super( "Options" );
// SETUP choosing a colour
colorList = new JComboBox( colorNames );
colorList.setMaximumRowCount( 3 );
colorList.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent event )
{
drawingColor =
colors[ colorList.getSelectedIndex() ];
}
}
);
// adding clear button
clearButton = new JButton( "Erase" );
clearButton.addActionListener(
new ClearButtonHandler() );
//adding save button
saveButton = new JButton( "Save" );
saveButton.addActionListener(
new saveButtonHandler() );
//adding Open button
openButton = new JButton( "Open" );
openButton.addActionListener(
new openButtonHandler() );
// adding choose filled
fillBox = new JCheckBox( "Filled" );
FillBoxHandler fillHandle = new FillBoxHandler();
fillBox.addItemListener( fillHandle );
// adding choose shapes
ovalBox = new JRadioButton( "Oval", true );
rectBox = new JRadioButton( "Rectangle", false );
RadioButtonHandler handler = new RadioButtonHandler();
ovalBox.addItemListener( handler );
rectBox.addItemListener( handler );
shapeGroup = new ButtonGroup();
shapeGroup.add(ovalBox);
shapeGroup.add(rectBox);
// set up GUI layout
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel1.setLayout( new GridLayout( 1, 4 ) );
panel2.setLayout( new GridLayout( 1, 3 ) );
panel3.setLayout( new FlowLayout() );
panel4.setLayout( new FlowLayout() );
panel1.add( ovalBox );
panel1.add( rectBox );
panel1.add( fillBox );
panel2.add( new JScrollPane( colorList ) );
//adding clear save and open buttons.
panel4.add( clearButton );
panel4.add( saveButton );
panel4.add( openButton );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( panel1 );
container.add( panel2 );
container.add( panel3 );
container.add( panel4 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( 350, 175 );
setLocation( 300, 0 );
setVisible( true );
}
}
private class MouseHandler extends MouseAdapter {
public void mousePressed( MouseEvent event )
{
topX = event.getX();
topY = event.getY();
}
public void mouseReleased( MouseEvent event )
{
bottomX = event.getX();
bottomY = event.getY();
width = Math.abs( topX - bottomX );
height = Math.abs( topY - bottomY );
repaint();
}
}
// clearin the background
private class ClearButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
clear = true;
repaint();
}
}
// save button
private class saveButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
int width = getWidth();
int height = getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
g2.dispose();
try {
ImageIO.write(image, "JPEG", new File("mypanel.JPEG"));
}
catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
// open button
private class openButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY);//FILES_AND_DIRECTORIES
int result = fileChooser.showOpenDialog(null);
//System.out.println("result"+result);
if (result==JFileChooser.CANCEL_OPTION ){
//System.out.println("Hello"+result);
return;}
File fileName = fileChooser.getSelectedFile();
if ( fileName == null || result == -1 /* instead of null*/ )
JOptionPane.showMessageDialog(null,
"Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try{
input = new ObjectInputStream(
new FileInputStream(fileName));
//open.setEnabled(false);
//next.setEnabled(true);
}
catch (IOException e){
JOptionPane.showMessageDialog(null,
"Error Opening File", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
}//end of openButtonHandler
// determines what type of shape is drawn
private class RadioButtonHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
if ( event.getSource() == ovalBox )
shape = OVAL;
else if ( event.getSource() == rectBox )
shape = RECT;
}
}
// determine if shape is filled
private class FillBoxHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
filled = true;
else
filled = false;
}
}
public static void main( String args[] )
{
Drawing application = new Drawing();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}//End of the Drawing Class

