import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myJPanelstd extends JPanel implements MouseMotionListener, ActionListener
{
// Attributes
JButton bx,by, bmessage,bc1,bc2,bc3,be1;
JPanel p1;
int width=5;
int height=5;
Point[]ps= new Point [10000];
Color[]cs= new Color[10000];
int[] size = new int[10000];
int count=0;
JSpinner spin1;
// Constructor
public myJPanelstd()
{
setBackground(Color.white);
setLayout(new BorderLayout());
p1 = new JPanel();
p1.setLayout(new GridLayout(2,4));
//Create Buttons
bc1= new JButton("RED");
bc2= new JButton("BLUE");
bc3= new JButton("GREEN");
be1= new JButton("ERASE");
bx = new JButton("x = ");
by = new JButton("y = ");
spin1=new JSpinner();
bmessage = new JButton("Move the mouse");
// Add buttons to Panel p1
p1.add(bc1);
bc1.addActionListener(this);
bc1.setBackground(Color.red);
p1.add(bc2);
bc2.setBackground(Color.blue);
p1.add(bc3);
bc3.setBackground(Color.green);
p1.add(be1);
p1.add(spin1);
add(p1,"South");
// Add Mouse Listener to Panel
addMouseMotionListener(this);
} // end constructor
//----------------------------------------------------------------------------
// All 2 methods must be created, even if you are not going to use one of them
//----------------------------------------------------------------------------
public void mouseMoved(MouseEvent evt)
{
Point pt = evt.getPoint(); // get the mouse position
String sx = "x = " + pt.getX(); // get X position of the mouse
bx.setText(sx);
String sy = "y = " + pt.getY(); // get X position of the mouse
by.setText(sy);
bmessage.setText("Mouse moved");
}
//-------------------------------------------------------------------
public void mouseDragged(MouseEvent evt)
{
Point pt=evt.getPoint();
int mx=(int) pt.getX();
int my =(int) pt.getY();
Graphics g=getGraphics();
g.setColor(Color.cyan);
g.fillOval(mx,my,width ,height);
ps[count]=pt;
cs[count]=Color.cyan;
size[count]=
count++;
bmessage.setText("Mouse dragged");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int j=0;j< count; j++)
{
g.setColor(cs[j]);
g.fillOval((int)ps[j].getX(),(int)ps[j].getY(),5,5);
}
}
} // end class