Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Simple Animation Question

Thread view: 
Alberto.V - 09 Dec 2003 14:04 GMT
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;
     }
  }

}
Andrew Thompson - 09 Dec 2003 15:55 GMT
> Hi!
>
> I have a simple animation question.

Hi Alberto!

I advised you to post over here, but perhaps
I should have stressed 'in future' more.

What you have done here is 'multi-post',
which is very difficult.

So, my advice is..
Post NEW qns on GUI to c.l.j.g
Post NEW qns on java in c.l.j.p

If you are really unsure as to which your message
belongs in, cross-post, this sends messages
to both at the same instant, and keeps the
thread of the conversation together.

On _this_ one, please see my response
c.l.j.p

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.