Chris Johnson, Index
Scrollpanes (AWT 1.1)


/*
Author:         C.W. Johnson
                johnson@dcs.gla.ac.uk

Last modified:  07/01/99

Creates a canvas and draws an image onto it.   This is then
placed onto a scrollpane.

*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class ScrollPaneExample extends Applet{
 
 private ImageCanvas anImageCanvas;
 private ScrollPane aScrollPane;
 
    boolean inAnApplet = true;
    String imageFile = "cables.jpg";
    Dimension preferredImageSize = new Dimension(450, 450);
    // size of the canvas on which the image is drawn
 
    //This method assumes this Applet is visible.
    public void init() {
        Image img;
        Dimension dimension;
 
    BorderLayout ImageBorderLayout = new BorderLayout();
 
        if (inAnApplet) {
            img = getImage(getCodeBase(), imageFile);
        } else {
            img = Toolkit.getDefaultToolkit().getImage(imageFile);
        }
 
    anImageCanvas = new ImageCanvas (img, preferredImageSize);
    aScrollPane = new ScrollPane (ScrollPane.SCROLLBARS_ALWAYS);
    aScrollPane.add(anImageCanvas, "Center");
 
    dimension = this.getSize();
    aScrollPane.setSize(dimension.width, dimension.height);
 
    this.add(aScrollPane);
 }//init
 
}
 
class ImageCanvas extends Canvas {
 Image image;
 
    ImageCanvas(Image img, Dimension prefSize) {
        super();
        image = img;
        this.setSize(prefSize.width, prefSize.height);
    }
 
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, getBackground(), this);
    }
}




forward