I REALLY NEED HELP HERE!
I need some help with getting my animation to work correctly. I have tried every method I can find online and none of them seems to work fully. My ‘game’ so far is just the framework to get a ball to bounce on the screen. I have a problem that the ball is very jittery (not flicker). It seems as though every .5-3 seconds the image jumps instead of smoothly moving. It appears as though the system stop painting it for a short period or something. I have tried many things including double buffering, using accelerated graphics, running the animation on the main thread, running the animation on a separate thread, etc. None of these approaches seems to fix the problem. I have now tested this with my home computer (Win XP PIII 800 512MB, etc) and it is still happenening.
Here is the code for the Main Canvas
import java.awt.Canvas;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
public class MyGame2 extends Canvas implements Runnable {
private BufferStrategy strategy;
private Ball theBall;
private Thread animator;
private boolean gameRunning = true;
public MyGame2() {
JFrame container = new JFrame("Chad Game");
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(Vars.WIDTH,Vars.HEIGHT));
panel.setLayout(null);
setBounds(0,0,Vars.WIDTH,Vars.HEIGHT);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setResizable(false);
container.setVisible(true);
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
container.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
initEntities();
startThread();
}
private void startThread() {
if (animator == null || !gameRunning) {
animator = new Thread(this);
animator.start();
}
}
private void initEntities() {
theBall = new Ball();
theBall.fire(400,250,10,10);
}
public void run() {
double lastLoopTime = System.nanoTime();
double nextFrameTime = lastLoopTime + 15000000;
double currTime = lastLoopTime;
while (gameRunning) {
double delta = System.nanoTime() - lastLoopTime;
lastLoopTime = System.nanoTime();
nextFrameTime = lastLoopTime + 15000000;
currTime = lastLoopTime;
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,Vars.WIDTH,Vars.HEIGHT);
double percent = delta/1000000000;
theBall.update(percent);
theBall.draw(g);
strategy.show();
while (currTime < nextFrameTime) {
Thread.yield();
try {
Thread.sleep(1);
} catch (Exception e) {}
currTime = System.nanoTime();
}
}
}
public static void main(String argv[]) {
new MyGame2();
}
}
Here is the code for the ball
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Ball
{
private boolean alive;
private double x, y;
private double vX, vY;
private int radius;
private Image icon;
private Image img;
public Ball()
{
alive = false;
radius = 10;
icon = new ImageIcon("ball.gif").getImage();
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
img = gc.createCompatibleImage(icon.getWidth(null),icon.getHeight(null),Transparency.BITMASK);
img.getGraphics().drawImage(icon,0,0,null);
} // end of Ball()
public void fire(double vex, double vye, int ex, int ye) {
vX = vex;
vY = vye;
x = ex;
y = ye;
alive = true;
//System.out.println("xVector: " + vX + "\n" + "yVector: " + vY);
}
public void update(double percent) {
if(!alive)
return;
x = x + percent*vX;
y = y + percent*vY;
if(x <= 0.0 || x + (double)radius*2 >= (double)Vars.WIDTH)
vX = -vX;
if(y <= 0.0 || y + (double)radius*2 >= (double)Vars.HEIGHT)
vY = -vY;
}
public void draw(Graphics g) {
if(!alive)
return;
g.drawImage(img,(int)x,(int)y,null);
}
} // end of Ball class
And finally the Vars class for screen width
public class Vars
{
public static int WIDTH = 600;
public static int HEIGHT = 400;
} // end of Vars class
Any advice would be much appreciated. If you want to run the code just put all the files in the same directory as any image with the name “ball.gif”.
Thanks