aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why does this still flicker?" Watch "Why does this still flicker?" New topic
Author

Why does this still flicker?

Prashant P
Greenhorn

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) {
super.paint(g);
setBackground(Color.white);
g.setColor(Color.blue);
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);
us.draw(g);
}
}
public void start()
{
if (myThread == null) {
myThread = new Thread(this); // Create a new thread
myThread.start(); // Start the new thread
}
}

public void run() {
try{
myThread.sleep(50);
repaint();
}catch(InterruptedException ie) {ie.printStackTrace();}
}
public void addBall(Ball us){
ballsCollection.add (us); // only one Ball for now
start();
}
public void update(Graphics g)
{
paint(g);
}

public static void main(String[] args) {
Simulation ws = new Simulation(10,10);
Ball u1 = new Ball(100,100);
ws.addBall(u1);
ws.addWindowListener(new WindowAdapter() {
public void WindowClosingEvent(WindowEvent e) {
System.exit(0);
}
});
ws.setVisible(true);
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) {
super();
this.xpos = xpos; this.ypos = ypos;
}
public void draw(Graphics g){
g.setColor (Color.green );
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){
this.us = us;
this.jf = jf;
}
public void run(){
while(true){
try{
super.sleep(100);
}catch(Exception e){
}
us.move();
jf.repaint();
}
}
}

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

Joined: Dec 06, 2000
Posts: 148
Hi,
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


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

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?
thanks
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: http://aspose.com/file-tools
 
subject: Why does this still flicker?