home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-03-23 | 2.4 KB | 102 lines |
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
-
- public class scribble extends Applet
- implements MouseListener, MouseMotionListener, ActionListener
- {
- private int prevX, prevY;
-
- PopupMenu popup;
- Color col = Color.black;
-
- public void init ()
- {
- super.init ();
- Color foreground = getColourParameter ("foreground");
- Color background = getColourParameter ("background");
-
- if (foreground != null)
- {
- setForeground (foreground);
- col = foreground;
- }
- if (background != null)
- setBackground (background);
-
- prevX = prevY = 0;
- addMouseListener (this);
- addMouseMotionListener (this);
-
- popup = new PopupMenu ();
- Menu colours = new Menu ("Colour");
-
- String [] colNames = { "Black", "Red", "Green", "Blue" };
- for (int i = 0; i < colNames.length; i++)
- {
- MenuItem mi = new MenuItem (colNames [i]);
- mi.addActionListener (this);
- colours.add (mi);
- }
-
- popup.add (colours);
- add (popup);
- }
-
- private Color getColourParameter (String name)
- {
- String value = getParameter (name);
- try
- {
- return new Color (Integer.parseInt (value, 16));
- }
- catch (Exception e)
- {
- return null;
- }
- }
-
- public void processMouseEvent (MouseEvent e)
- {
- if (e.isPopupTrigger ())
- popup.show (this, e.getX (), e.getY ());
- else if (e.getID () == MouseEvent.MOUSE_PRESSED)
- {
- prevX = e.getX ();
- prevY = e.getY ();
- }
- }
-
- public void mouseDragged (MouseEvent e)
- {
- Graphics g = getGraphics ();
- int x = e.getX ();
- int y = e.getY ();
-
- g.setColor (col);
- g.drawLine (prevX, prevY, x, y);
- prevX = x;
- prevY = y;
- }
-
- public void mouseReleased (MouseEvent e) {}
- public void mousePressed (MouseEvent e) {}
- public void mouseClicked (MouseEvent e) {}
- public void mouseEntered (MouseEvent e) {}
- public void mouseExited (MouseEvent e) {}
- public void mouseMoved (MouseEvent e) {}
-
- public void actionPerformed (ActionEvent event)
- {
- String command = event.getActionCommand ();
- if (command.equals ("Black"))
- col = Color.black;
- else if (command.equals ("Red"))
- col = Color.red;
- else if (command.equals ("Green"))
- col = Color.green;
- else if (command.equals ("Blue"))
- col = Color.blue;
- }
- }
-