A
Alberto.V
Hi!
I have a simple animation question. I am trying to make an Oval move
from one position of a window to another. I have managed to make the
Oval to update itself and redraw in a new position. Unfortunatly the
previous position/drawing of the Oval does not disappear. What happens
is that the Oval moves through the window and leaves a trail after it.
I thought that the Panel (myPanel) would also redraw itself after a call
to repaint() and the previous drawing of the Oval would disappear.
My questions are:
- How do I remove the previous drawing of the Oval or make the Panel
(myPanel) update itself, so that just the Oval moves without a trail.
- Why isn't the background/Panel "black" even though I am calling the
setBackground(Color.BLACK); function ?
Thanks for any help
Alberto.V
========== Code follows below ==========
//
// Main Code
//
public class Main {
public static void main(String[] args){
JFrame gameFrame = new JFrame();
AVPanel p = new myPanel();
gameFrame.setSize(600, 600);
gameFrame.setLocation(150,50);
gameFrame.getContentPane().setBackground(Color.BLACK);
gameFrame.getContentPane().add(p);
gameFrame.setVisible(true);
}
}
//
// myPanel CODE
//
public class myPanel extends JPanel implements Runnable {
Vector balls = new Vector();
public myPanel() {
setBackground(Color.BLACK);
balls.add(new Ball(0, 0));
Thread runnerThread = new Thread(this);
runnerThread.setDaemon(true);
runnerThread.start();
}
// Starts a new Thread
public void run() {
while (true) {
try {
this.updateBalls();
this.repaint();
Thread.sleep(9);
} catch (InterruptedException ie) {}
}
}
public void paintComponent(Graphics g) {
int sz = balls.size();
for (int i=0;i < sz; i++) {
Ball b = (Ball)balls.get(i);
b.draw(g);
}
}
public void updateBalls() {
int sz = balls.size();
for (int i=0;i < sz; i++) {
Ball b = (Ball)balls.get(i);
b.updatePosition();
}
}
public void paint(Graphics g) {
int size = balls.size();
super.paint(g);
}
}
//
// Ball object
//
public class Ball {
int xposition, yposition;
int radius = 30;
// Constructor
public Ball(int x, int y){
xposition = x;
yposition = y;
}
// Draw the object
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(xposition, yposition, radius, radius);
}
// Update the objects position
public void updatePosition(){
Random r = new Random(System.currentTimeMillis() + xposition);
if ((r.nextInt(10) % 2) == 0){
xposition += 3;
} else {
yposition += 3;
}
}
}
I have a simple animation question. I am trying to make an Oval move
from one position of a window to another. I have managed to make the
Oval to update itself and redraw in a new position. Unfortunatly the
previous position/drawing of the Oval does not disappear. What happens
is that the Oval moves through the window and leaves a trail after it.
I thought that the Panel (myPanel) would also redraw itself after a call
to repaint() and the previous drawing of the Oval would disappear.
My questions are:
- How do I remove the previous drawing of the Oval or make the Panel
(myPanel) update itself, so that just the Oval moves without a trail.
- Why isn't the background/Panel "black" even though I am calling the
setBackground(Color.BLACK); function ?
Thanks for any help
Alberto.V
========== Code follows below ==========
//
// Main Code
//
public class Main {
public static void main(String[] args){
JFrame gameFrame = new JFrame();
AVPanel p = new myPanel();
gameFrame.setSize(600, 600);
gameFrame.setLocation(150,50);
gameFrame.getContentPane().setBackground(Color.BLACK);
gameFrame.getContentPane().add(p);
gameFrame.setVisible(true);
}
}
//
// myPanel CODE
//
public class myPanel extends JPanel implements Runnable {
Vector balls = new Vector();
public myPanel() {
setBackground(Color.BLACK);
balls.add(new Ball(0, 0));
Thread runnerThread = new Thread(this);
runnerThread.setDaemon(true);
runnerThread.start();
}
// Starts a new Thread
public void run() {
while (true) {
try {
this.updateBalls();
this.repaint();
Thread.sleep(9);
} catch (InterruptedException ie) {}
}
}
public void paintComponent(Graphics g) {
int sz = balls.size();
for (int i=0;i < sz; i++) {
Ball b = (Ball)balls.get(i);
b.draw(g);
}
}
public void updateBalls() {
int sz = balls.size();
for (int i=0;i < sz; i++) {
Ball b = (Ball)balls.get(i);
b.updatePosition();
}
}
public void paint(Graphics g) {
int size = balls.size();
super.paint(g);
}
}
//
// Ball object
//
public class Ball {
int xposition, yposition;
int radius = 30;
// Constructor
public Ball(int x, int y){
xposition = x;
yposition = y;
}
// Draw the object
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(xposition, yposition, radius, radius);
}
// Update the objects position
public void updatePosition(){
Random r = new Random(System.currentTimeMillis() + xposition);
if ((r.nextInt(10) % 2) == 0){
xposition += 3;
} else {
yposition += 3;
}
}
}