Baca lebih lanjut tentang Cara Menggunakan Pengatur Waktu Swing
Berikut contoh kodenya:
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
Temukan Contoh kode di sini
Kode contoh:(Pindahkan gambar 1 piksel secara horizontal dari kiri ke kanan pada interval 200 md )
private int x = 1;
...
final Image image = ImageIO.read(new File("resources/1.png"));
final JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,x,10,null);
}
};
int delay = 200; // milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x++;
if (x > 100) {
x = 1;
}
panel.repaint();
}
};
new Timer(delay, taskPerformer).start();