home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 1.8 KB | 84 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
-
- /*
- * an applet that allows an image to be resized
- */
- public class ImageSize extends Applet implements ActionListener {
-
- /*
- * the image to draw
- */
- Image img;
- Button btnSmaller;
- Button btnBigger;
-
- /*
- * the current width and height of the image
- */
- double width, height;
-
- /*
- * the image size scale factor
- */
- double scale = 1.0;
-
-
- /*
- * called when the applet is loaded or reloaded
- */
- public void init() {
-
- setLayout(new BorderLayout());
- Panel p = new Panel ();
- add("South", p);
- btnSmaller = new Button("Smaller");
- p.add (btnSmaller);
- btnBigger = new Button("Bigger");
- p.add (btnBigger);
- btnSmaller.addActionListener(this);
- btnBigger.addActionListener(this);
-
- MediaTracker tracker = new MediaTracker (this);
-
- img = getImage (getDocumentBase(), "T1.gif");
- tracker.addImage (img, 0);
- showStatus ("Getting image: T1.gif");
- try {
- tracker.waitForID (0);
- } catch (InterruptedException e) { }
- width = img.getWidth (this);
- height = img.getHeight (this);
- }
-
- public void paint (Graphics g) {
- double w, h;
-
-
- w = scale * width;
- h = scale * height;
-
- // if we don't have the size yet, we shouldn't draw
- if (w < 0 || h < 0) { w=75; h=75; } //return;
-
- // explicitly specify width (w) and height (h)
- g.drawImage (img, 10, 10, (int) w, (int) h, this);
- }
- public void actionPerformed(ActionEvent evt)
- {
- Object object1 = evt.getSource();
- if (object1 == btnSmaller)
- {
- scale *= 0.9; // make it 10% smaller
- repaint ();
- }
- if (object1 == btnBigger)
- {
- scale *= 1.1; // make it 10% bigger
- repaint ();
- }
- }
-
- }
-