Original implementation.

COMPONENT JAVA
Rubber-Banding and Object-Oriented Design

Vince Huston
Listing 1. Original implementation.


 // Purpose.  Rubber-band drawing demo (less-than-OO, brute force
 // design)

 import java.awt.*;
 import java.awt.event.*;
 
 public class RubberBandBrute extends Canvas
                   implements MouseListener, MouseMotionListener {
    abstract class Element {
       protected int xo, yo;
       public abstract void draw( Graphics g );
    }

    class Line extends Element {
       private int xe, ye;
       public Line( int a, int b, int x, int y ) {
          xo = a;   yo = b;   xe = x;   ye = y;
       }
       public void draw( Graphics g ) {
          g.drawLine( xo, yo, xe, ye );
    }  }

    class Rectangle extends Element {
       protected int w, h;
       public Rectangle( int x, int y, int ww, int hh ) {
          xo = x;   yo = y;   w = ww;   h = hh;
       }
       public void draw( Graphics g ) {
          g.drawRect( xo, yo, w, h );
    }  }
 
    class Circle extends Rectangle {
       public Circle( int x, int y, int ww, int hh )  {
          super( x, y, ww, hh );
       }
       public void draw( Graphics g ) {
          g.drawOval( xo, yo, w, h );
    }  }

    private int           x1, y1, x2, y2, total = 0;
    private Element[] drawingList = new Element[30];

   private int            currentState;
    private final int    LINE = 1, RECT = 2, CIRC = 3;

    public RubberBandBrute() {
       Frame f = new Frame( "GraphicsDemos" );
       f.add( this );  f.setSize( 300, 300 );  f.setVisible( true );
       addMouseListener( this );
       addMouseMotionListener( this );
    }

    public void mouseEntered( MouseEvent e ) { }
    public void mouseExited(  MouseEvent e ) { }
    public void mouseClicked( MouseEvent e ) { }
    public void mousePressed( MouseEvent e ) {
       if ((e.getModifiers() & InputEvent.CTRL_MASK) ==
                                             InputEvent.CTRL_MASK)
          currentState = CIRC;
       else if ((e.getModifiers() & InputEvent.BUTTON3_MASK ==
                                          InputEvent.BUTTON3_MASK)
          currentState = RECT;
       else
          currentState = LINE;
       x1 = x2 = e.getX();
       y1 = y2 = e.getY();
    }
    public void mouseReleased( MouseEvent e ) {
       if (currentState == LINE)
          drawingList[total++] = new Line( x1, y1, e.getX(), e.getY() );
       else if (currentState == RECT)
          drawingList[total++] = new Rectangle(
                       Math.min(x1,e.getX()), Math.min(y1,e.getY()),
                       Math.abs(e.getX()-x1), Math.abs(e.getY()-y1) );
       else
          drawingList[total++] = new Circle(
                       Math.min(x1,e.getX()), Math.min(y1,e.getY()),
                       Math.abs(e.getX()-x1), Math.abs(e.getY()-y1) );
    }

    // MouseMotionListener: btn up
    public void mouseMoved(MouseEvent e) { }
    // MouseMotionListener: btn down
    public void mouseDragged(MouseEvent e) {
       Graphics g = getGraphics();
       g.setXORMode( getBackground() );
       if (currentState == LINE)            // undraw previous position
          g.drawLine( x1, y1, x2, y2 );
       else if (currentState == RECT)
          g.drawRect( Math.min(x1,x2), Math.min(y1,y2),
                                Math.abs(x2-x1), Math.abs(y2-y1) );
       else
          g.drawOval( Math.min(x1,x2), Math.min(y1,y2),
                                Math.abs(x2-x1), Math.abs(y2-y1) );
       x2 = e.getX();
       y2 = e.getY();
       if (currentState == LINE)            // draw current position
          g.drawLine( x1, y1, x2, y2 );
       else if (currentState == RECT)
          g.drawRect( Math.min(x1,x2), Math.min(y1,y2),
                                Math.abs(x2-x1), Math.abs(y2-y1) );
       else
          g.drawOval( Math.min(x1,x2), Math.min(y1,y2),
                                Math.abs(x2-x1), Math.abs(y2-y1) );
       g.dispose();
    }

    public void paint( Graphics g ) {
       for (int i=0; i < total; i++) drawingList[i].draw( g );
    }
    public static void main( String[] args ) {
       new RubberBandBrute();
 }  }