Simple Animation Question

Discussion in 'Java' started by Alberto.V, Dec 9, 2003.

  1. Alberto.V

    Alberto.V Guest

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

    }
     
    Alberto.V, Dec 9, 2003
    #1
    1. Advertising

  2. Andrew Thompson, Dec 9, 2003
    #2
    1. Advertising

  3. "Alberto.V " <> wrote in message
    news:...
    > 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

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 9, 2003
    #3
  4. Alberto.V

    Alberto.V Guest

    In article <A7iBb.45895$>,
    "Andrew Thompson" <> wrote:

    > "Alberto.V " <> wrote in message
    > news:...
    > ...
    > > - Why isn't the background/Panel "black" even though I am calling the
    > > setBackground(Color.BLACK); function ?

    >
    > 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
     
    Alberto.V, Dec 9, 2003
    #4
  5. "Alberto.V " <> wrote in message
    news:...
    > In article <A7iBb.45895$>,
    > "Andrew Thompson" <> wrote:
    >
    > > "Alberto.V " <> wrote in message
    > > news:...
    > > ...
    > > > - Why isn't the background/Panel "black" even though I am calling the
    > > > setBackground(Color.BLACK); function ?

    > >
    > > Component.setOpaque(true);

    ....
    > 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
     
    Andrew Thompson, Dec 9, 2003
    #5
  6. Alberto.V

    Alberto.V Guest

    In article <l6mBb.46193$>,
    "Andrew Thompson" <> wrote:

    > "Alberto.V " <> wrote in message
    > news:...
    > > In article <A7iBb.45895$>,
    > > "Andrew Thompson" <> wrote:
    > >
    > > > "Alberto.V " <> wrote in message
    > > > news:...
    > > > ...
    > > > > - Why isn't the background/Panel "black" even though I am calling the
    > > > > setBackground(Color.BLACK); function ?
    > > >
    > > > Component.setOpaque(true);

    > ...
    > > 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
    >
    >


    Hi!

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


    Alllberto.V
     
    Alberto.V, Dec 9, 2003
    #6
  7. "Alberto.V " <> wrote in message
    news:...
    > In article <l6mBb.46193$>,
    > "Andrew Thompson" <> wrote:

    ...
    > 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).

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 9, 2003
    #7
  8. Alberto.V

    Alberto.V Guest

    In article <hatBb.46477$>,
    "Andrew Thompson" <> wrote:

    > "Alberto.V " <> wrote in message
    > news:...
    > > In article <l6mBb.46193$>,
    > > "Andrew Thompson" <> wrote:

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

    }
     
    Alberto.V, Dec 10, 2003
    #8
  9. 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

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 10, 2003
    #9
  10. Alberto.V

    Alberto.V Guest

    In article <vrIBb.47623$>,
    "Andrew Thompson" <> wrote:

    > // 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
     
    Alberto.V, Dec 11, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John D.
    Replies:
    5
    Views:
    852
    John D.
    Nov 2, 2005
  2. Sudhin

    error in simple animation

    Sudhin, Jul 19, 2006, in forum: Java
    Replies:
    2
    Views:
    406
    Andrew Thompson
    Jul 19, 2006
  3. John Doe
    Replies:
    7
    Views:
    333
    Daniel Pitts
    Nov 19, 2006
  4. Replies:
    7
    Views:
    1,552
    osmium
    Mar 26, 2006
  5. softwaredeveloper

    How to make simple animation on web

    softwaredeveloper, Sep 27, 2006, in forum: Javascript
    Replies:
    1
    Views:
    105
    webEater
    Sep 27, 2006
Loading...

Share This Page