public void init() { Image img = getImage(getCodeBase(), "image.jpg"); }
public void paint(Graphics g) { Image img = getImage(getCodeBase(), "image.jpg"); g.drawImage(img, 0, 0, this); }
In this case, the applet itself, the Image object, and the x and y coordinates of (0, 0) are passed as parameters to the drawImage() method. This will cause the applet's top-left image to be drawn.
public void init() { try { Image img = getImage(new URL("http://example.com/image.jpg")); } catch (Exception e) { e.printStackTrace(); } }
The drawImage() method in this case receives as parameters the applet itself, the Image object, and the (0, 0) x and y coordinates. This will cause the applet's top-left image to be drawn.
It's crucial to keep in mind that images should load asynchronously in the init() method to avoid interrupting the applet's user interface. Use methods like getImage(), which load the image in the background and return right away, in order to achieve this. The handling of any exceptions that might be thrown when loading the image is something else you should remember to do.
import java.awt.*; import java.applet.*; public class ImageApplet extends Applet { Image img; public void init() { img = getImage(getCodeBase(), "image.jpg"); } public void paint(Graphics g) { g.drawImage(img, 0, 0, this); } }
Police Colony
Patna, Bihar
India
Email:
Post your comment