home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch12
/
ui
/
tree
/
scrollPanel.java
< prev
next >
Wrap
Text File
|
1997-01-02
|
5KB
|
242 lines
package ui.tree;
import java.awt.*;
import ui.tree.*;
public class scrollPanel extends Panel
{
protected treeCanvas tCanvas;
protected Scrollbar vertBar ;
protected Scrollbar horzBar ;
protected int imgWidth ;
protected int imgHeight ;
protected int imgX ;
protected int imgY ;
protected int page ;
public scrollPanel (TreeTool tool)
{
setBackground(Color.white);
imgX = 0 ;
imgY = 0 ;
vertBar = new Scrollbar( Scrollbar.VERTICAL ) ;
horzBar = new Scrollbar( Scrollbar.HORIZONTAL ) ;
tCanvas = new treeCanvas(tool) ;
tCanvas.startObserving();
while ((imgHeight = tCanvas.getImageHeight()) == -1 )
{
// loop until image loaded
}
while ((imgWidth = tCanvas.getImageWidth()) == -1 )
{
// loop until image loaded
}
//System.out.println("image height = " + imgHeight + " Image width = " + imgWidth);
GridBagLayout gridbag = new GridBagLayout();
setLayout( gridbag ) ;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH ;
c.weightx = 1.0;
c.weighty = 1.0;
gridbag.setConstraints(tCanvas, c);
add( tCanvas ) ;
c.fill = GridBagConstraints.VERTICAL ;
c.gridwidth = GridBagConstraints.REMAINDER ;
gridbag.setConstraints(vertBar, c);
add( vertBar ) ;
c.fill = GridBagConstraints.HORIZONTAL ;
c.gridwidth = 1 ;
gridbag.setConstraints(horzBar, c);
add( horzBar ) ;
}
// used to clear the screen of all nodes except root node
public void clearScreen(TreeNode n)
{
tCanvas.removeNode(n);
}
/**
* Creates a new SelfValidatingTextField.
*
* @parm x new x pos
* @parm y new y pos
* @parm width new width
* @parm height new height
*/
public synchronized void reshape(int x,
int y,
int width,
int height) {
super.reshape( x, y, width, height ) ;
Dimension d = tCanvas.getImageSize();
imgWidth = d.width;
imgHeight = d.height;
if ( width > imgWidth + vertBar.bounds().width ) {
horzBar.disable() ;
} else {
horzBar.enable() ;
Rectangle bndRect = bounds() ;
int barWidth = vertBar.preferredSize().width ;
int max = imgWidth - (bndRect.width - barWidth);
page = max/10 ;
int oldMax = horzBar.getMaximum() ;
if ( oldMax == 0) {
imgX = 0 ;
} else {
imgX = (int)(((float)imgX/(float)oldMax) * (float)max) ;
}
horzBar.setValues( imgX, page, 0, max ) ;
horzBar.setPageIncrement( page ) ;
}
if (height > imgHeight + horzBar.bounds().height) {
vertBar.disable() ;
} else {
vertBar.enable() ;
Rectangle bndRect = bounds() ;
int barHeight = horzBar.preferredSize().height ;
int max = imgHeight - (bndRect.height - barHeight) ;
page = max/10 ;
int oldMax = vertBar.getMaximum() ;
if ( oldMax == 0) {
imgY = 0 ;
} else {
imgY = (int)(((float)imgY/(float)oldMax) * (float)max) ;
}
vertBar.setValues( imgY, page, 0, max ) ;
vertBar.setPageIncrement( page ) ;
}
}
/**
* Over rise Compnent.handleEvent
*
* @parm e Event to be handled
*/
public boolean handleEvent(Event e) {
if ( e.target == horzBar ) {
switch( e.id ) {
case Event.SCROLL_PAGE_UP:
case Event.SCROLL_LINE_UP:
case Event.SCROLL_ABSOLUTE:
case Event.SCROLL_LINE_DOWN:
case Event.SCROLL_PAGE_DOWN:
imgX = horzBar.getValue() ;
tCanvas.repaint();
return true ;
}
} else if ( e.target == vertBar ) {
switch( e.id ) {
case Event.SCROLL_PAGE_UP:
case Event.SCROLL_LINE_UP:
case Event.SCROLL_ABSOLUTE:
case Event.SCROLL_LINE_DOWN:
case Event.SCROLL_PAGE_DOWN:
imgY = vertBar.getValue() ;
tCanvas.repaint();
return true ;
}
}
return super.handleEvent(e) ;
}
};
/**
* A class that draws an Image
*
* @version 1.0 96/04/29
* @author David Chung
*/
class ImageCanvas extends Canvas {
Image canvasImg ;
/**
* Create an ImageCanvas object
*
* @parm img Image to be displayed
*/
public ImageCanvas( Image img ) {
canvasImg = img ;
}
/**
* Paint the Canvas
*
* @parm g Graphics context
*/
public void paint(Graphics g) {
g.drawImage( canvasImg,
-1 * ((scrollPanel)getParent()).imgX,
-1 * ((scrollPanel)getParent()).imgY,
this ) ;
}
}