Some comments:
1 — You are using the older AWT, heavy–weight components, eg, Applet vis–a–vis the newer Swing, light–weight components, eg, JApplet. The AWT drawing is more involved and offscreen drawing, aka double–buffereing, is just about mandatory for descent animations. If you are committed to AWT drawing, okay, we can go ahead with it. Otherwise, I would definately switch to Swing (J–prefix) drawing.
2 — Working with images:
Loading your image in the "testi" class with
|
Code:
|
j = getImage(getCodeBase (), "rj_castle.gif"); |
the jvm (java virtual machine) will look for the image file in the same directory that your "testi" class file (compiled byte code) is in.
Look up the
getImage method in the Applet class api Method Detail section to see that the method returns immediately, ie, without waiting to load the image data. This is often not a problem for (a few) small images. The way to insure that the image data is loaded when using the antique
get/createImage methods is to use a MediaTracker. The MediaTracker class api comments section has an example of how to do this.
This way of loading images will not throw an exception if the file cannot be found, if the file extension (image type) is unsupported or if the image data is unreadable/corrupted. You have to use MediaTracker methods to inquire about the success/failure of the loading process.
Look up the
getImage method in the Toolkit api Method Detail section to see that image caching is a problem with this method and the recommendation to use
createImage instead. This is a problem with images in applets. On the subject of caching, applets can be cached in both browsers and the java plug-in (jre — java runtime environment) when run from/in html docs so it is preferable/easier to develop them with the appletviewer run from the prompt (see comments in applet file below).
|
Code:
|
// <applet code="TestiRx" width="200" height="200"></applet>
// use: >appletviewer TestiRx.java
import java.applet.Applet;
import java.awt.*;
public class TestiRx extends Applet
{
Image j;
@Override
public void init()
{
setSize(300,200);
// Image file is in the "images" folder which is in
// the current folder with this class (compiled) file.
j = getImage(getCodeBase (), //"rj_castle.gif");
"images/dukeWaveRed.gif");
}
@Override
public void paint(Graphics g)
{
g.drawImage(j, 0, 0, this);
}
} |
3 — In drawing it is usually a better idea to draw on a separate component and add it to the top–level container instead of drawing directly in/on the top–level container. There are exceptions to this.
4 — The basic way to do offscreen–drawing:
|
Code:
|
// <applet code="BasicOffscreen" width="400" height="300"></applet>
import java.applet.Applet;
import java.awt.*;
public class BasicOffscreen extends Applet {
BasicOffscreenPanel component = new BasicOffscreenPanel();
public void init() {
setLayout(new BorderLayout());
add(component, BorderLayout.CENTER);
}
}
class BasicOffscreenPanel extends Panel {
Image offscreen;
Graphics osg;
@Override
public void paint(Graphics g) {
// To make sure we get real values for width and height
// wait until this component is realized/showing before
// trying to initialize the offscreen image.
if(offscreen == null) {
int w = getWidth();
int h = getHeight();
offscreen = createImage(w, h);
// Get a reference to offscreens graphics context
// which we use for drawing in/on the image.
osg = offscreen.getGraphics();
// Default background will be black, let's fill it.
osg.setColor(Color.white);
osg.fillRect(0,0,w,h);
// Draw some text.
osg.setColor(Color.blue);
osg.drawString("hello world", 100, 100);
}
// Draw offscreen.
g.drawImage(offscreen, 0, 0, this);
}
} |
5 — To alter the image later (in event code):
to erase anything, fill over it with the background color,
or you can fill the entire background;
to add something, set a color and draw it.