Simple Animation Question

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;
}
}

}
 
A

Andrew Thompson

Alberto.V said:
Hi!

I have a simple animation question. I am trying to make an Oval move
from one position of a window to another.

And for future reference, comp.lang.java.gui
specialises in this sort of thing.

I would recommend getting hold of the c.l.j.g
FAQ as well

HTH
 
A

Alberto.V

Andrew Thompson said:
...

Component.setOpaque(true);

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site

Hi!

Where do I put this code? I tried to put this code in the myPanel Class:

public AVPanel() {
setOpaque(true); <----------
balls.add(new Ball(0, 0));

. . . . . . rest of code


and in the main() method with a call to myPanel.setOpaque(true);

But neither helped.


Alberto.V
 
A

Andrew Thompson

Alberto.V said:
....
Where do I put this code?

You need both setBackground _and_ setOpaque
in the component..
......
public myPanel() {
setBackground(Color.BLACK);
setOpaque(true);
balls.add(new Ball(0, 0));
......
 
A

Alberto.V

Andrew Thompson said:
...
Where do I put this code?

You need both setBackground _and_ setOpaque
in the component..
.....
public myPanel() {
setBackground(Color.BLACK);
setOpaque(true);
balls.add(new Ball(0, 0));
.....

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
[/QUOTE]

Hi!

Thanks for your help. I tried that but it didn't make any difference,
the background still isnt't black.


Alllberto.V
 
A

Andrew Thompson

Alberto.V said:
Thanks for your help. I tried that but it didn't make any difference,
the background still isnt't black.

I think we need a fresh copy of the source you
are using Alberto, just to be sure we are all on
the same page (have same understanding).
 
A

Alberto.V

Andrew Thompson said:
I think we need a fresh copy of the source you
are using Alberto, just to be sure we are all on
the same page (have same understanding).

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site

Ok, Here comes the sourcecode. It's just 3 files/objects, very simple.

Alberto.V


==== main.java ====

public class Main {

public static void main(String[] args){
JFrame gameFrame = new JFrame();
myPanel p = new myPanel();

gameFrame.setSize(600, 600);
gameFrame.setLocation(150,50);
gameFrame.getContentPane().setBackground(Color.BLACK);
gameFrame.getContentPane().add(p);

gameFrame.setVisible(true);

}
}

==== myPanel.java ====

public class myPanel extends JPanel implements Runnable {
Vector balls = new Vector();

public myPanel() {
setBackground(Color.BLACK);
setOpaque(true);
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(90);
} 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);

}
}

==== Balls.java ====

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;
}
}

}
 
A

Andrew Thompson

Corrected source, note the changes..
_________________________________
import java.awt.*;
import java.util.*;
import javax.swing.*;

public class AnimatePanel extends JPanel implements Runnable {
Vector balls = new Vector();

public AnimatePanel() {
setBackground(Color.BLACK);
setOpaque(true);
balls.add(new Ball(0, 0));
balls.add(new Ball(50, 50));
balls.add(new Ball(100, 100));
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(90);
} catch (InterruptedException ie) {}
}
}

/* Straight from the JDocs..
..if you do not invoker super's implementation
you must honor the opaque property, that is if this
component is opaque, you must completely fill in the
background in a non-opaque color.. */
public void paintComponent(Graphics g) {
super.paintComponent(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);

}
*/
public static void main(String[] args){
JFrame gameFrame = new JFrame();
AnimatePanel p = new AnimatePanel();

gameFrame.setSize(600, 600);
gameFrame.setLocation(150,50);
gameFrame.getContentPane().setBackground(Color.BLACK);
gameFrame.getContentPane().add(p);

gameFrame.setVisible(true);

}
}

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;
}
}

}
_________________________________
HTH
 
A

Alberto.V

Andrew Thompson said:
// Starts a new Thread
public void run() {
while (true) {
try {
this.updateBalls();
this.repaint();
Thread.sleep(90);
} catch (InterruptedException ie) {}
}
}

Thank you very much, now it works!


Alberto.V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top