Sample12.java import java.awt.*; import java.awt.event.*; import java.applet.*; /** * Define our applet class. It extends the Applet object */ public class Sample12 extends Applet implements ActionListener{ // // define and create the objects that make up the interface // Sketcher pad = new Sketcher(); // create a something to draw on. Label current = new Label("You are drawing this color",Label.CENTER); /** * Constructor the applet */ public Sample12() { } /** * The following methods initialize the applet and are * invoked automatically. */ public void init(){ // // first set the applets layout manager // BorderLayout layout = new BorderLayout(5,5); setLayout(layout); setBackground(Color.lightGray); // // add the sketcher to the center section and a color // add("Center",pad); // // create a toolbar object for colors // Panel tb = new Panel(); tb.setLayout(new GridLayout(8,1,3,3)); add("West",tb); // add the toolbar to the west region Button red = new Button(""); red.setBackground(Color.red); red.addActionListener(this); tb.add(red); Button green = new Button(""); green.setBackground(Color.green); green.addActionListener(this); tb.add(green); Button blue = new Button(""); blue.setBackground(Color.blue); blue.addActionListener(this); tb.add(blue); Button yellow = new Button(""); yellow.setBackground(Color.yellow); yellow.addActionListener(this); tb.add(yellow); Button black = new Button(""); black.setBackground(Color.black); black.addActionListener(this); tb.add(black); Button white = new Button(""); white.setBackground(Color.white); white.addActionListener(this); tb.add(white); Button cyan = new Button(""); cyan.setBackground(Color.cyan); cyan.addActionListener(this); tb.add(cyan); Button pink = new Button(""); pink.setBackground(Color.pink); pink.addActionListener(this); tb.add(pink); // // Show the current color in the south // // current.setBackground(Color.lightGray); current.setForeground(pad.getColor()); add("South",current); } /** * this method is called whenever a button is * pressed. Figure out what was pressed then do it */ public void actionPerformed(ActionEvent e){ // // first make sure that it was a button that sent this // event // if(e.getSource() instanceof Button){ Button b = (Button) e.getSource(); pad.setColor(b.getBackground()); current.setForeground(b.getBackground()); } // end if } // end of actionPerformed } // end of class Sketcher.java import java.awt.*; import java.awt.event.*; /** * Sketcher class, handles mouse input to draw on a panel */ class Sketcher extends Panel implements MouseListener, MouseMotionListener{ boolean mouseDown = false; int downX, downY; Color currentColor = Color.black; /** * Constructor */ Sketcher() { addMouseListener(this); // create and add the mouse listener addMouseMotionListener(this); // create and add the mouse motion listener setBackground(Color.white); } /** * Set the color to use */ public void setColor(Color theColor){ currentColor = theColor; } /** * return the current drawing color */ public Color getColor(){ return currentColor; } /** * Mouse Clicked Event */ public void mouseClicked(MouseEvent e) { } /** * Mouse Entered Event */ public void mouseEntered(MouseEvent e) { } /** * Mouse Exited Event */ public void mouseExited(MouseEvent e){ } /** * Mouse Pressed Event */ public void mousePressed(MouseEvent e) { mouseDown = true; // remember this happened downX = e.getX(); // store the x location of the mouse downY = e.getY(); // store the y location } /** * Mouse Released Event */ public void mouseReleased(MouseEvent e) { if(mouseDown) { Graphics g = getGraphics(); // create a graphics object g.setColor(currentColor); // set its color g.drawLine(downX,downY, e.getX(), e.getY()); // tell it to draw a line mouseDown = false; } } /** * Mouse Moved Event */ public void mouseMoved(MouseEvent e) { } /** * Mouse Dragged Event */ public void mouseDragged(MouseEvent e) { if(mouseDown) { Graphics g = getGraphics(); // create a graphics object g.setColor(currentColor); // set its color g.drawLine(downX,downY, e.getX(), e.getY()); // tell it to draw a line downX = e.getX(); downY = e.getY(); } } } //