File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Java Timer and Fixed Frame Rate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Java Timer and Fixed Frame Rate" Watch "Java Timer and Fixed Frame Rate" New topic
Author

Java Timer and Fixed Frame Rate

Gary Drocella
Greenhorn

Joined: May 23, 2012
Posts: 2
Hello All! I'm trying to implement a game which requires blocks falling from the top of the grid, and No it's not Tetris. I imagined it would be very simple using the built in Java TimerTask, but after beginning to develop the game I'm having second thoughts. This is because at each *step* it goes through each player and moves their respective stub (blocks) down the grid until it hits the bottom, that is, the bottom of the grid or sits on top other previously fallen stubs (blocks). I would like to note that the graphics would be rigid in the sense that I am planning to have the grid represented as a 2D Matrix of JPanel/JComponent that are just color coded. So the movement of the stubs would be essentially setting the color of the previous set of blocks to white and then the next set of blocks to respective colors of the stub.

What I'm worried about is that when the stub hits the bottom of the grid, there will be some relatively heavy computation (NP complete) that could potentially decrease the throughput of the game. I am thinking about having a dispatch thread to perform the computation for the appropriate players who need the task performed.

I should also note, that the player will have the option of holding the down arrow to increase the speed of the stub to reaching the bottom.

Anyway, I think this game would be insanely fun. I'm definitely going to add a server/client model to it. Here are my questions, and I am open to suggestions

1. Is the Java TimerTask reliable for implementing a Fixed Frame Rate?

2. Should I use Fixed Frame Rate at all? or should I use Variable Frame Rate?

3. Would it ultimately be better to go the extra mile, and go from rigid motion to smoother motion?

Any help is greatly appreciated thank you.
Gary Drocella
Greenhorn

Joined: May 23, 2012
Posts: 2
Anyone?
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
It sounds like you haven't decided how your game will work yet, and you keep moving the goal post farther and farther making things harder on yourself with more work.

TimerTask's running speed depends on your CPU and the randomness of thread scheduling.

TimerTask is no more or less accurate to schedule than anything else in Java. Java isn't a real-time system so you should expect a few milliseconds of delay in either direction.
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
Also, Swing has only 1 thread to do both GUI updates and Event handling.

You must use the Swing thread to do GUI updates, but take the heavy computations out of your Event Listeners or it can freeze the GUI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Timer and Fixed Frame Rate