Canvas paint

I've made an "image component" (button with image) that extends Canvas and an "image container" (panel with image) that extends Panel. The problem is that when i add a partially transparent GIF image to an image component and add it to the canvas the transparent areas of the image component show the canvas background color not the canvas picture. How can this be avoided?
[378 byte] By [mbutu] at [2007-11-15 20:12:43]
# 1 Re: Canvas paint
Is your image not loaded completely ? If the image object is null, the image may not be shown on
the canvas. Try to use the class MediaTracker to force your image loading before you drawImage()
them on the canvas. If this is not the case, you may consider to post your code.
good luck,
Alfred Wu
kib63613 at 2007-11-10 2:53:39 >
# 2 Re: Canvas paint
Thanx for the answer but...
The image is not null and when added to the panel the image component is visible (i use MediaTracker).
The only thing i've done is that i've implemented my own paint() methods in the Canvas and Panel classes so that the images are drawn on the components. The problem is that the transparent areas of the GIF show the panel background color not the image that is drawn on the Panel with the canvas paint() method.

Is it possible to paint the background image before the components are drawn?
mbutu at 2007-11-10 2:54:40 >
# 3 Re: Canvas paint
MButu,
An image can be loaded and painted without the use of a java.awt.Canvas.
It is at the Panel level that you want to paint the image, so that the transparent areas of the image will reveal the background of the Panel, not the Canvas.

Hope this helps.
Regards,
dogBear
dogbear at 2007-11-10 2:55:42 >
# 4 Re: Canvas paint
I've tried this case. As you mentioned, I encounter the same problem as yours. I write the following
code which works fine when you use appletviewer. I don't know why it is ok if I do it as the code
writen. It is just fine in appletviewer. It does not work in Communicator 4.7 due to the waitForAll() or
wiatForID() not working.

// Import Package
// JDK package
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

// private package


//
/////////////////////////////////////////////////////////////////////
class MyCanvas extends Canvas {
Image my_image = null;

//
//
public MyCanvas(Image img) {
super();
my_image = img;
} // end of public MyCanvas(Image img)
//
//
public void paint(Graphics g) {
if(my_image != null) {
g.drawImage(my_image, 0, 0, getSize().width, getSize().height, null);
} // end of if(my_image != null)
} // end of public void paint(Graphics g)

//
//
public void update(Graphics g) {
paint(g);
} // end of public void update(Graphics g)

//
//
public void paintIt(Graphics g, Image img) {
my_image = img;
// repaint();
paint(g);
} // end of public void paintIt(Image img)
} // end of class MyCanvas extends Canvas

class MyPanel extends Panel {
Image my_image = null;

//
//
public MyPanel(Image img) {
super();
my_image = img;
} // end of public MyPanel(Image img)
//
//
public void paint(Graphics g) {
if(my_image != null) {
g.drawImage(my_image, 0, 0, getSize().width, getSize().height, null);
} // end of if(my_image != null)
} // end of public void paint(Graphics g)

//
//
public void update(Graphics g) {
paint(g);
} // end of public void update(Graphics g)

//
//
public void paintIt(Graphics g, Image img) {
my_image = img;
// repaint();
paint(g);
} // end of public void paintIt(Image img)
} // end of class MyPanel extends Panel

public class BkImage extends Applet {

// declaration of variables

// constant definition

// thread variables

// component variables
Image img_canvas = null;
Image img_panel = null;
MyCanvas my_canvas = null;
MyPanel my_panel = null;
MediaTracker mt = null;
Graphics gp_canvas = null;
Graphics gp_panel = null;

// state variables

// common variables

/////////////////////////////////////////////////////////////////////

// declaration of private functions

/////////////////////////////////////////////////////////////////////

// declaration of public functions
//
//
public void init() {
img_canvas = getImage(getCodeBase(), "gif0003.gif");
img_panel = getImage(getCodeBase(), "gif0015.gif");

mt = new MediaTracker(this);
mt.addImage(img_canvas, 1);
mt.addImage(img_panel, 1);

try {
mt.waitForAll();
} // end of try
catch(InterruptedException ie) {
} // end of catch(InterruptedException ie)
my_canvas = new MyCanvas(img_canvas);
my_panel = new MyPanel(img_panel);
} // end of public void init()

//
//
public void start() {
setLayout(null);
my_canvas.setBounds(20, 20, 200, 200);
my_canvas.setBackground(new Color(0, 200, 200));
my_panel.setLayout(null);
my_panel.setBounds(0, 0, getSize().width, getSize().height);
my_panel.setBackground(new Color(200, 200, 0));

my_panel.add(my_canvas);
add(my_panel);
gp_canvas = my_canvas.getGraphics();
gp_panel = my_panel.getGraphics();
my_canvas.paintIt(gp_canvas, img_canvas);
my_panel.paintIt(gp_panel, img_panel);
} // end of public start()

//
//
public void stop() {
} // end of public void stop()

//
//
public void destroy() {
} // end of public void destroy()

//
//
public void run() {
} // end of public void run()

/////////////////////////////////////////////////////////////////////

// declaration of Event Handlers of Listeners

} // end of public class BkImage extends Applet {
/////////////////////////////////////////////////////////////////////

good luck,
Alfred Wu
kib63613 at 2007-11-10 2:56:37 >
# 5 Re: Canvas paint
I found a way around the problem, here's the solution. This code is from the image component.

initGraphics calls the containers paint function which paints it's background on the image component.

public boolean initGraphics()
{
System.out.println("initGraphics()");
Rectangle rectangle = getBounds();
parentImage = createImage(size.width, size.height);
Graphics g1 = parentImage.getGraphics();
Container container = getParent();
if (container != null)
{
g1.translate(-rectangle.x, -rectangle.y);
g1.setClip(rectangle.x, rectangle.y, size.width, size.height);
container.update(g1);
g1.translate(rectangle.x, rectangle.y);
}
return true;
}

public void paint( Graphics g )
{
if (!initPerformed)
initPerformed = initGraphics();
System.out.println("paint component");
if (image!=null)
{
g.drawImage( parentImage, 0, 0, this);
g.drawImage( image, 0, 0, this );
}
else loadImage();
}
mbutu at 2007-11-10 2:57:41 >