TCLUG Development Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG-DEVEL:174] AWT



Create a Panel that draws an image in the background and then just add things
to the panel.

Here's one I use frequently, but it uses Swing, MUCH better than AWT.  It's
based on AWT, but is lighter weight.

public class ImageScrollPanel extends JPanel {

  public static void main(String[] args) {
    javax.swing.JFrame frame = new javax.swing.JFrame();
    frame.setSize(640, 480);
    String filename =
"com.honeywell.htc.schedinfra/gui/images/refineryDay.gif";
    filename = filename.replace('/', java.io.File.separatorChar);
    ImageScrollPanel panel = new ImageScrollPanel(filename);
    frame.getContentPane().add(panel);

    frame.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent we) {
        System.exit(0);
      }
    });
    frame.show();
  }

  /**
     @param filename the name of the file that holds the image.
     @pre (filename != null)
  **/
  public ImageScrollPanel(String filename) {
    super();
    setup(new ImageIcon(filename));
  }

  /**
     @pre (url != null)
  **/
  public ImageScrollPanel(URL url) {
    super();
    setup(new ImageIcon(url));
  }

  /**
     @pre (image != null)
  **/
  protected void setup(ImageIcon image) {
    setLayout(new java.awt.GridLayout(1, 1));

    JLabel imageLabel = new JLabel(image);
    imageLabel.setSize(image.getIconWidth(), image.getIconHeight());
    JScrollPane scroller = new JScrollPane(imageLabel,
                                           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    add(scroller);
  }

}

-- 
Jon Schewe 
http://eggplant.mtu.net/~jpschewe
schewe@tcfreenet.org