Im trying to move an object, but I want the object to move every couple of seconds.
I heared of the java script "time" or "timer"
But im not sure how it goes..
Thanks,
Im trying to move an object, but I want the object to move every couple of seconds.
I heared of the java script "time" or "timer"
But im not sure how it goes..
Thanks,
mentioned you’re using Java in BlueJ; pointed toward a timer-based approach. For Swing GUIs the simplest, safest way to move a visible object at regular intervals is to use javax.swing.Timer so the update runs on the Event Dispatch Thread. Below is a minimal, copyable example (moves a circle every 2 seconds) plus practical tips to avoid common mistakes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MoveDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame("Move");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MovePanel());
f.setSize(400, 200);
f.setVisible(true);
});
}
}
class MovePanel extends JPanel {
private int x = 0;
private final Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
x += 20;
if (x > getWidth()) x = 0;
repaint();
}
});
MovePanel() {
timer.setInitialDelay(0);
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, getHeight()/2 - 10, 20, 20);
}
} Notes and troubleshooting: the Timer delay is in milliseconds (2000 = 2s). Do not use Thread.sleep on the EDT — it freezes the UI. If you need background work use java.util.concurrent.ScheduledExecutorService; if you use java.util.Timer and then update Swing components, wrap UI changes in SwingUtilities.invokeLater. Common fixes: call timer.start() (or setInitialDelay(0) for an immediate first move), call repaint() after changing coordinates, shorten the delay for smoother motion, and keep heavy computation out of the timer callback to avoid lag.
Jump to Post— Ezzaral 2,714If you are using JavaScript, then this question would be better answered in that forum:
http://www.daniweb.com/forums/forum117.htmlIf you are using pure Java, then you can use javax.Swing.Timer
Jump to Post— Ezzaral 2,714ej whats up i see you are talking about moving an object can you help me with my home work its about an matrix in wich an element moves around pleaseeeeeeeeeeeee if you can help me
Please keep unrelated questions to their own thread. Also, homework help is given when …
If you are using JavaScript, then this question would be better answered in that forum:
http://www.daniweb.com/forums/forum117.html
If you are using pure Java, then you can use javax.Swing.Timer
ej whats up i see you are talking about moving an object can you help me with my home work its about an matrix in wich an element moves around pleaseeeeeeeeeeeee if you can help me
ej whats up i see you are talking about moving an object can you help me with my home work its about an matrix in wich an element moves around pleaseeeeeeeeeeeee if you can help me
Please keep unrelated questions to their own thread. Also, homework help is given when effort is shown, so keep that in mind when you post a thread asking for help.
yes thanks for the advice but can you help me
Others have already answered this in your original thread http://www.daniweb.com/forums/thread103716.html.
You need to post what you have done so far and specific questions about it - in that thread, not this one.
No, it's not javascipt.
It's Java language.
Im using an application called BlueJ.
Thanks anyway.
Does anyone know how to do that in Java language?
Read the API link that I posted for Swing Timer. There is a link "How to use Timers" in the documentation for that class. If you have futher questions about applying it, post them back here with the code you have so far.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.