This is a mini-paint program which uses class Point. It is very basic and allows a line to be drawn, then altered by its size and color. The issue that I am having is when I change color or size, all of the previous lines change with it?
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Painter extends JFrame
{
private int pointCount = 0;
private int size1 = 4;
private int size2 = 4;
private JRadioButton thinButton;
private JRadioButton mediumButton;
private JRadioButton thickButton;
private JButton changeColorButton;
private Color color = Color.BLACK;
private Container container;
// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
// set up GUI and register mouse event handler
public Painter()
{
super( "A simple paint program" );
class MouseDraggedPoint extends MouseMotionAdapter
{
// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length )
{
points[ pointCount ] = event.getPoint();
++pointCount;
repaint();
}
}
}
MouseDraggedPoint draggedPoint = new MouseDraggedPoin();
addMouseMotionListener(draggedPoint);
setSize( 800, 300 );
setVisible( true );
JPanel buttonPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
thinButton = addRadioButton(buttonPanel, group, "Thin", true);
mediumButton = addRadioButton(buttonPanel,group, "Medium", false);
thickButton = addRadioButton(buttonPanel, group, "Thick", false);
thinButton.addItemListener(new SelectItemListener());
mediumButton.addItemListener(new SelectItemListener());
thickButton.addItemListener(new SelectItemListener());
// set up changeColorButton and register its event handler
changeColorButton = new JButton( "Change Color" );
ActionListener listener = new ColorListener();
changeColorButton.addActionListener(listener);
getContentPane().setLayout( new FlowLayout() );
getContentPane().add( new JLabel( "Drag the mouse to draw"));
getContentPane().add(buttonPanel);
getContentPane().add(changeColorButton);
}
public JRadioButton addRadioButton(JPanel buttonPanel, ButtonGroup g,
String buttonName, boolean v) {
JRadioButton button = new JRadioButton(buttonName, v);
g.add(button);
buttonPanel.add(button);
return button;
}
public JButton addJButton(JPanel buttonPanel, String buttonName)
{
JButton button = new JButton(buttonName);
buttonPanel.add(button);
return(button);
}
class SelectItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getSource();
if (source == thinButton)
size1 = 2;
else if (source == mediumButton)
size1 = 12;
else if (source == thickButton)
size1 = 24;
}
}
/**
* display JColorChooser when user clicks button
*/
class ColorListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
color = JColorChooser.showDialog(
Painter.this, "Choose a color", color );
}
} // end ColorListener class
// end Painter constructor
/** draw oval in a 4-by-4 bounding box at specified location on window */
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area
g.setColor(color);
for ( int i = 0; i < points.length && points[ i ] != null; i++ )
g.fillOval( points[ i ].x, points[ i ].y, size1, size2 );
}
public static void main( String args[] )
{
Painter application = new Painter();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class Painter