ScrollableScribblepublic 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 = new Canvas();
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 boolean | handleEvent(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 boolean | mouseDown(java.awt.Event e, int x, int y)
// Get canvas origin to find relative mouse position //MSG
Point pt = canvas.location(); //MSG
last_x = x-pt.x; last_y = y-pt.y; //MSG
return true;
| public boolean | mouseDrag(java.awt.Event e, int x, int y)
Graphics g = canvas.getGraphics();
// Get canvas origin to find relative mouse position //MSG
Point pt = canvas.location(); //MSG
x -= pt.x; //MSG
y -= pt.y; //MSG
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 boolean | mouseUp(java.awt.Event e, int x, int y) return true;
| public void | paint(java.awt.Graphics g)
Line l;
Graphics canvas_g = canvas.getGraphics();
for(int i = 0; i < lines.size(); i++) {
l = (Line)lines.elementAt(i);
canvas_g.drawLine(l.x1 - offset_x, l.y1 - offset_y,
l.x2 - offset_x, l.y2 - offset_y);
}
| public synchronized void | reshape(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());
|
|