Copy and display a drawing

I made a simple paint program which allows the user to draw in a panel. I want to be able to copy what is drawn onto the panel and display it in another panel when the user clicks a save button. Can I save it as an image somehow and then just display the image? Or, could I serialize the panel object and then display that object?

Thanks for the help

Cheers,

Ryan

Painter Panel:

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.colorchooser.*;

publicclass painterextends JPanelimplements ActionListener, ChangeListener{

private JPanel buttonPanel;

private JList sizeList;

private String drawSize="3";

privateint x, y;

private Color drawColor = Color.BLACK;

privateboolean firstRun =true;

private JColorChooser tcc;

// This array defines the pixel sizes that the paint brush can use.

privatefinal String pixSizes[] ={"1","2","3","4","5","6","7","8",

"9","10","12","15","20","30","40","50"};

// Setup the GUI.

public painter(){

// Listener to detect and log the drag of the mouse.

addMouseMotionListener(

new MouseMotionAdapter(){// anonymous inner class

publicvoid mouseDragged( MouseEvent event )

{

x = event.getX();

y = event.getY();

repaint();

}

}// end anonymous inner class

);// end call to addMouseMotionListener

// Establish the button panel.

buttonPanel =new JPanel();

buttonPanel.setLayout(new GridLayout(1,3) );

JButton colorButton=new JButton("Color");

colorButton.addActionListener(this);

//Set up color chooser for setting draw color

tcc =new JColorChooser();

tcc.getSelectionModel().addChangeListener(this);

tcc.setBorder(BorderFactory.createTitledBorder("Choose Draw Color"));

// This list allows the user to select the pixel size of the brush tool.

sizeList =new JList( pixSizes );

sizeList.setVisibleRowCount( 1 );

sizeList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

sizeList.addListSelectionListener(

new ListSelectionListener(){

publicvoid valueChanged( ListSelectionEvent event )

{

drawSize = pixSizes[sizeList.getSelectedIndex()];

}

}

);

// Create the save button which will save whatever has been drawn

// so far and copy it into the display panel.

JButton saveButton =new JButton("Save" );

saveButton.addActionListener(

new ActionListener(){

publicvoid actionPerformed( ActionEvent event )

{

// Don't know what to do here.

}

}

);// End of actionListener definition.

buttonPanel.add( colorButton );

buttonPanel.add(new JScrollPane( sizeList ) );

buttonPanel.add( saveButton );

setLayout(new BorderLayout());

// Add the button panel to the bottom of the container.

add( buttonPanel, BorderLayout.SOUTH );

setSize( 300, 300 );

setVisible(true );

}// End of the constructor.

publicvoid paint( Graphics g )

{

// Set the size of the brush based on the selection from the sizeList.

int pixelSize = Integer.parseInt(drawSize);

// Only clear the paint area if this is the first repaint().

if ( firstRun ==true ){

super.paint( g );

firstRun =false;

}

// Set the color to whatever color is selected with the colors combo

// box.

g.setColor(drawColor);

// Draw the selected shape along all of the points dragged with the

// selected size.

g.fillOval( x, y, pixelSize, pixelSize );

}

publicvoid actionPerformed(ActionEvent e){

Color newColor = JColorChooser.showDialog(

painter.this,

"Choose Draw Color",

drawColor);

if (newColor !=null){

drawColor = newColor;

}

}

publicvoid stateChanged(ChangeEvent e){

drawColor = tcc.getColor();

}

}

[8036 byte] By [trek1413a] at [2007-11-27 6:14:04]
# 1

> Can I save it as an image somehow and then just display the image?

Well you can draw the image onto a BufferedImage. You can then use the BufferedImage to create an ImageIcon which can be added to a JLabel.

Swing related questions should be posted in the Swing forum. If you search the Swing forum you will find my "ScreenImage" class which shows you how to create the BufferedImage.

camickra at 2007-7-12 17:23:13 > top of Java-index,Java Essentials,Java Programming...
# 2
Cool, thanks for the tip. I will give it a try. Sorry for posting in the wrong place.
trek1413a at 2007-7-12 17:23:13 > top of Java-index,Java Essentials,Java Programming...