Reputation: 98
What is the best way to make a JLabel automatically move to a coordinate location? Is there a predefined method for such task?
Example: instead of label.setLocation(x + 1, y + 1); (re-run to move) Something like: Example: label.moveTo(500, 500); or a way to find the best path/route?
I've read the java documentation on Oracle and can't find anything like such.
Upvotes: 0
Views: 1625
Reputation: 347184
This is a basic example of moving a JLabel
from one position to another over a period of time.
This is a time based animation, meaning that the speed of the animation is dependent on how long you want the animation to play. The distance a object needs to move will also effect the speed.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MoveLabel {
public static void main(String[] args) {
new MoveLabel();
}
public MoveLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
protected static final int PLAY_TIME = 4000;
private JLabel label;
private int targetX;
private int targetY;
private long startTime;
private final int startX;
private final int startY;
public TestPane() {
setLayout(null);
label = new JLabel("Testing");
label.setSize(label.getPreferredSize());
add(label);
Dimension size = getPreferredSize();
startX = 0;
startY = 0;
targetX = (size.width - label.getSize().width) / 2;
targetY = (size.height - label.getSize().height) / 2;
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int x = label.getX();
int y = label.getY();
long duration = System.currentTimeMillis() - startTime;
float progress = (float)duration / (float)PLAY_TIME;
if (progress > 1f) {
progress = 1f;
((Timer)(e.getSource())).stop();
}
x = startX + (int)Math.round((targetX - startX) * progress);
y = startY + (int)Math.round((targetY - startY) * progress);
label.setLocation(x, y);
}
});
startTime = System.currentTimeMillis();
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
Upvotes: 1