Practice pieces


Networks

This practice piece actually mainly about just applet construction and getting hold of the URL representing the directory the Applet is served from.


Hints:

You just need one, Applet, class to do this job, plus a webpage to embed it in.

First, make a basic Applet, and call setBackground(Color.PINK) in its init so you can see it is properly embedded in the webpage. To get the Applet running out of your personal webspace, you'll need the instructions for making this if you have access to the system at Leeds.

Next, use getCodeBase() to get the directory the Applet was sent from as a URL.

Use the URL's getHost() and getPath() methods to get the host and path from which the Applet was sent, and use this, plus the filename for your image (assuming it is in the same directory) to construct a new URL string. Use this to make a new URL object.

You should now have everything you need to use getImage() to get the image. Attach it to an instance variable label, and it should then be available to draw in a paint method.

If you need more help, here's the answers (or one option, anyhow).