This topic is now archived and is closed to further replies.


[java] Java Animation

Recommended Posts

d_aelion    122
Hey guys! Im writing a physics simulator that graphs and animates quantum waves. I need a way to animate the wave and catch events (key press/button) to stop the wave, but all the code I have seen on threading causes problems when I integrate it into my code and I have yet to see a way use them. It would be cool to just do it in a loop in the main event thread and just check if keys were pressed, but I don''t think that is possible without catching events. I would rather not put the animation in a thread, but perhaps threading the events would work. Any ideas? Help!

Share this post

Link to post
Share on other sites
harleyrana    122
I have just been working on a particle system, so i''ve got some stuff that should help.
Email me if you have any questions!

public class SimFrame extends JFrame implements Runnable, KeyListener {

Simulator sim;
boolean running = false;
int repaint_delay = 60; /* For a sim thats probally to low but whatever you want*/
Thread repaintThread;

Image bufferImage;

int elapsed_time = 0;
long last_frame_time = System.currentTimeMillis();

// You could use an array, but this is less error prone
boolean isUpKeyDown = false;
boolean isDownKeyDown = false;
boolean isLeftKeyDown = false;
boolean isRightKeyDown = false;

public SimFrame(){

bufferImage = this.createImage(600,600);

repaintThread = new Thread(this);
running = true;


public static void main(String[] args){
new SimFrame();

public void run(){ // Only method from Runable you need.


// Now we have can easily see which keys have been pressed
// do what ever!

// This is the interesting part!
/* First you need to establish how much time elasped
between your last frame, and use that to scale your
results when you intergrate them

long time = System.currentTimeMillis();
int time_delta = (int)((time - last_frame_time) / 1000);
elapsed_time += time_delta;
last_frame_time = time;

sim.processUpdate(time_step); /* Intergrate your values with the elapsed time between frames */
Graphics g = bufferImage.getGraphics();
sim.renderScene(g); /* pass the graphics object to the sim
so it can draw its current state */

repaint(); //this will call your update(Graphics);


}catch(InterruptedException ie){}



public void update(Graphics g){
// Now we need to move the buffer onto the sceen

public void keyReleased(java.awt.event.KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){

isUpKeyDown = false;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){

isDownKeyDown = false;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){

isLeftKeyDown = false;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){

isRightKeyDown = false;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_SPACE){

isPrimaryFireDown = false;
}else if(keyEvent.getKeyCode() == KeyEvent.CTRL_MASK){

isSecondaryFireDown = false;

public void keyPressed(java.awt.event.KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){

isUpKeyDown = true;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){
isDownKeyDown = true;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){

isLeftKeyDown = true;
}else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){

isRightKeyDown = true;

public void keyTyped(java.awt.event.KeyEvent keyEvent) {


Share this post

Link to post
Share on other sites