Scribble.java
上传用户:cj1251
上传日期:2021-10-26
资源大小:14k
文件大小:32k
源码类别:

Applet

开发平台:

Java

  1. /*
  2.   Name: Scribbler version 0.0.1
  3.   By Arash Partow (1999)
  4.   Functionality:
  5.     1. Pen Function
  6.     2. Line Function
  7.     3. Rectangle Function
  8.     4. Oval Function
  9.     5. Filled Rectangle Function
  10.     6. Filled Oval Function
  11.     7. Polygon Function (limited number of nodes) (TBC)
  12.     8. Spline Function (limited number of nodes) (TBC)
  13.     9. User defined color function (RGB setting)
  14.   Colors:
  15.   Scibble contains 10 colors, including most primary colors.
  16.     1. Black
  17.     2. Blue
  18.     3. Green
  19.     4. Red
  20.     5. Purple
  21.     6. Orange
  22.     7. Pink
  23.     8. Gray
  24.     9. Yellow
  25.    10. User-Defined
  26.   Copyright notice:
  27.   Free use of Scribble is permitted under the guidelines and in accordance
  28.   with the most current version of the Common Public License.
  29.   http://www.opensource.org/licenses/cpl1.0.php
  30. */
  31. import java.awt.*;
  32. import java.applet.*;
  33. import java.awt.event.*;
  34. import java.awt.event.MouseMotionListener;
  35. import java.awt.event.MouseEvent;
  36. import java.lang.Math.*;
  37. import java.text.*;
  38. import java.io.*;
  39. import java.awt.Scrollbar.*;
  40. public class Scribble extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener
  41. {
  42.  /* Maximum X and Maximum Y coordinate values. */
  43.  private final int MAX_X           = 800;
  44.  private final int MAX_Y           = 600;
  45.  /* Operation Constants */
  46.  private final int  NO_OP          = 0;
  47.  private final int  PEN_OP         = 1;
  48.  private final int  LINE_OP        = 2;
  49.  private final int  ERASER_OP      = 3;
  50.  private final int  CLEAR_OP       = 4;
  51.  private final int  RECT_OP        = 5;
  52.  private final int  OVAL_OP        = 6;
  53.  private final int  FRECT_OP       = 7;
  54.  private final int  FOVAL_OP       = 8;
  55.  private final int  SPLINE_OP      = 9;
  56.  private final int  POLY_OP        = 10;
  57.  /* Current mouse coordinates */
  58.  private int mousex                = 0;
  59.  private int mousey                = 0;
  60.  /* Previous mouse coordinates */
  61.  private int prevx                 = 0;
  62.  private int prevy                 = 0;
  63.  /* Initial state falgs for operation */
  64.  private boolean initialPen        = true;
  65.  private boolean initialLine       = true;
  66.  private boolean initialEraser     = true;
  67.  private boolean initialRect       = true;
  68.  private boolean initialOval       = true;
  69.  private boolean initialFRect      = true;
  70.  private boolean initialFOval      = true;
  71.  private boolean initialPolygon    = true;
  72.  private boolean initialSpline     = true;
  73.  /* Main Mouse X and Y coordiante variables */
  74.  private int  Orx                  = 0;
  75.  private int  Ory                  = 0;
  76.  private int  OrWidth              = 0;
  77.  private int  OrHeight             = 0;
  78.  private int  drawX                = 0;
  79.  private int  drawY                = 0;
  80.  private int  eraserLength         = 5;
  81.  private int  udefRedValue         = 255;
  82.  private int  udefGreenValue       = 255;
  83.  private int  udefBlueValue        = 255;
  84.  /* Primitive status & color variables */
  85.  private int    opStatus           = PEN_OP;
  86.  private int    colorStatus        = 1;
  87.  private Color  mainColor          = new Color(0,0,0);
  88.  private Color  xorColor           = new Color(255,255,255);
  89.  private Color  statusBarColor     = new Color(166,166,255);
  90.  private Color  userDefinedColor   = new Color(udefRedValue,udefGreenValue,udefBlueValue);
  91.  /* Operation Button definitions */
  92.  private Button penButton          = new Button("Pen");
  93.  private Button lineButton         = new Button("Line");
  94.  private Button eraserButton       = new Button("Eraser");
  95.  private Button clearButton        = new Button("Clear");
  96.  private Button rectButton         = new Button("Rectangle");
  97.  private Button ovalButton         = new Button("Oval");
  98.  private Button fillRectButton     = new Button("Filled Rectangle");
  99.  private Button fillOvalButton     = new Button("Filled Oval");
  100.  private Button splineButton       = new Button("Spline");
  101.  private Button polygonButton      = new Button("Polygon");
  102.  /* Color Button definitions */
  103.  private Button blackButton        = new Button("Black");
  104.  private Button blueButton         = new Button("Blue");
  105.  private Button redButton          = new Button("Red");
  106.  private Button greenButton        = new Button("Green");
  107.  private Button purpleButton       = new Button("Purple");
  108.  private Button orangeButton       = new Button("Orange");
  109.  private Button pinkButton         = new Button("Pink");
  110.  private Button grayButton         = new Button("Gray");
  111.  private Button yellowButton       = new Button("Yellow");
  112.  private Button userDefButton      = new Button("User-Def");
  113.  /* User defined Color variables */
  114.  private Scrollbar redSlider       = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
  115.  private Scrollbar blueSlider      = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
  116.  private Scrollbar greenSlider     = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
  117.  /* Assorted status values for different variables */
  118.  private TextField colorStatusBar  = new TextField(20);
  119.  private TextField opStatusBar     = new TextField(20);
  120.  private TextField mouseStatusBar  = new TextField(10);
  121.  private TextField redValue        = new TextField(3);
  122.  private TextField greenValue      = new TextField(3);
  123.  private TextField blueValue       = new TextField(3);
  124.  /* Labels for operation and color fields */
  125.  private Label operationLabel      = new Label("   Tool mode:");
  126.  private Label colorLabel          = new Label("   Color mode:");
  127.  private Label cursorLabel         = new Label("   Cursor:");
  128.  /* Sub panels of the main applet */
  129.  private Panel controlPanel        = new Panel(new GridLayout(11,2,0,0));
  130.  private Panel drawPanel           = new Panel();
  131.  private Panel statusPanel         = new Panel();
  132.  private Panel udefcolPanel        = new Panel(new GridLayout(3,2,0,0));
  133.  private Panel udefdemcolPanel     = new Panel();
  134.  public void init()
  135.  {
  136.     setLayout(new BorderLayout());
  137.     /* setup color buttons */
  138.     controlPanel.add(blackButton);
  139.     controlPanel.add(blueButton);
  140.     controlPanel.add(redButton);
  141.     controlPanel.add(greenButton);
  142.     controlPanel.add(purpleButton);
  143.     controlPanel.add(orangeButton);
  144.     controlPanel.add(pinkButton);
  145.     controlPanel.add(grayButton);
  146.     controlPanel.add(yellowButton);
  147.     controlPanel.add(userDefButton);
  148.     blueButton.setBackground(Color.blue);
  149.     redButton.setBackground(Color.red);
  150.     greenButton.setBackground(Color.green);
  151.     purpleButton.setBackground(Color.magenta);
  152.     orangeButton.setBackground(Color.orange);
  153.     pinkButton.setBackground(Color.pink);
  154.     grayButton.setBackground(Color.gray);
  155.     yellowButton.setBackground(Color.yellow);
  156.     userDefButton.setBackground(userDefinedColor);
  157.     /* setup operation buttons */
  158.     controlPanel.add(penButton);
  159.     controlPanel.add(lineButton);
  160.     controlPanel.add(eraserButton);
  161.     controlPanel.add(clearButton);
  162.     controlPanel.add(rectButton);
  163.     controlPanel.add(ovalButton);
  164.     controlPanel.add(fillRectButton);
  165.     controlPanel.add(fillOvalButton);
  166.     controlPanel.add(splineButton);
  167.     controlPanel.add(polygonButton);
  168.     controlPanel.setBounds(0,0,100,300);
  169.     controlPanel.add(udefcolPanel);
  170.     controlPanel.add(udefdemcolPanel);
  171.     /* Add user-defined RGB buttons to panel */
  172.     udefcolPanel.add(redValue);
  173.     udefcolPanel.add(redSlider);
  174.     udefcolPanel.add(greenValue);
  175.     udefcolPanel.add(greenSlider);
  176.     udefcolPanel.add(blueValue);
  177.     udefcolPanel.add(blueSlider);
  178.     /* Add label and color text field */
  179.     statusPanel.add(colorLabel);
  180.     statusPanel.add(colorStatusBar);
  181.     /* Add label and operation text field */
  182.     statusPanel.add(operationLabel);
  183.     statusPanel.add(opStatusBar);
  184.     /* Add label and cursor text field */
  185.     statusPanel.add(cursorLabel);
  186.     statusPanel.add(mouseStatusBar);
  187.     /* Set not editable */
  188.     colorStatusBar.setEditable(false);
  189.     opStatusBar.setEditable(false);
  190.     mouseStatusBar.setEditable(false);
  191.     statusPanel.setBackground(statusBarColor);
  192.     controlPanel.setBackground(Color.white);
  193.     drawPanel.setBackground(Color.white);
  194.     add(statusPanel, "North");
  195.     add(controlPanel, "West");
  196.     add(drawPanel, "Center");
  197.     /* Setup action listener */
  198.     penButton.addActionListener(this);
  199.     lineButton.addActionListener(this);
  200.     eraserButton.addActionListener(this);
  201.     clearButton.addActionListener(this);
  202.     rectButton.addActionListener(this);
  203.     ovalButton.addActionListener(this);
  204.     fillRectButton.addActionListener(this);
  205.     fillOvalButton.addActionListener(this);
  206.     splineButton.addActionListener(this);
  207.     polygonButton.addActionListener(this);
  208.     blackButton.addActionListener(this);
  209.     blueButton.addActionListener(this);
  210.     redButton.addActionListener(this);
  211.     greenButton.addActionListener(this);
  212.     purpleButton.addActionListener(this);
  213.     orangeButton.addActionListener(this);
  214.     pinkButton.addActionListener(this);
  215.     grayButton.addActionListener(this);
  216.     yellowButton.addActionListener(this);
  217.     userDefButton.addActionListener(this);
  218.     redSlider.addAdjustmentListener(this);
  219.     blueSlider.addAdjustmentListener(this);
  220.     greenSlider.addAdjustmentListener(this);
  221.     /* Adding component listeners to main panel (applet) */
  222.     drawPanel.addMouseMotionListener(this);
  223.     drawPanel.addMouseListener(this);
  224.     this.addMouseListener(this);
  225.     this.addMouseMotionListener(this);
  226.     updateRGBValues();
  227.     opStatusBar.setText("Pen");
  228.     colorStatusBar.setText("Black");
  229.  }
  230.  /*
  231.     Method is called when an action event has been preformed.
  232.     All button operations and some labels, text field operations
  233.     are handled in this method.
  234.  */
  235.  public void actionPerformed(ActionEvent e)
  236.  {
  237.     /* Determine what action has occured */
  238.     /* Set the relative values           */
  239.     if (e.getActionCommand() == "Pen")
  240.        opStatus = PEN_OP;
  241.     if (e.getActionCommand() == "Line")
  242.        opStatus = LINE_OP;
  243.     if (e.getActionCommand() == "Eraser")
  244.        opStatus = ERASER_OP;
  245.     if (e.getActionCommand() == "Clear")
  246.        opStatus = CLEAR_OP;
  247.     if (e.getActionCommand() == "Rectangle")
  248.        opStatus = RECT_OP;
  249.     if (e.getActionCommand() == "Oval")
  250.        opStatus = OVAL_OP;
  251.     if (e.getActionCommand() == "Filled Rectangle")
  252.        opStatus = FRECT_OP;
  253.     if (e.getActionCommand() == "Filled Oval")
  254.        opStatus = FOVAL_OP;
  255.     if (e.getActionCommand() == "Polygon")
  256.        opStatus = POLY_OP;
  257.     if (e.getActionCommand() == "Spline")
  258.        opStatus = SPLINE_OP;
  259.     if (e.getActionCommand() == "Black")
  260.        colorStatus = 1;
  261.     if (e.getActionCommand() == "Blue")
  262.        colorStatus = 2;
  263.     if (e.getActionCommand() == "Green")
  264.        colorStatus = 3;
  265.     if (e.getActionCommand() == "Red")
  266.        colorStatus = 4;
  267.     if (e.getActionCommand() == "Purple")
  268.        colorStatus = 5;
  269.     if (e.getActionCommand() == "Orange")
  270.        colorStatus = 6;
  271.     if (e.getActionCommand() == "Pink")
  272.        colorStatus = 7;
  273.     if (e.getActionCommand() == "Gray")
  274.        colorStatus = 8;
  275.     if (e.getActionCommand() == "Yellow")
  276.        colorStatus = 9;
  277.     if (e.getActionCommand() == "User-Def")
  278.        colorStatus = 10;
  279.     initialPolygon = true;
  280.     initialSpline   = true;
  281.     /* Update Operations status bar, with current tool */
  282.     switch (opStatus)
  283.     {
  284.        case PEN_OP   : opStatusBar.setText("Pen");
  285.                        break;
  286.        case LINE_OP  : opStatusBar.setText("Line");
  287.                        break;
  288.        case ERASER_OP: opStatusBar.setText("Eraser");
  289.                        break;
  290.        case CLEAR_OP : clearPanel(drawPanel);
  291.                        break;
  292.        case RECT_OP  : opStatusBar.setText("Rectangle");
  293.                        break;
  294.        case OVAL_OP  : opStatusBar.setText("Oval");
  295.                        break;
  296.        case FRECT_OP : opStatusBar.setText("Fill-Rectangle");
  297.                        break;
  298.        case FOVAL_OP : opStatusBar.setText("Fill-Oval");
  299.                        break;
  300.        case POLY_OP : opStatusBar.setText("Polygon");
  301.                        break;
  302.        case SPLINE_OP: opStatusBar.setText("Spline");
  303.                        break;
  304.     }
  305.     /* Update Color status bar, with current color */
  306.     switch (colorStatus)
  307.     {
  308.        case  1: colorStatusBar.setText("Black");
  309.                 break;
  310.        case  2:  colorStatusBar.setText("Blue");
  311.                  break;
  312.        case  3:  colorStatusBar.setText("Green");
  313.                  break;
  314.        case  4:  colorStatusBar.setText("Red");
  315.                  break;
  316.        case  5:  colorStatusBar.setText("Purple");
  317.                  break;
  318.        case  6:  colorStatusBar.setText("Orange");
  319.                  break;
  320.        case  7:  colorStatusBar.setText("Pink");
  321.                  break;
  322.        case  8:  colorStatusBar.setText("Gray");
  323.                  break;
  324.        case  9: colorStatusBar.setText("Yellow");
  325.                 break;
  326.        case 10: colorStatusBar.setText("User Defined Color");
  327.                 break;
  328.     }
  329.     /*
  330.       Set main color, to equivelent colorStatus value
  331.     */
  332.     setMainColor();
  333.     updateRGBValues();
  334.  }
  335.  public void adjustmentValueChanged(AdjustmentEvent e)
  336.  {
  337.     updateRGBValues();
  338.  }
  339.  /*
  340.    Method will clear the whole drawPanel with
  341.    the current background color
  342.  */
  343.  public void clearPanel(Panel p)
  344.  {
  345.     opStatusBar.setText("Clear");
  346.     Graphics g = p.getGraphics();
  347.     g.setColor(p.getBackground());
  348.     g.fillRect(0,0,p.getBounds().width,p.getBounds().height);
  349.   }
  350.  /*
  351.    Method will emulate a pen style graphic.
  352.    by drawing a line from the previous mouse corrdinates
  353.    to the current mouse coordinates.
  354.    Note: In initial attempt the previous mouse coordinates
  355.          are set to the current mouse coordinates so as
  356.          not to begin the pen graphic from an unwanted
  357.          arbitrary point.
  358.  */
  359.  public void penOperation(MouseEvent e)
  360.  {
  361.     Graphics g  = drawPanel.getGraphics();
  362.     g.setColor(mainColor);
  363.     /*
  364.       In initial state setup default values
  365.       for mouse coordinates
  366.     */
  367.     if (initialPen)
  368.     {
  369.        setGraphicalDefaults(e);
  370.        initialPen = false;
  371.        g.drawLine(prevx,prevy,mousex,mousey);
  372.     }
  373.     /*
  374.       Make sure that the mouse has actually
  375.       moved from its previous position.
  376.     */
  377.     if (mouseHasMoved(e))
  378.     {
  379.        /*
  380.           set mouse coordinates to
  381.           current mouse position
  382.        */
  383.        mousex = e.getX();
  384.        mousey = e.getY();
  385.        /*
  386.           draw a line from the previous mouse coordinates
  387.           to the current mouse coordinates
  388.        */
  389.        g.drawLine(prevx,prevy,mousex,mousey);
  390.        /*
  391.           set the current mouse coordinates to
  392.           previous mouse coordinates for next time
  393.        */
  394.        prevx = mousex;
  395.        prevy = mousey;
  396.     }
  397.  }
  398.  /*
  399.    Method will emulate a line drawing graphic.
  400.    By drawing a shadow line for an origin mouse
  401.    coordinate pair to a moving mouse coordinate
  402.    pair, until the mouse has been release from
  403.    dragmode.
  404.  */
  405.  public void lineOperation(MouseEvent e)
  406.  {
  407.     Graphics g  = drawPanel.getGraphics();
  408.     g.setColor(mainColor);
  409.     /*
  410.       In initial state setup default values
  411.       for mouse coordinates
  412.     */
  413.     if (initialLine)
  414.     {
  415.        setGraphicalDefaults(e);
  416.        g.setXORMode(xorColor);
  417.        g.drawLine(Orx,Ory,mousex,mousey);
  418.        initialLine=false;
  419.     }
  420.     /*
  421.       Make sure that the mouse has actually
  422.       moved from its previous position.
  423.     */
  424.     if (mouseHasMoved(e))
  425.     {
  426.        /*
  427.          Delete previous line shadow
  428.          by xor-ing the graphical object
  429.        */
  430.        g.setXORMode(xorColor);
  431.        g.drawLine(Orx,Ory,mousex,mousey);
  432.        /* Update new mouse coordinates */
  433.        mousex = e.getX();
  434.        mousey = e.getY();
  435.        /* Draw line shadow */
  436.        g.drawLine(Orx,Ory,mousex,mousey);
  437.     }
  438.  }
  439.  /*
  440.    Method will emulate a rectangle drawing graphic.
  441.    By drawing a shadow rectangle for an origin mouse
  442.    coordinate pair to a moving mouse coordinate
  443.    pair, until the mouse has been release from
  444.    dragmode.
  445.  */
  446.  public void rectOperation(MouseEvent e)
  447.  {
  448.     Graphics g  = drawPanel.getGraphics();
  449.     g.setColor(mainColor);
  450.     /*
  451.       In initial state setup default values
  452.       for mouse coordinates
  453.     */
  454.     if (initialRect)
  455.     {
  456.        setGraphicalDefaults(e);
  457.        initialRect = false;
  458.     }
  459.     /*
  460.       Make sure that the mouse has actually
  461.       moved from its previous position.
  462.     */
  463.     if (mouseHasMoved(e))
  464.     {
  465.        /*
  466.          Delete previous rectangle shadow
  467.          by xor-ing the graphical object
  468.        */
  469.        g.setXORMode(drawPanel.getBackground());
  470.        g.drawRect(drawX,drawY,OrWidth,OrHeight);
  471.        /* Update new mouse coordinates */
  472.        mousex = e.getX();
  473.        mousey = e.getY();
  474.        /* Check new mouse coordinates for negative errors */
  475.        setActualBoundry();
  476.        /* Draw rectangle shadow */
  477.        g.drawRect(drawX,drawY,OrWidth,OrHeight);
  478.     }
  479.  }
  480.  /*
  481.    Method will emulate a oval drawing graphic.
  482.    By drawing a shadow oval for an origin mouse
  483.    coordinate pair to a moving mouse coordinate
  484.    pair, until the mouse has been release from
  485.    dragmode.
  486.  */
  487.  public void ovalOperation(MouseEvent e)
  488.  {
  489.     Graphics g  = drawPanel.getGraphics();
  490.     g.setColor(mainColor);
  491.     /*
  492.       In initial state setup default values
  493.       for mouse coordinates
  494.     */
  495.     if (initialOval)
  496.     {
  497.        setGraphicalDefaults(e);
  498.        initialOval=false;
  499.     }
  500.     /*
  501.       Make sure that the mouse has actually
  502.       moved from its previous position.
  503.     */
  504.     if (mouseHasMoved(e))
  505.     {
  506.        /*
  507.          Delete previous oval shadow
  508.          by xor-ing the graphical object
  509.        */
  510.        g.setXORMode(xorColor);
  511.        g.drawOval(drawX,drawY,OrWidth,OrHeight);
  512.        /* Update new mouse coordinates */
  513.        mousex = e.getX();
  514.        mousey = e.getY();
  515.        /* Check new mouse coordinates for negative errors */
  516.        setActualBoundry();
  517.        /* Draw oval shadow */
  518.        g.drawOval(drawX,drawY,OrWidth,OrHeight);
  519.     }
  520.  }
  521.  /*
  522.    Method will emulate a filled-rectangle drawing graphic.
  523.    By drawing a shadow filled-rectangle for an origin mouse
  524.    coordinate pair to a moving mouse coordinate
  525.    pair, until the mouse has been release from
  526.    dragmode.
  527.  */
  528.  public void frectOperation(MouseEvent e)
  529.  {
  530.     Graphics g  = drawPanel.getGraphics();
  531.     g.setColor(mainColor);
  532.     /*
  533.       In initial state setup default values
  534.       for mouse coordinates
  535.     */
  536.     if (initialFRect)
  537.     {
  538.        setGraphicalDefaults(e);
  539.        initialFRect=false;
  540.     }
  541.     /*
  542.       Make sure that the mouse has actually
  543.       moved from its previous position.
  544.     */
  545.     if (mouseHasMoved(e))
  546.     {
  547.        /*
  548.          Delete previous rectangle shadow
  549.          by xor-ing the graphical object
  550.        */
  551.        g.setXORMode(xorColor);
  552.        g.drawRect(drawX,drawY,OrWidth-1,OrHeight-1);
  553.        /* Update new mouse coordinates */
  554.        mousex = e.getX();
  555.        mousey = e.getY();
  556.        /* Check new mouse coordinates for negative errors */
  557.        setActualBoundry();
  558.        /* Draw rectangle shadow */
  559.        g.drawRect(drawX,drawY,OrWidth-1,OrHeight-1);
  560.     }
  561.  }
  562.  /*
  563.    Method will emulate a filled-oval drawing graphic.
  564.    By drawing a shadow filled-oval for an origin mouse
  565.    coordinate pair to a moving mouse coordinate
  566.    pair, until the mouse has been release from
  567.    dragmode.
  568.  */
  569.  public void fovalOperation(MouseEvent e)
  570.  {
  571.     Graphics g  = drawPanel.getGraphics();
  572.     g.setColor(mainColor);
  573.     /*
  574.       In initial state setup default values
  575.       for mouse coordinates
  576.     */
  577.     if (initialFOval)
  578.     {
  579.        setGraphicalDefaults(e);
  580.        initialFOval = false;
  581.     }
  582.     /*
  583.       Make sure that the mouse has actually
  584.       moved from its previous position.
  585.     */
  586.     if (mouseHasMoved(e))
  587.     {
  588.        /*
  589.          Delete previous oval shadow
  590.          by xor-ing the graphical object
  591.        */
  592.        g.setXORMode(xorColor);
  593.        g.drawOval(drawX,drawY,OrWidth,OrHeight);
  594.        /* Update new mouse coordinates */
  595.        mousex = e.getX();
  596.        mousey = e.getY();
  597.        /* Check new mouse coordinates for negative errors */
  598.        setActualBoundry();
  599.        /* Draw oval shadow */
  600.        g.drawOval(drawX,drawY,OrWidth,OrHeight);
  601.     }
  602.  }
  603.  /*
  604.    Method will emulate a eraser graphic.
  605.    By drawing a filled rectangle of background color,
  606.    with the current mouse coordinates being the center
  607.    of the rectangle. This is done until the mouse has
  608.    been release from dragmode
  609.  */
  610.  public void eraserOperation(MouseEvent e)
  611.  {
  612.     Graphics g  = drawPanel.getGraphics();
  613.     /*
  614.       In initial state setup default values
  615.       for mouse coordinates
  616.     */
  617.     if (initialEraser)
  618.     {
  619.        setGraphicalDefaults(e);
  620.        initialEraser = false;
  621.        g.setColor(mainColor.white);
  622.        g.fillRect(mousex-eraserLength, mousey-eraserLength,eraserLength*2,eraserLength*2);
  623.        g.setColor(Color.black);
  624.        g.drawRect(mousex-eraserLength,mousey-eraserLength,eraserLength*2,eraserLength*2);
  625.        prevx = mousex;
  626.        prevy = mousey;
  627.     }
  628.     /*
  629.       Make sure that the mouse has actually
  630.       moved from its previous position.
  631.     */
  632.     if (mouseHasMoved(e))
  633.     {
  634.        g.setColor(mainColor.white);
  635.        g.drawRect(prevx-eraserLength, prevy-eraserLength,eraserLength*2,eraserLength*2);
  636.        /* Get current mouse coordinates */
  637.        mousex  = e.getX();
  638.        mousey  = e.getY();
  639.        /* Draw eraser block to panel */
  640.        g.setColor(mainColor.white);
  641.        g.fillRect(mousex-eraserLength, mousey-eraserLength,eraserLength*2,eraserLength*2);
  642.        g.setColor(Color.black);
  643.        g.drawRect(mousex-eraserLength,mousey-eraserLength,eraserLength*2,eraserLength*2);
  644.        prevx = mousex;
  645.        prevy = mousey;
  646.     }
  647.  }
  648.  /*
  649.     Method will draw a polygon of N points on drawable surface
  650.  */
  651.  public void polygonOperation(MouseEvent e)
  652.  {
  653.     if (initialPolygon)
  654.     {
  655.        prevx = e.getX();
  656.        prevy = e.getY();
  657.        initialPolygon = false;
  658.     }
  659.     else
  660.     {
  661.        mousex = e.getX();
  662.        mousey = e.getY();
  663.        Graphics g = drawPanel.getGraphics();
  664.        g.setColor(mainColor);
  665.        g.drawLine(prevx,prevy,mousex,mousey);
  666.        prevx = mousex;
  667.        prevy = mousey;
  668.     }
  669.  }
  670. /*
  671.    Not fully implemented spline operation
  672. */
  673. public void splineOperation(MouseEvent e)
  674. {
  675.    if(initialSpline)
  676.    {
  677.       initialSpline = false;
  678.    }
  679. }
  680.  /*
  681.    Method determines weather the mouse has moved
  682.    from its last recorded position.
  683.    If mouse has deviated from previous position,
  684.    the value returned will be true, otherwise
  685.    the value that is returned will be false.
  686.  */
  687.  public boolean mouseHasMoved(MouseEvent e)
  688.  {
  689.     return (mousex != e.getX() || mousey != e.getY());
  690.  }
  691.  /*
  692.    Method is a key segment in the operations where
  693.    there are more than 2 variables deviating to
  694.    makeup a graphical operation.
  695.    This method calculates the new values for the
  696.    global varibles drawX and drawY according to
  697.    the new positions of the mouse cursor.
  698.    This method eleviates the possibility that
  699.    a negative width or height can occur.
  700.  */
  701.  public void setActualBoundry()
  702.  {
  703.        /*
  704.          If the any of the current mouse coordinates
  705.          are smaller than the origin coordinates, meaning
  706.          if drag occured in a negative manner, where either
  707.          the x-shift occured from right and/or y-shift occured
  708.          from bottom to top.
  709.        */
  710.        if (mousex < Orx || mousey < Ory)
  711.        {
  712.           /*
  713.             if the current mouse x coordinate is smaller
  714.             than the origin x coordinate,
  715.             equate the drawX to be the difference between
  716.             the current width and the origin x coordinate.
  717.           */
  718.           if (mousex < Orx)
  719.           {
  720.              OrWidth = Orx - mousex;
  721.              drawX   = Orx - OrWidth;
  722.           }
  723.           else
  724.           {
  725.              drawX    = Orx;
  726.              OrWidth  = mousex - Orx;
  727.           }
  728.           /*
  729.             if the current mouse y coordinate is smaller
  730.             than the origin y coordinate,
  731.             equate the drawY to be the difference between
  732.             the current height and the origin y coordinate.
  733.           */
  734.           if (mousey < Ory)
  735.           {
  736.              OrHeight = Ory - mousey;
  737.              drawY    = Ory - OrHeight;
  738.           }
  739.           else
  740.           {
  741.              drawY    = Ory;
  742.              OrHeight = mousey - Ory;
  743.           }
  744.        }
  745.        /*
  746.          Else if drag was done in a positive manner meaning
  747.          x-shift occured from left to right and or y-shift occured
  748.          from top to bottom
  749.        */
  750.        else
  751.        {
  752.           drawX    = Orx;
  753.           drawY    = Ory;
  754.           OrWidth  = mousex - Orx;
  755.           OrHeight = mousey - Ory;
  756.        }
  757.  }
  758.  /*
  759.    Method sets all the drawing varibles to the default
  760.    state which is the current position of the mouse cursor
  761.    Also the height and width varibles are zeroed off.
  762.  */
  763.  public void setGraphicalDefaults(MouseEvent e)
  764.  {
  765.     mousex   = e.getX();
  766.     mousey   = e.getY();
  767.     prevx    = e.getX();
  768.     prevy    = e.getY();
  769.     Orx      = e.getX();
  770.     Ory      = e.getY();
  771.     drawX    = e.getX();
  772.     drawY    = e.getY();
  773.     OrWidth  = 0;
  774.     OrHeight = 0;
  775.  }
  776.  /*
  777.    Method will be activated when mouse is being dragged.
  778.    depending on what operation is the opstatus, the switch
  779.    statement will call the relevent operation
  780.  */
  781.  public void mouseDragged(MouseEvent e)
  782.  {
  783.     updateMouseCoordinates(e);
  784.     switch (opStatus)
  785.     {
  786.        /* If opStatus is PEN_OP  then call penOperation method */
  787.        case PEN_OP   : penOperation(e);
  788.                        break;
  789.        /* If opStatus is LINE_OP then call lineOperation method */
  790.        case LINE_OP  : lineOperation(e);
  791.                        break;
  792.        /* If opStatus is RECt_OP  then call rectOperation method */
  793.        case RECT_OP  : rectOperation(e);
  794.                        break;
  795.        /* If opStatus is OVAL_OP then call ovalOperation method */
  796.        case OVAL_OP  : ovalOperation(e);
  797.                        break;
  798.        /* If opStatus is FRECT_OP  then call frectOperation method */
  799.        case FRECT_OP : frectOperation(e);
  800.                        break;
  801.        /* If opStatus is FOVAL_OP then call fovalOperation method */
  802.        case FOVAL_OP : fovalOperation(e);
  803.                        break;
  804.        /* If opStatus is ERASER_OP then call eraserOperation method */
  805.        case ERASER_OP: eraserOperation(e);
  806.                        break;
  807.     }
  808.  }
  809.  /*
  810.     Method will be activated when mouse has been release from pressed 
  811.     mode. At this stage the method will call the finalization routines
  812.     for the current operation.
  813.  */
  814.  public void mouseReleased(MouseEvent e)
  815.  {
  816.     /* Update current mouse coordinates to screen */
  817.     updateMouseCoordinates(e);
  818.     switch (opStatus)
  819.     {
  820.        /* If opStatus is PEN_OP  then call releasedPen method */
  821.        case PEN_OP    : releasedPen();
  822.                         break;
  823.        /* If opStatus is LINE_OP then call releasedLine method */
  824.        case LINE_OP   : releasedLine();
  825.                         break;
  826.        /* If opStatus is RECT_OP  then call releasedRect method */
  827.        case RECT_OP   : releasedRect();
  828.                         break;
  829.        /* If opStatus is OVAL_OP then call releasedOval method */
  830.        case OVAL_OP   : releasedOval();
  831.                         break;
  832.        /* If opStatus is FRECT_OP  then call releasedFrect method */
  833.        case FRECT_OP  : releasedFRect();
  834.                         break;
  835.        /* If opStatus is FOVAL_OP then call releasedFoval method */
  836.        case FOVAL_OP  : releasedFOval();
  837.                         break;
  838.        /* If opStatus is ERASER_OP then call releasedEraser method */
  839.        case ERASER_OP : releasedEraser();
  840.                         break;
  841.     }
  842.  }
  843.  /*
  844.     Method will be activated when mouse enters the applet area.
  845.     This method will then update the current mouse x and coordinates
  846.     on the screen.
  847.  */
  848.  public void mouseEntered(MouseEvent e)
  849.  {
  850.     updateMouseCoordinates(e);
  851.  }
  852.  /*
  853.    Method will set the main system color according to the
  854.    current color status.
  855.  */
  856.  public void setMainColor()
  857.  {
  858.     switch (colorStatus)
  859.     {
  860.        case 1 : mainColor = Color.black;
  861.                 break;
  862.        case 2:  mainColor = Color.blue;
  863.                 break;
  864.        case 3:  mainColor = Color.green;
  865.                 break;
  866.        case 4:  mainColor = Color.red;
  867.                 break;
  868.        case 5:  mainColor = Color.magenta;
  869.                 break;
  870.        case 6:  mainColor = Color.orange;
  871.                 break;
  872.        case 7:  mainColor = Color.pink;
  873.                 break;
  874.        case 8:  mainColor = Color.gray;
  875.                 break;
  876.        case 9:  mainColor = Color.yellow;
  877.                 break;
  878.        case 10: mainColor = userDefinedColor;
  879.                 break;
  880.     }
  881.  }
  882.  /*
  883.    Method is invoked when mouse has been released
  884.    and current operation is Pen
  885.  */
  886.  public void releasedPen()
  887.  {
  888.     initialPen = true;
  889.  }
  890.  /*
  891.    Method is invoked when mouse has been released
  892.    and current operation is Line
  893.  */
  894.  public void releasedLine()
  895.  {
  896.     if ((Math.abs(Orx - mousex) + Math.abs(Ory - mousey)) != 0)
  897.     {
  898.        System.out.println("Line has been released....");
  899.        initialLine = true;
  900.        Graphics g  = drawPanel.getGraphics();
  901.        g.setColor(mainColor);
  902.        g.drawLine(Orx,Ory,mousex,mousey);
  903.     }
  904.  }
  905.  /*
  906.    Method is invoked when mouse has been released
  907.    and current operation is Eraser
  908.  */
  909.  public void releasedEraser()
  910.  {
  911.     initialEraser = true;
  912.     Graphics g  = drawPanel.getGraphics();
  913.     g.setColor(mainColor.white);
  914.     g.drawRect(mousex-eraserLength,mousey-eraserLength,eraserLength*2,eraserLength*2);
  915.  }
  916.  /*
  917.    Method is invoked when mouse has been released
  918.    and current operation is Rectangle
  919.  */
  920.  public void releasedRect()
  921.  {
  922.     initialRect = true;
  923.     Graphics g  = drawPanel.getGraphics();
  924.     g.setColor(mainColor);
  925.     g.drawRect(drawX,drawY,OrWidth,OrHeight);
  926.  }
  927.  /*
  928.    Method is invoked when mouse has been released
  929.    and current operation is Oval
  930.  */
  931.  public void releasedOval()
  932.  {
  933.     initialOval = true;
  934.     Graphics g  = drawPanel.getGraphics();
  935.     g.setColor(mainColor);
  936.     g.drawOval(drawX,drawY,OrWidth,OrHeight);
  937.  }
  938.  /*
  939.    Method is invoked when mouse has been released
  940.    and current operation is Fill-Rectangle
  941.  */
  942.  public void releasedFRect()
  943.  {
  944.     initialFRect = true;
  945.     Graphics g  = drawPanel.getGraphics();
  946.     g.setColor(mainColor);
  947.     g.fillRect(drawX,drawY,OrWidth,OrHeight);
  948.  }
  949.  /*
  950.    Method is invoked when mouse has been released
  951.    and current operation is Fill-Oval
  952.  */
  953.  public void releasedFOval()
  954.  {
  955.     initialFOval = true;
  956.     Graphics g  = drawPanel.getGraphics();
  957.     g.setColor(mainColor);
  958.     g.fillOval(drawX - 1,drawY - 1,OrWidth + 2,OrHeight + 2);
  959.  }
  960.  /*
  961.    Method displays the mouse coordinates x and y values
  962.    and updates the mouse status bar with the new values.
  963.  */
  964.  public void updateMouseCoordinates(MouseEvent e)
  965.  {
  966.     String xCoor ="";
  967.     String yCoor ="";
  968.     if (e.getX() < 0) xCoor = "0";
  969.     else
  970.     {
  971.        xCoor = String.valueOf(e.getX());
  972.     }
  973.     if (e.getY() < 0) xCoor = "0";
  974.     else
  975.     {
  976.        yCoor = String.valueOf(e.getY());
  977.     }
  978.     mouseStatusBar.setText("x:" + xCoor + "   y:" + yCoor);
  979.  }
  980.  /*
  981.    Method updates user-defined values for udefRGB
  982.  */
  983.  public void updateRGBValues()
  984.  {
  985.     udefRedValue = redSlider.getValue();
  986.     udefGreenValue = greenSlider.getValue();
  987.     udefBlueValue = blueSlider.getValue();
  988.     if (udefRedValue > 255)
  989.        udefRedValue = 255;
  990.     if (udefRedValue < 0 )
  991.        udefRedValue =0;
  992.     if (udefGreenValue > 255)
  993.        udefGreenValue = 255;
  994.     if (udefGreenValue < 0 )
  995.        udefGreenValue =0;
  996.     if (udefBlueValue > 255)
  997.        udefBlueValue = 255;
  998.     if (udefBlueValue < 0 )
  999.        udefBlueValue =0;
  1000.     redValue.setText(String.valueOf(udefRedValue));
  1001.     greenValue.setText(String.valueOf(udefGreenValue));
  1002.     blueValue.setText(String.valueOf(udefBlueValue));
  1003.     userDefinedColor = new Color(udefRedValue,udefGreenValue,udefBlueValue);
  1004.     userDefButton.setBackground(userDefinedColor);
  1005.     Graphics g = udefdemcolPanel.getGraphics();
  1006.     g.setColor(userDefinedColor);
  1007.     g.fillRect(0,0,800,800);
  1008.  }
  1009.  /*
  1010.    Method updates mouse coordinates if mouse has been clicked
  1011.  */
  1012.  public void mouseClicked(MouseEvent e)
  1013.  {
  1014.     updateMouseCoordinates(e);
  1015.     switch (opStatus)
  1016.     {
  1017.        case 9  : splineOperation(e);
  1018.                  break;
  1019.        case 10 : polygonOperation(e);
  1020.                  break;
  1021.     }
  1022. }
  1023.  /*
  1024.    Method updates mouse coordinates if mouse has exited applet
  1025.  */
  1026.  public void mouseExited(MouseEvent e)
  1027.  {
  1028.     updateMouseCoordinates(e);
  1029.  }
  1030.  /*
  1031.    Method updates mouse coordinates if mouse has moved
  1032.  */
  1033.  public void mouseMoved(MouseEvent e)
  1034.  {
  1035.     updateMouseCoordinates(e);
  1036.  }
  1037.  /*
  1038.    Method updates mouse coordinates if mouse has been pressed
  1039.  */
  1040.  public void mousePressed(MouseEvent e)
  1041.  {
  1042.     updateMouseCoordinates(e);
  1043.  }
  1044. } // End of Scribble