Reviews : Java Books :
Learning Java : Chapter 14: Using Swing Components

Title: Learning Java
ISBN: 1565927184
Order No 7184
US Price: $ 34.95
Publication Date: May 2000
Pages: 722
© O'Reilly & Associates, Inc.
Author's Top Ten Tips and Tricks

Learning Java
Chapter 14: Using Swing Components

Here's an example that uses a JScrollPane to display a large image. The application itself is very simple; all we do is place the image in an ImageComponent, wrap a JScrollPane around it, and put the JScrollPane in a JFrame's content pane. Here's the code:

//file: ScrollPaneFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollPaneFrame {
  public static void main(String[] args) {
    String filename = "Piazza di Spagna.jpg";
    if (args.length > 0)
      filename = args[0];
    JFrame f = new JFrame("ScrollPaneFrame v1.0");
    f.setSize(300, 300);
    f.setLocation(200, 200);
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });

    Image image = Toolkit.getDefaultToolkit(  ).getImage(filename);
    f.getContentPane(  ).add(
      new JScrollPane(new ImageComponent(image)));
    f.setVisible(true);
  }
}

And here's the ImageComponent. It waits for the image to load, using a MediaTracker, and sets its size to the size of the image. It also provides a paint( ) method to draw the image. This takes a single call to drawImage( ). The first argument is the image itself; the next two are the coordinates of the image relative to the ImageComponent; and the last is a reference to the ImageComponent itself (this), which serves as an image observer. (We'll discuss image observers in Chapter 18, Working with Images and Other Media; for the time being, take this on faith.)

//file: ImageComponent.java
import java.awt.*;
import javax.swing.*;

public class ImageComponent extends JComponent {
  Image image;
  Dimension size;
  public ImageComponent(Image image) {
    this.image = image;
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 0);
    try {
      mt.waitForAll(  );
    }
    catch (InterruptedException e) {
      // error ...
    };

    size = new Dimension (image.getWidth(null),
                          image.getHeight(null));
    setSize(size);
  }
  public void paint(Graphics g) {
    g.drawImage(image, 0, 0, this);
  }
  public Dimension getPreferredSize(  ) {
    return size;
  }
}

Finally, ImageComponent provides a getPreferredSize( ) method, overriding the method it inherits from Component. This method simply returns the image's size, which is a Dimension object. When you're using JScrollPane, it's important for the object you're scrolling to provide a reliable indication of its size. Figure 14-6 shows the ScrollPaneFrame with the ImageComponent.

Figure 14-6. The ScrollPaneFrame application

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.