Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Animating JLabels using Timers

Status
Not open for further replies.

owls

Instructor
Oct 10, 2002
23
US
I am attempting to edit an existing application that uses a timer to animate incrementing numbers in a JLabel. I am trying to figure out how to animate the position of the JLabel in the panel, as in the future I am planning on placing images in JLabels to animate. I have the code below for the application but it does not work. Can anybody tell me what I am doing wrong

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

/*
* A template for animation applications.
*/
public class Animator extends JFrame implements ActionListener {
int frameNumber = -1;
Timer timer;
boolean frozen = false;
JLabel label;
private XYLayout xYLayout1 = new XYLayout();
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout2 = new XYLayout();

Animator(int fps, String windowTitle) {
super(windowTitle);
int delay = (fps > 0) ? (1000 / fps) : 100;

//Set up a timer that calls this object's action handler.
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);

addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
startAnimation();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
getContentPane().setSize(800,800);
getContentPane().add(jPanel1);
label = new JLabel("Frame ");
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) {
frozen = false;
startAnimation();
} else {
frozen = true;
stopAnimation();
}
}
});

jPanel1.add(label, new XYConstraints(0,0,100,100));
}

//Can be invoked by any thread (since timer is thread-safe).
public void startAnimation() {
if (frozen) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (!timer.isRunning()) {
timer.start();
}
}
}

//Can be invoked by any thread (since timer is thread-safe).
public void stopAnimation() {
//Stop the animating thread.
if (timer.isRunning()) {
timer.stop();
}
}

public void actionPerformed(ActionEvent e) {
//Advance the animation frame.
frameNumber++;
label.setText("Frame " + frameNumber);
label.setLocation(frameNumber,frameNumber);
}

public static void main(String args[]) {
Animator animator = null;
int fps = 10;

//Get frames per second from the command line argument.
if (args.length > 0) {
try {
fps = Integer.parseInt(args[0]);
} catch (Exception e) {}
}
animator = new Animator(fps, "Animator with Timer");
animator.pack();
animator.setVisible(true);

//It's OK to start the animation here because
//startAnimation can be invoked by any thread.
animator.startAnimation();
}

public Animator() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jPanel1.setLayout(xYLayout2);
this.getContentPane().add(jPanel1, new XYConstraints(11, 9, 380, 222));
}
}
 
Are you editing the code in a version of JBuilder Pro? You need that for the borland xy layout package. As in...

import com.borland.jbcl.layout.*;

if not just comment out the above line and change...

private XYLayout xYLayout1 = new XYLayout();

private XYLayout xYLayout2 = new XYLayout();

to..

private FlowLayout xYLayout1 = new FlowLayout();

private FlowLayout xYLayout2 = new FlowLayout();

and change these bits...
jPanel1.add(label);//, new XYConstraints(0,0,100,100));
this.getContentPane().add(jPanel1);//, new XYConstraints(11, 9, 380, 222));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top