File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Why does this still flicker? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why does this still flicker?" Watch "Why does this still flicker?" New topic

Why does this still flicker?

Prashant P

Joined: Dec 06, 2000
Posts: 2
I have the code to simulate multiple ball movements.
Sorry its kinda long but can anyone help me fix this problem or tell me a better way of doing it.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Simulation extends JFrame implements Runnable {
public int _height;
public int _width;
public int _xnumberOfCells;
public int _ynumberOfCells;
protected int radius;
protected Color color;
private Thread myThread;
Vector ballsCollection = new Vector();
public Simulation(int xNumberOfCells, int yNumberOfCells) {
_xnumberOfCells = xNumberOfCells;
_ynumberOfCells = yNumberOfCells;
_height = _ynumberOfCells * 30 + 100;
_width = _xnumberOfCells * 30 + 100;
this.setSize(_width, _height);

public void paint(Graphics g) {
int x=0;
int y=0;
for(int i=0; i<= _xnumberOfCells; i++){
g.drawLine(x, 0, x, _height);
x = x + 30;
for(int j=0; j <= _ynumberOfCells; j++) {
g.drawLine(0, y, _width, y);
y = y + 30;
Ball us;
for(int i=0;i<ballsCollection.size ();i++){>
us = (Ball) ballsCollection.elementAt(i);
public void start()
if (myThread == null) {
myThread = new Thread(this); // Create a new thread
myThread.start(); // Start the new thread

public void run() {
}catch(InterruptedException ie) {ie.printStackTrace();}
public void addBall(Ball us){
ballsCollection.add (us); // only one Ball for now
public void update(Graphics g)

public static void main(String[] args) {
Simulation ws = new Simulation(10,10);
Ball u1 = new Ball(100,100);
ws.addWindowListener(new WindowAdapter() {
public void WindowClosingEvent(WindowEvent e) {
MoverThread mv = new MoverThread(u1,ws);
mv.start ();

class Ball {
public int xpos;
public int ypos;
public int xvel=10;
public int yvel=10;
public Ball(int xpos , int ypos) {
this.xpos = xpos; this.ypos = ypos;
public void draw(Graphics g){
g.setColor ( );
g.fillOval (xpos,ypos,20,20);
public void move(){
int rand1, rand2;
rand1 = (int)(Math.random()*100);
rand2 = (int)(Math.random()*100);
if( rand1 % 2 == 0){
rand1 = -rand1;
if( rand2 % 2 != 0){
rand2 = -rand2;
// System.out.println("r1:=" + rand1);
// System.out.println("r2:=" + rand2);
xpos += (int)(rand1/5);
ypos += (int)(rand2/5);

class MoverThread extends Thread{
Ball us;
JFrame jf;
public MoverThread(Ball us,JFrame jf){ = us;
this.jf = jf;
public void run(){
}catch(Exception e){

I want to keep ball a separate class..
any help is appreciated.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
If I'm not mistaken for all these types of multiple object movements or animation types its better to use the MediaTracker class to minimize flicker problems.

- Vinod<br />-------<br />SCJP2
Prashant P

Joined: Dec 06, 2000
Posts: 2
I thought that media tracker can be used only for Images and audio files. I donot have any images in my animation. Can someone suggest a solution?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Forget MediaTracker. You need to use double buffering and eliminate the super.paint(g). Here is your new paint() method.

[This message has been edited by Thomas Paul (edited December 07, 2000).]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Why does this still flicker?
Similar Threads
Update a JPanel with runnables there are created at runtime
Using Graphics in a BorderLayout
change pixel value to cell coordinate
How do I save a variable value -Unsolved-
Fluid Motion Moving Circles Smooth -solved-