I really can't figure this out and I need your help. I've created a simple applet. I have a panel and a canvas. I draw on the canvas. I add the canvas to the panel. I add the panel to the applet. I'm trying to draw a circle on canvas but I can't see it. If I resize the applet, I can see the circle flickering behind the canvas. Please help.
import java.applet.*;
import java.awt.*;
public class TestCanvas extends Applet implements Runnable
{
Panel myPanel = new Panel();
int x = 50;
int y = 50;
int width = 100;
int height = 100;
boolean startBall = false;
Graphics bufferGraphics;
Image offscreen;
Thread t;
public void init()
{
canvas.setSize(500,500);
//canvas.setMaximumSize(new Dimension(500,500));
//canvas.setMinimumSize(new Dimension(50,50));
myPanel.add(canvas);
add(myPanel);
//For double buffering
offscreen = createImage(canvas.getWidth(),canvas.getHeight());
bufferGraphics = offscreen.getGraphics();
}
public void start()
{
t = new Thread();
t.start();
startBall = true;
}
public void stop()
{
t = null;
}
public void destroy()
{
t = null;
}
public void run()
{
for ( ; ; )
{
try
{
canvas.repaint();
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
class myCanvas extends Canvas
{
private static final long serialVersionUID = 1L;
public myCanvas()
{
setBackground(Color.yellow);
setSize(500, 300);
//setBounds(0, 0, 500, 300);
}
}
myCanvas canvas = new myCanvas();
public void paint(Graphics g)
{
g = canvas.getGraphics();
bufferGraphics.clearRect(0,0,canvas.getWidth(), canvas.getHeight());
bufferGraphics.setColor(Color.black);
//x++;
//y++;
bufferGraphics.setColor(Color.black);
bufferGraphics.fillOval(x, y, width, height);
g.drawImage(offscreen,0,0,null);
}
public void update(Graphics g)
{
canvas.paint(g);
}
}