The Zoom Applet:
Code:
//*******************************************************************
//
// Zoom.java
//
//*******************************************************************
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Zoom extends Applet {
private final int SIZE = 300;
private Scrollbar bar =
new Scrollbar(Scrollbar.HORIZONTAL, 10, 25, 10, SIZE);
private Zoom_Listener listener = new Zoom_Listener (this);
private int current_size = 10;
private Image picture;
//===========================================================
// Sets up the applet and retrieves the image.
//===========================================================
public void init() {
setLayout (new BorderLayout());
setBackground (Color.white);
bar.addAdjustmentListener (listener);
picture = getImage (getDocumentBase(), "owl.gif");
add (bar, "South");
setSize (SIZE, SIZE);
setVisible (true);
} // method init
//===========================================================
// Repaints the image after adjusting its size.
//===========================================================
public void zoom_image (int size) {
current_size = size;
repaint();
} // method zoom_image
//===========================================================
// Paints the image, scaled to the current size.
//===========================================================
public void paint (Graphics page) {
page.drawImage (picture, SIZE/2-current_size/2,
SIZE/2-current_size/2, current_size, current_size, this);
} // method draw
} // class Zoom
class Zoom_Listener implements AdjustmentListener {
private Zoom applet;
//===========================================================
// Sets up the listener object by setting a reference to
// the applet.
//===========================================================
public Zoom_Listener (Zoom zoom_applet) {
applet = zoom_applet;
} // constructor Zoom_Listener
//===========================================================
// Changes the image size in response to a scrollbar
// adjustment event.
//===========================================================
public void adjustmentValueChanged (AdjustmentEvent event) {
applet.zoom_image (event.getValue());
} // method adjustmentValueChanged
} // class Zoom_Listner