# 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
# 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 >
