Painting on Top of Image

I'm trying to design an applet which simulates a radar screen. Although I'm a novice, I have managed to create the radar itself OK.

However, I would like the radar screen to "sit" on a "console", and I'm trying to use an image file as this background console. Although the image loads and displays AOK, I can't seem to draw the radar on top of it.

I have tried using both the Image and Graphics objects, for example:

public class radar extends Applet implements Runnable {

private Image img;

private Graphics buffer;

.....

// First get the 'console' image... this works AOK....

buffer=getImage(getDocumentBase(),"ar200.png");

// Now I need to paint the radar screen on top of the

// image, but it applet won't work when I add...

buffer=img.getGraphics();

// I've used getGraphics separately - without preloading

// an image - to create the animated radar screen, which

// works AOK, for example:

public void paint(Graphics g) {

// Set the radar screen grid circles

buffer.setColor(Color.gray);

buffer.drawOval(0, 0, aW, aH);

buffer.drawOval(aW/2/4*1, aH/2/4*1, aW/4*3, aH/4*3);

buffer.drawOval(aW/2/4*2, aH/2/4*2, aW/4*2, aH/4*2);

buffer.drawOval(aW/2/4*3, aH/2/4*3, aW/4*1, aH/4*1);

.... etc

g.drawImage (img,0,0,this);

.... etc

So, I can display an imported "console" image file, OR draw and display an animated radar screen - but not both! I think it might be an issue with combining "on-screen" and "off-screen" painting, but I'm not at all sure.

If anyone could suggest the method to accomplish this, or point me to an online resource that I can study, I'd really appreciate it.

MTIA, Max

[1774 byte] By [maxhugena] at [2007-9-27 23:32:26]
# 1

[nobr]> // Now I need to paint the radar screen on top of

> of the

>// image, but it applet won't work when I add...

>buffer=img.getGraphics();

>

I am surprised that this line above compiled. I don't think you can load an image and get its graphics directly. You can get graphics of an image only if it is created by the component. What you need is to create an image (off-screen), draw everything (background image and radar) on it and copy it to on-screen.

On top of my head, here is a code for your applet to do this stuff:

Image off, back;

Graphics offG;

public void init()

{

// create your offscreeen image

off = createImage(getSize().width, getSize().height);

offG = off.getGraphics();<br>

// load your background image

back = getImage(getDocumentBase(),"ar200.png");

}

public void paint(Graphics g)

{

// draw everything to your off screen first

offG.drawImage(back, 0, 0, this);

// draw your radar on top of it.

offG.setColor(Color.gray);

//...other radar stuff

// copy your off screen to on screen now

g.drawImage(off, 0,0, this);

}

ibosan

[/nobr]

ibosana at 2007-7-7 15:54:18 > top of Java-index,Other Topics,Java Game Development...
# 2
g.drawImage (img,0,0,this);buffer.setColor(Color.gray);buffer.drawOval(0, 0, aW, aH);buffer.drawOval(aW/2/4*1, aH/2/4*1, aW/4*3, aH/4*3);buffer.drawOval(aW/2/4*2, aH/2/4*2, aW/4*2, aH/4*2);buffer.drawOval(aW/2/4*3, aH/2/4*3, aW/4*1, aH/4*1);....
noah.wa at 2007-7-7 15:54:18 > top of Java-index,Other Topics,Java Game Development...
# 3

Thanks!

You're right, the code wouldn't compile as I described it.

Your advice to create an off-screen image first, draw to it, then copy to the on-screen image finally "switched on the light" in my head. <g>

Now, after re-reading the Sun Dev Graphics2D examples, it suddenly makes sense.

Again, many thanks for the help!

Cheers, Max

maxhugena at 2007-7-7 15:54:18 > top of Java-index,Other Topics,Java Game Development...