home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 June / APC561.ISO / workshop / java / scribble.java < prev   
Encoding:
Java Source  |  2000-03-23  |  2.4 KB  |  102 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class scribble extends Applet
  6.   implements MouseListener, MouseMotionListener, ActionListener
  7. {
  8.   private int prevX, prevY;
  9.  
  10.   PopupMenu popup;
  11.   Color col = Color.black;
  12.  
  13.   public void init ()
  14.   {
  15.     super.init ();
  16.     Color foreground = getColourParameter ("foreground");
  17.     Color background = getColourParameter ("background");
  18.  
  19.     if (foreground != null)
  20.     {
  21.       setForeground (foreground);
  22.       col = foreground;
  23.     }
  24.     if (background != null)
  25.       setBackground (background);
  26.  
  27.     prevX = prevY = 0;
  28.     addMouseListener (this);
  29.     addMouseMotionListener (this);
  30.  
  31.     popup = new PopupMenu ();
  32.     Menu colours = new Menu ("Colour");
  33.  
  34.     String [] colNames = { "Black", "Red", "Green", "Blue" };
  35.     for (int i = 0; i < colNames.length; i++)
  36.     {
  37.       MenuItem mi = new MenuItem (colNames [i]);
  38.       mi.addActionListener (this);
  39.       colours.add (mi);
  40.     }
  41.  
  42.     popup.add (colours);
  43.     add (popup);
  44.   }
  45.  
  46.   private Color getColourParameter (String name)
  47.   {
  48.     String value = getParameter (name);
  49.     try
  50.     {
  51.       return new Color (Integer.parseInt (value, 16));
  52.     }
  53.     catch (Exception e)
  54.     {
  55.       return null;
  56.     }
  57.   }
  58.  
  59.   public void processMouseEvent (MouseEvent e)
  60.   {
  61.     if (e.isPopupTrigger ())
  62.       popup.show (this, e.getX (), e.getY ());
  63.     else if (e.getID () == MouseEvent.MOUSE_PRESSED)
  64.     {
  65.       prevX = e.getX ();
  66.       prevY = e.getY ();
  67.     }
  68.   }
  69.  
  70.   public void mouseDragged (MouseEvent e)
  71.   {
  72.     Graphics g = getGraphics ();
  73.     int x = e.getX ();
  74.     int y = e.getY ();
  75.  
  76.     g.setColor (col);
  77.     g.drawLine (prevX, prevY, x, y);
  78.     prevX = x;
  79.     prevY = y;
  80.   }
  81.  
  82.   public void mouseReleased (MouseEvent e) {}
  83.   public void mousePressed (MouseEvent e) {}
  84.   public void mouseClicked (MouseEvent e) {}
  85.   public void mouseEntered (MouseEvent e) {}
  86.   public void mouseExited (MouseEvent e) {}
  87.   public void mouseMoved (MouseEvent e) {}
  88.  
  89.   public void actionPerformed (ActionEvent event)
  90.   {
  91.     String command = event.getActionCommand ();
  92.     if (command.equals ("Black"))
  93.       col = Color.black;
  94.     else if (command.equals ("Red"))
  95.       col = Color.red;
  96.     else if (command.equals ("Green"))
  97.       col = Color.green;
  98.     else if (command.equals ("Blue"))
  99.       col = Color.blue;
  100.   }
  101. }
  102.