in the code below the color of the drawing won't change can someone explain why??
//
// Using class MouseMotionAdapter.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Painter extends JFrame
{
private String x[] = { "4", "8", "16", "32", "64" };
private String y[] = { "4", "8", "16", "32", "64" };
private int xValue = 4, yValue = 4;
private JButton changeBackground, changeColor, ChangePen;
private Color color = Color.lightGray;
//private Color pcolor = Color.BLACK;
private Color myColor[] = new Color[1000];
private Color currentColor;
private Container c;
private JPanel buttonPanel;
private int pointCount = 0;
private JComboBox penSizex, penSizey;
private int myProperties[][] = new int[1000][2];
// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
public Painter()
{
super( "A simple paint program" );
//System.out.println("in painter!!!");
setSize( 800, 600 );
show();
c = getContentPane();
c.add(
new Label( "Drag the mouse to draw" ),
BorderLayout.SOUTH );
//Add button here to change color
buttonPanel = new JPanel();
changeColor = new JButton( "Pen color" );
changeBackground = new JButton( "Change Background" );
buttonPanel.add( changeBackground );
buttonPanel.add( changeColor );
buttonPanel.add(new Label( "Click arrows to choose pensize" ));
penSizex = new JComboBox( x );
penSizex.setMaximumRowCount( 3 );
penSizey = new JComboBox( y );
penSizey.setMaximumRowCount( 3 );
buttonPanel.add( penSizex );
buttonPanel.add(new Label( "X" ));
buttonPanel.add( penSizey );
buttonPanel.add(new Label( "Y" ));
// listen to backgroud button events
changeBackground.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", color );
if ( color == null )
color = Color.lightGray; // need to set screen to previous colour here
c.setBackground( color );
c.repaint();
}
}
);
// listen to pen color button events
changeColor.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", currentColor );
if ( currentColor == null )
currentColor = Color.BLACK;
//c.repaint();
}
}
);
/** Listens to the combobox(penSizex). */
penSizex.addItemListener(
//.anonymous inner class to handle jcomboBox events
new ItemListener()
{
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{
//String petNamey = (String)penSizey.getSelectedItem();
String petNamex = (String)penSizex.getSelectedItem();
xValue = Integer.parseInt( petNamex );
//yValue = Integer.parseInt( petNamey );
}
}
}
);// end call to item listener
// Listens to the combobox(penSizey).
penSizey.addItemListener(
//.anonymous inner class to handle jcomboBox events
new ItemListener()
{
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{
String petNamey = (String)penSizey.getSelectedItem();
yValue = Integer.parseInt( petNamey );
}
}
}
);// end call to item listener
c.add( buttonPanel, BorderLayout.NORTH );
setVisible( true );
addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xValue;
myProperties[pointCount][1] = yValue;
myColor[pointCount] = currentColor;
++pointCount;
repaint();
}
}
}
);// end call to addMouseMotionListener
}// end Painter2 constructor
// draw oval of dimensions determined from combobox
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area
for ( int i = 0; i < points.length && points[ i ] != null; i++ )
{
g.setColor(myColor);
g.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1] );
}
}
// execute program
public static void main( String args[] )
{
Painter app = new Painter();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
//
// Using class MouseMotionAdapter.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Painter extends JFrame
{
private String x[] = { "4", "8", "16", "32", "64" };
private String y[] = { "4", "8", "16", "32", "64" };
private int xValue = 4, yValue = 4;
private JButton changeBackground, changeColor, ChangePen;
private Color color = Color.lightGray;
//private Color pcolor = Color.BLACK;
private Color myColor[] = new Color[1000];
private Color currentColor;
private Container c;
private JPanel buttonPanel;
private int pointCount = 0;
private JComboBox penSizex, penSizey;
private int myProperties[][] = new int[1000][2];
// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
public Painter()
{
super( "A simple paint program" );
//System.out.println("in painter!!!");
setSize( 800, 600 );
show();
c = getContentPane();
c.add(
new Label( "Drag the mouse to draw" ),
BorderLayout.SOUTH );
//Add button here to change color
buttonPanel = new JPanel();
changeColor = new JButton( "Pen color" );
changeBackground = new JButton( "Change Background" );
buttonPanel.add( changeBackground );
buttonPanel.add( changeColor );
buttonPanel.add(new Label( "Click arrows to choose pensize" ));
penSizex = new JComboBox( x );
penSizex.setMaximumRowCount( 3 );
penSizey = new JComboBox( y );
penSizey.setMaximumRowCount( 3 );
buttonPanel.add( penSizex );
buttonPanel.add(new Label( "X" ));
buttonPanel.add( penSizey );
buttonPanel.add(new Label( "Y" ));
// listen to backgroud button events
changeBackground.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", color );
if ( color == null )
color = Color.lightGray; // need to set screen to previous colour here
c.setBackground( color );
c.repaint();
}
}
);
// listen to pen color button events
changeColor.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", currentColor );
if ( currentColor == null )
currentColor = Color.BLACK;
//c.repaint();
}
}
);
/** Listens to the combobox(penSizex). */
penSizex.addItemListener(
//.anonymous inner class to handle jcomboBox events
new ItemListener()
{
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{
//String petNamey = (String)penSizey.getSelectedItem();
String petNamex = (String)penSizex.getSelectedItem();
xValue = Integer.parseInt( petNamex );
//yValue = Integer.parseInt( petNamey );
}
}
}
);// end call to item listener
// Listens to the combobox(penSizey).
penSizey.addItemListener(
//.anonymous inner class to handle jcomboBox events
new ItemListener()
{
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{
String petNamey = (String)penSizey.getSelectedItem();
yValue = Integer.parseInt( petNamey );
}
}
}
);// end call to item listener
c.add( buttonPanel, BorderLayout.NORTH );
setVisible( true );
addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xValue;
myProperties[pointCount][1] = yValue;
myColor[pointCount] = currentColor;
++pointCount;
repaint();
}
}
}
);// end call to addMouseMotionListener
}// end Painter2 constructor
// draw oval of dimensions determined from combobox
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area
for ( int i = 0; i < points.length && points[ i ] != null; i++ )
{
g.setColor(myColor);
g.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1] );
}
}
// execute program
public static void main( String args[] )
{
Painter app = new Painter();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}