Java reset timertask based on a mouse movement - java

I have a Java Swing component that prints "hello" every 1 minute.
timer = new Timer();
timer.schedule(new HelloWordTask(), TIME, TIME);
However, I want to reset timertask when a user moves mouse.
int count = 0;
private CustomMouseAction extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
timer.cancel();
timer = new Timer();
timer.schedule(new HelloWordTask(), TIME, TIME);
System.out.println("moved " + count++);
}
}
Whenever I am moving a mouse, my timer gets rescheduled, which is the expected behaviour. However, it means that new Timer is created for every "bit" of movement. For example, when I move my mouse around 5 cm, ~30 print statements show up.
Is there a way to make the program more efficient? Moreover, would it be possible to somehow "reduce" the mouseMoved detection rate? For example, instead of being called 30 times, can I make it only being called 10 times (I assume if this was possible, there is a small chance to miss refresh chance).

Test with different n values until you're satisfied.
int count = 0, lastX = 0, lastY = 0, n = 30;
private CustomMouseAction extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
int ix= e.getX();
int iy= e.getY();
if (Math.abs(lastX-ix)<n && (Math.abs(lastY-iy)<n) return;
lastX= ix;
lastY= iy;
timer.cancel();
timer = new Timer();
timer.schedule(new HelloWordTask(), TIME, TIME);
System.out.println("moved " + count++);
}
}

For example, when I move my mouse around 5 cm, ~30 print statements show up.
Is there a way to make the program more efficient?
This is the whole point of event driven programming. The OS generates the event so you know the user is doing something. You can't control how frequently the events are generated. You can only control the processing you do when the event is generated.
However, it means that new Timer is created for every "bit" of movement.
You should not be using a util.Timer. Instead you should be using a javax.swing.Timer.
Don't keep creating a new Timer in the mouseMoved() method. Instead you should create and start the Timer when your frame is made visible. Then in the mouseMoved() method you just invoke the restart() method of the Timer.
See: Application Acvitity for a reusable class that you may find helpful.

Related

Changing the delay of the timer without creating a new one every time the method runs

So basically I have a timer method in my program which uses the integer z as it's parameter as well as the delay for the timer itself. But every time I run this method, it creates a new timer not deleting the old one. So I decided to add an if else block that made it so that it only created a timer on the first time but now it's saying that it might not have been initialized because it was initialized in the if else block. Can someone help me?
public void timer(int z) {
int count = 0;
Timer tester;
z = (60000 / z);
decide = true;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {noteDecider();}
};
if(count == 0) {
tester = new Timer(z, taskPerformer);
tester.start();
}
else {
tester.setDelay(z);
tester.start();
}
count++;
}
I would say that if you are concern about optimizing your code you should look into optimizing your Timer class. Like moving the ActionListener object inside Timer itself and more. All your timer(int z) method is doing is trying to use an object to keep track time not managing the lifecycle of Timer objects.

Make a timer in swing

For this game I'm making for java class, I want a timer display in the corner that counts up from 0
I made a variable that represents seconds, which ==> double s =0;
My idea was that using a Timer object where I could increment the variable every second and use the repaint() method on the string
However, I misinterpreted how the timer object works, and after the delay it counts up rapidly to infinity. I have no idea what to do now. A hint would be appreciated.
Here is my code,thanks for any help:
int delay = 5000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
s=(int)s+1;
}
};
new Timer(delay, taskPerformer).start();
String t = "" + s;
g.drawString(t, 100, 100);
repaint();
How about something like this: (you might have to change it for your needs)
boolean temp=true;
long startTime = System.nanoTime();
while(temp){
float time = (System.nanoTime()-startTime)/ 10000000.000f;
System.out.println(time);
}
What you can try doing is implementing the Runnable interface and then starting a new Thread of the class that you are using to draw your Components. Since Thread implements the Runnable interface, you will be able to create a new point of execution in the program where you can call a delay with Thread.sleep() before repainting your components. Here is an example of what you should try doing:
Thread thread = new Thread(ClassNameHere);
thread.start();
If you are interested in trying this out, more information can be found on the API on Threads and on the Runnable interface.

Pause game and add flashing text Java

I have this loop
while (true) {
game.update();
view.repaint();
Thread.sleep(DELAY);
}
In the game.update various components of the game have their position changed and those updates are reflected when the repaint() method is called on the view. The view extends JComponent and loops through the game objects and calls their print methods.
What I want to do is have a boolean called nextLevel in the game and if it's true Flash text on the screen for the player to notify them that they're going onto the next level. Maybe flash 4-5 times. Then continue the game.
Is this possible? I have been playing around with Thead.Sleep() but this only seems to pause the displaying and in the background the game is still going on.
Any ideas on how to do this?
Maybe you want to avoid threading by using a Timer object.
an example like that could be
int flashTimer = 0;
if(nextLevel) {
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//flash something method here
flashTimer++;
}
});
timer.start();
}
and then check your flashTimer if it reaches the number you want then just stop the timer by timer.stop();
Just an idea which seems to me a bit simpler. the 1000 value is milliseconds which is passed and executes the code inside the actionPerformed method every 1 sec.
Hope it helped

Keeping timer events equally spaced

I'm attempting to get an animation working in a game I'm developing. The animation works by setting a button size to very small, then gradually growing it to its normal size again. I have it working, except I'm having timing issues.
Sometimes the button will grow almost instantly, sometimes it goes VERY slow. I'm looking for something inbetween, and I need it to ALWAYS grow at that size, not some times fast sometimes slow.
I've looked into it and I found this pseudocode:
distance_for_dt = speed * delta_time
new_position = old_position + distance_for_dt
Unfortunately I don't understand what's being said, and I don't know how to apply this to my code. Can anyone help with that or explain what's being said in the above pseudocode?
Here's my timer code, timer is already defined above as a Timer, and z[] is just a pair of coordinates:
timer = new Timer(18, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Dimension dim = button[z[0]][z[1]].getSize();
if (dim.getHeight() < 79.9) {
button[z[0]][z[1]].setSize((int) (dim.getWidth() + 6), (int) (dim.getHeight() + 6));
} else {
button[z[0]][z[1]].setSize(80, 80);
timer.stop();
}
}
});
timer.start();
Depending on how many updates you're calling on your Swing application, it may be getting "backed up" and slowing down. For instance, if you wanted to accomplish the animation without a Timer, you could just do something like this:
// example method to do animation
public void animateButton(final int wait){
Thread thread = new Thread(){
public void run(){
// some loop structure to define how long to run animation
Dimension dim = button[z[0]][z[1]].getSize();
while (dim.getHeight() < 79.9){
SwingUtilities.invokeLater(new Runnable(){
//update Swing components here
});
try{ Thread.Sleep(wait); }
catch(Exception e){}
}
}
}
}
thread.start();
}
I think this may be similar to how a Timer updates the GUI, as Timers run on a separate thread. I would look into whether or not you need to use invokeLater(new Runnable) inside a timer to properly schedule the task. I had to do this to allow a project I was working on to keep responsive during long tasks. If you really needed to ensure the speed and maybe DROP updates to adjust for system lag, then you'll need to be calculating how complete the animation is vs how much time has passed, using a method call such as System.currentTimeMillis() or System.nanoTime(). Then, adjust accordingly for each step of the animation.

Displaying JLabel movement

I am attempting to create a simple animation in which a series of bubble rotate around a centre point. I have one version of animation where the bubbles spread from the centrepoint before they begin to rotate, which works fine, but as soon as I click one of the images (which sparks the animation) the screen freezes for a moment and then the bubbles appear in their end position, rather than showing each step they made.
What I have so far is:
while(bubble[1].getDegree() != 270)
{
long time = System.currentTimeMillis();
//the below if statement contains the function calls for
//the rotating bubble animations.
next();
draw();
// delay for each frame - time it took for one frame
time = (1000 / fps) - (System.currentTimeMillis() - time);
if (time > 0)
{
try
{
Thread.sleep(time);
}
catch(Exception e){}
}
}
public void draw()
{
for(int i = 1; i < bubble.length; i++)
{
iconLabel[i].setLocation(bubble[i].getX(), bubble[i].getY());
textLabel[i].setLocation((bubble[i].getX()+10),(bubble[i].getY()+10));
}
}
For clarity, the method "next()" merely changes the position of the bubble to the appropriate place, I know this to be functioning as I have had the animation work before but once I implemented the animation to JLabels it stopped working.
Any help would be appreciated.
The drawing is frozen because you block the event dispatch thread. Drawing is done in the same thread as the while loop, and since the loop prevents anything else happening while it's running, swing can get to drawing only after the loop is finished, so only the last position is drawn.
Use a swing Timer instead:
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// whatever you need for the animation
updatePositions();
repaint();
}
});
timer.start();
And then call timer.stop() when all the frames you need have been processed.

Categories

Resources