FileDocCategorySizeDatePackage
ScrollableScribble.javaAPI DocExample5680Thu Aug 08 12:23:38 BST 1996None

ScrollableScribble

public class ScrollableScribble extends Panel

Fields Summary
Canvas
canvas
Scrollbar
hbar
Scrollbar
vbar
Vector
lines
int
last_x
int
last_y
int
offset_x
int
offset_y
int
canvas_width
int
canvas_height
Constructors Summary
public ScrollableScribble()

    
    // Create a canvas and two scrollbars and lay them out in the panel.
    // Use a BorderLayout to get the scrollbars flush against the 
    // right and bottom sides of the canvas.  When the panel grows,
    // the canvas and scrollbars will also grow appropriately.
      
        // implicit super() call here creates the panel
        canvas = (Canvas) new BlankCanvas();
        hbar = new Scrollbar(Scrollbar.HORIZONTAL);
        vbar = new Scrollbar(Scrollbar.VERTICAL);
        this.setLayout(new BorderLayout(0, 0));
        this.add("Center", canvas);
        this.add("South", hbar);
        this.add("East", vbar);
    
Methods Summary
public booleanhandleEvent(java.awt.Event e)

        if (e.target == hbar) {
            switch(e.id) {
            case Event.SCROLL_LINE_UP:  
            case Event.SCROLL_LINE_DOWN: 
            case Event.SCROLL_PAGE_UP:  
            case Event.SCROLL_PAGE_DOWN: 
            case Event.SCROLL_ABSOLUTE:  
                offset_x = ((Integer)e.arg).intValue();    break;
            }
            this.update(canvas.getGraphics());
            return true;
        }
        else if (e.target == vbar) {
            switch(e.id) {
            case Event.SCROLL_LINE_UP:  
            case Event.SCROLL_PAGE_UP:  
            case Event.SCROLL_LINE_DOWN: 
            case Event.SCROLL_PAGE_DOWN: 
            case Event.SCROLL_ABSOLUTE:  
                offset_y = ((Integer)e.arg).intValue();    break;
            }
            this.update(canvas.getGraphics());
            return true;
        }
        
        // If we didn't handle it above, pass it on to the superclass
        // handleEvent routine, which will check its type and call
        // the mouseDown(), mouseDrag(), and other methods.
        return super.handleEvent(e);
    
public booleanmouseDown(java.awt.Event e, int x, int y)

        last_x = x; last_y = y;
        return true;
    
public booleanmouseDrag(java.awt.Event e, int x, int y)

        Graphics g = canvas.getGraphics();
        g.drawLine(last_x, last_y, x, y);
        lines.addElement(new Line(last_x + offset_x, last_y + offset_y, 
                      x + offset_x, y + offset_y));
        last_x = x;
        last_y = y;
        return true;
    
public booleanmouseUp(java.awt.Event e, int x, int y)

  return true; 
public voidpaint(java.awt.Graphics g)

        Line l;
        for(int i = 0; i < lines.size(); i++) {
            l = (Line)lines.elementAt(i);
            g.drawLine(l.x1 - offset_x, l.y1 - offset_y, 
                      l.x2 - offset_x, l.y2 - offset_y);
        }
    
public synchronized voidreshape(int x, int y, int width, int height)

        // do the real stuff
        super.reshape(x, y, width, height);
        // Update our scrollbar page size
        Dimension hbar_size = hbar.size();
        Dimension vbar_size = vbar.size();
        canvas_width = width - vbar_size.width;
        canvas_height = height - hbar_size.height;
        hbar.setValues(offset_x, canvas_width, 0, 1000-canvas_width);
        vbar.setValues(offset_y, canvas_height, 0, 1000-canvas_height);
        hbar.setPageIncrement(canvas_width/2);
        vbar.setPageIncrement(canvas_height/2);
        this.update(canvas.getGraphics());