package chapter16; // ImageTest.java: Applet + Application import java.awt.*; // Graphics, Color, and Font import javax.swing.*; // JApplet and JPanel, Image Icon import java.net.URL; public class ImageTest extends JApplet { public void init() { JLabel labelImg = new JLabel(new ImageIcon("robo01a.jpg")); ImagePanel2 imp = new ImagePanel2(); JPanel p = new JPanel(); // for holding a lable and ImagePanel p.setLayout(new GridLayout(2,1)); p.add(labelImg); p.add(imp); getContentPane().add(p, BorderLayout.CENTER); } public static void main(String[] args) { JFrame f = new JFrame("I am an Appication"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageTest ha = new ImageTest(); // create an instance of HelloApplet3 f.getContentPane().add(ha, BorderLayout.CENTER); ha.init(); ha.start(); f.setSize(650,700); f.setVisible(true); } } class ImagePanel2 extends JPanel { private URL url = this.getClass().getResource("image/us.gif"); private ImageIcon iicon = new ImageIcon(url); Image img = iicon.getImage(); public void paintComponent(Graphics g) { super.paintComponent(g); // to clear the viewing area g.setFont( new Font("Arial", Font.BOLD, 16) ); g.drawString("The above is a label and the below is an image.", 20, 50); if(img != null) g.drawImage(img, 20, 80, getSize().width, getSize().height, this); } } // chung (c) 2000