Reputation: 11
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);
}
}
Upvotes: 0
Views: 124
Reputation: 168825
It has gotten to the stage where I won't touch a problem involving either applets or AWT until the person asking the question had given reasonable responses as to why they were using either. OTOH you have already partly fulfilled that, so here is a working example of animating the ball.
Look carefully at the differences between your code and this code.
import java.applet.*;
import java.awt.*;
public class TestCanvas extends Applet implements Runnable {
Panel myPanel = new Panel(new GridLayout());
int x = 0;
int y = 0;
int width = 100;
int height = 100;
boolean startBall = false;
myCanvas canvas = new myCanvas();
Thread t;
public void init() {
myPanel.add(canvas);
System.out.println("" + this.getLayout());
/* A Single component added to a no args GridLayout will be stretched
to fill the avilable space. */
this.setLayout(new GridLayout());
add(myPanel);
t = new Thread(this);
t.start();
}
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);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
x++;
y++;
g.setColor(Color.black);
g.fillOval(x, y, width, height);
}
}
}
Upvotes: 1