GetImagepublic class GetImage extends JApplet This program, which can be an Applet or an Application,
shows a form of Toolkit.getImage() which works the same
in either Applet or Application! |
Methods Summary |
---|
public void | init()
loadImage();
| public void | loadImage()
// Applet-only version:
// Image = getImage(getCodeBase(), "Duke.gif");
// Portable version: getClass().getResource() works in either
// applet or application, 1.1 or 1.3, returns URL for file name.
URL url = getClass().getResource("Duke.gif");
image = getToolkit().getImage(url);
// Shorter portable version: same but avoids temporary variables
// image = getToolkit().getImage(getClass().getResource("Duke.gif"));
| public static void | main(java.lang.String[] args)
JFrame f = new JFrame("GetImage");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GetImage myApplet = new GetImage();
f.getContentPane().add(myApplet);
myApplet.init();
f.setSize(100, 100);
f.setVisible(true);
myApplet.start();
| public void | paint(java.awt.Graphics g)
g.drawImage(image, 20, 20, this);
|
|