import java.awt.*;
import java.applet.*;
import java.lang.*;

public class Whiteboard extends Applet
{
       Graphics       sG;
       Graphics       bG;
       Image          bI;

       Color          back     = new Color(247, 243, 234);
       Color          black    = new Color(0, 0, 0);
       Color          white    = new Color(255, 255, 255);
       Color          red      = new Color(255, 0, 0);
       Color          green    = new Color(0, 255, 0);
       Color          blue     = new Color(0, 0, 255);
       Color          nored    = new Color(0, 255, 255);
       Color          nogreen  = new Color(255, 0, 255);
       Color          noblue   = new Color(255, 255, 0);
       Color          gray     = new Color(128, 128, 128);

       Color          pencolor[] = new Color[10];

       int            buttons  = 340;
       int            brushes  = 380;

       int            wholewidth  = 701;
       int            wholeheight = 401;
      
       int            pen;
       int            penradius;

       int            oldx, oldy;

       public void drawdot(Graphics g, int x, int y, int radius, Color clr)
       {
              g.setColor(clr);
              g.fillOval(x - radius, y - radius, 2 * radius + 1, 2 * radius +1);
       }

       public void drawbutton(Graphics g, Color clr,
                              int x, int y, int width, int height,
                              int shadow)
       {
              g.setColor(gray);
              g.fillRect(x - width + shadow, y - height + shadow, 
                         2 * width + 1, 2 * height + 1);
              g.setColor(clr);
              g.fillRect(x - width, y - height, 2 * width + 1, 2 * height + 1);
       }

       public void init()
       {
              pencolor[1] = black;
              pencolor[2] = red;
              pencolor[3] = blue;
              pencolor[4] = green;
              pencolor[5] = nored;
              pencolor[6] = noblue;
              pencolor[7] = nogreen;
              pencolor[8] = back;

              pen       = 1;
              penradius = 2;

              bI = createImage(wholewidth, wholewidth + 50);
              bG = bI.getGraphics();
              drawback();
              drawcontrols();
       }

       public void paint(Graphics g)
       {
              g.drawImage(bI, 0, 0, this);
              sG = getGraphics();
       }

       public void update(Graphics g)
       {
              g.drawImage(bI, 0, 0, this);
       }

       public void drawback()
       {
             bG.setColor(gray);
             bG.fillRect(0, 0, wholewidth, wholeheight);
             bG.setColor(back);
             bG.fillRect(5, 5, wholewidth - 10, wholeheight - 10);
       }

       public void drawcontrols()
       {
             for(int i = 1; i <= 8; i = i + 1)
             {
                  drawdot(bG, i * 86 - 27, brushes, i + 7, back);
                  drawdot(bG, i * 86 - 27, brushes, i + 3, gray);
                  drawdot(bG, i * 86 - 27, brushes, i, pencolor[pen]);
                  drawbutton(bG, pencolor[i], i * 86 - 27, buttons,
                             20, 10, 4);
             }
             drawdot(bG, penradius * 86 - 27, brushes, 
                     penradius + 5, black);
             drawdot(bG, penradius * 86 - 27, brushes, 
                     penradius + 3, white);
             drawdot(bG, penradius * 86 - 27, brushes, 
                     penradius, pencolor[pen]);
       }

       public boolean mouseDown(Event evt, int mx, int my)
       {
            if((my > buttons - 12) && (my < buttons + 12))
            {
                 for(int i = 1; i <= 8; i = i + 1)
                 {
                      if((mx > i * 86 - 48) && (mx < i * 86 - 6))
                      {
                             pen = i;
                             drawcontrols();
                             sG.drawImage(bI, 0, 0, this);
                      }
                 }

            }

            if((my > brushes - 10) && (my < brushes + 10))
            {
                 for(int i = 1; i <= 8; i = i + 1)
                 {
                      if((mx > i * 86 - 35) && (mx < i * 86 - 19))
                      {
                             penradius = i;
                             drawcontrols();
                             sG.drawImage(bI, 0, 0, this);
                      }
                 }
            }

            if(my < buttons - 29)
            {
                 drawdot(bG, mx, my, penradius, pencolor[pen]);
                 oldx = mx;
                 oldy = my;
                 sG.drawImage(bI, 0, 0, this);
            }
   
            if((my < 5) || (my > wholeheight - 5) ||
               (mx < 5) || (mx > wholewidth - 5))
            {
               drawback();
               drawcontrols();
               sG.drawImage(bI, 0, 0, this);
            }
    
            return true;
       }

       public boolean mouseDrag(Event evt, int mx, int my)
       {
              if(my < buttons - 29)
              {
                 bG.setColor(pencolor[pen]);
                 for(int i = 0; i <= penradius; i = i + 1)
                 {
                    for(int j = 0; 
                        i * i + j * j <= penradius * penradius; j = j + 1)
                    {
                        bG.drawLine(oldx + i, oldy + j, mx + i, my + j);
                        bG.drawLine(oldx + i, oldy - j, mx + i, my - j);
                        bG.drawLine(oldx - i, oldy + j, mx - i, my + j);
                        bG.drawLine(oldx - i, oldy - j, mx - i, my - j);
                    }
                 }
                 oldx = mx;
                 oldy = my;
                 sG.drawImage(bI, 0, 0, this);
              }
              return true;
       }

}


Copyright c 1998 by Frank Wattenberg, Department of Mathematics, Montana State University, Bozeman, MT 59717