Delay Using Key Bindings
Jan 2, 2014
I got Key Bindings for a game I'm working on. They're supposed to move objects when keys are pressed.
(currently, each println(..) represents actual code to move things on the screen, will be added later).
The printing in the console works, but with delays. The words "left", "right", etc. appear with a delay in the console, sometimes half a second delay, sometimes a few seconds delay.
There is no delay at all sometimes, but when a lot of keys are pressed one after the other, there's a delay of several seconds, and only then the words suddenly appear in the console (like the computer had too much to handle at the same time).
When I did the same Key Bindings in a different project without all the game logic, only pressing buttons that print words in the console - it worked perfectly without delay.
So I suspect the problem is something with the game code, or the way I used Key Bindings inside the game code.
I tried putting the Key Bindings code in the thread that contains the game-loop (before the game-loop), and in the constructor. Same problem.
Here is the relevant code of the Board class (extending JPanel), the main JPanel of the game which displays the graphics and manipulates objects.
Java Code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements Runnable {
Tank tank1,tank2;
boolean[] keysPressed1,keysPressed2;
[Code] ....
As I said, I suspect the program demands too much of the computer, so pressing a lot of keys one after the other, freezes the program for several seconds, and then suddenly prints the words on the console.
View Replies
Jun 17, 2014
Is there any way in JavaFX 8 to remove all change/invalidation listeners on an observable? And is there any way to remove all bindings on a property?
View Replies
View Related
Sep 26, 2014
It is possible to delay the creation of a file until I know I am writing to it.
View Replies
View Related
Dec 4, 2014
We're twisting our minds how to use predicate bindings correctly in the real world, i. e. beyond the trivial examples for FilteredList using simply static code but no bindings!
The problem is that our predicate must be bound to a chain of BooleanBindings, of which the final term needs the item injected into the predicate by the FilteredList. Example see purple code:
BooleanBinding a = ...
StringBinding b = ...
ObjectBinding<Predicate> c = Bindings.createObjectBinding(() -> item -> a.or(b.isEqualTo(item.someProperty())).get(), a, b); // Ugly: No "Bindings" style!
myFilteredList.predicateProperty().bind(c);
This code has an ugly smell! It first looks like "Bindings" style, but in fact is plain old lamba mostly! But it also is slow: The code enforces splitting of a and b into separate bindings as it enforces rebuilding the chain a.or(b.isEqualTo(...)) for each single iteration of titem in turn. That induces unnecessarily creating and garbage-collecting Bindings "on the fly", which is not how Bindings are intended -- they shall be created once and simply update their value instead of getting replaced themselves to prevent wasting CPU cycles and keep memory clean.
How to do Predicate Bindings correctly (i. e. without temporarily building Bindings for each "t") ...
View Replies
View Related
Nov 16, 2014
I'm trying to write a program the simulates the time delay of an elevator door and I want to know how I can add an interactive time in delay in Java.
I know that this is how you delay a Java program:
try {
Thread.sleep(60000);
}
catch(InterruptedException ex) {
}
But how can I make it so that the user can make the timer last longer or end faster through user input.
View Replies
View Related
Jul 7, 2014
So I'm trying to make an applet and I found that Thread.sleep() to simply delay is a bad idea.
I'm not sure how to use the Timer class to implement a same version of sleep() to delay 6 seconds. I am trying to do this in a for loop to delay before every loop. How can I implement this?
This code is after the init() method. For simplicity I didn't include. Assuming I did:
Timer t = new Timer(6000, null);
public void actionPerformed(ActionEvent e) {
try {
URL rsUrl = new URL("http://rscript.org/lookup.php?type=namecheck&name=" + n1);
BufferedReader br = new BufferedReader(new InputStreamReader(rsUrl.openStream()));
for (count = 0; count < maxCount; count++) {
[Code] ....
View Replies
View Related