This is my code.
import java.awt.*;
import java.util.*;
import java.applet.*;
public class Main extends Applet implements Runnable
{
Thread th;
//double buffering
private Image dbImage;
private Graphics dbg;
//Reference
private Ball redBall;
private Ball blueBall;
public void init()
{
setBackground(Color.black);
redBall = new Ball(45, 20, 10, 1, 1, Color.red);
blueBall = new Ball(390, 345, 10, -1, -1, Color.blue);
}
public void start()
{
th = new Thread (this);
th.start ();
}
public void stop()
{
th.stop();
}
public void destroy()
{
th.stop();
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true)
{
redBall.moveBall();
blueBall.moveBall();
repaint();
try
{
Thread.sleep (15);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g)
{
redBall.drawBall(g);
blueBall.drawBall(g);
}
public void update(Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
}
import java.awt.*;
import java.applet.*;
public class Ball
{
private int xpos;
private int ypos;
private int radius;
private int vx; //vector x
private int vy; //vector y
Color color;
public Ball(int x, int y, int r, int vx, int vy, Color color)
{
xpos = x;
ypos = y;
radius = r;
this.vx = vx;
this.vy = vy;
this.color = color;
}
public void drawBall(Graphics g)
{
g.setColor (color);
g.fillOval (xpos - radius, ypos - radius, 2 * radius, 2 * radius);
}
public void moveBall()
{
xpos += vx;
ypos += vy;
}
}

New Topic/Question
Reply



MultiQuote







|