Results 1 to 7 of 7
Thread: Design choices...
- 12-19-2011, 05:29 PM #1
Design choices...
Hello, I'm new to developing GUI apps in Java (though been using regular Java for years) I've taught myself Swing and I get the gist of it but I have a few questions about general design choices:
- I want to make a program where the GUI app has a Start button, when the user presses it the main thread starts doing an animation, what is the best way to do this?
My solution was to have the main thread stuck in an endless loop waiting on a key object, when the button is pressed it calls the notify method of the key, like so:
Is this how everyone does it?Java Code:while(true){ keyObject.wait(); doTheAnimation(); } //The button's action listener method: void ActionPerformed(ActionEvent e){ keyObject.notify() } - Is Thread.sleep() the best method to use for doing animations?
- If I wanted to make a game in swing, I'd make the main thread be the main game-flow thread which in an endless loop sleeps for some small time and refreshes the game state by doing what each character needs to do, and there is a PipedInputStream for the main player's controls, whenever a keyboard button is pressed the event dispatcher thread puts the button in the PipedStream and when the main thread is refreshing it checks if there's a signal in the stream and if there is does the appropriate action for the player...
Is this the norm or is there a more optimal way?
Thanks in advanced...:)
- I want to make a program where the GUI app has a Start button, when the user presses it the main thread starts doing an animation, what is the best way to do this?
- 12-19-2011, 05:36 PM #2
Re: Design choices...
1- Why would you not just do:
2- It really depends. For simple animations, I would just use a Swing Timer.Java Code:void actionPerformed(ActionEvent e){ doTheAnimation(); }
3- Again, I would use a Swing Timer, at least at first.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-19-2011, 06:44 PM #3
Re: Design choices...
^ For 1, the animation involves alot of Thread.sleep() calls so it needs to be in it's own thread and not on the event dispatching thread...
Thanks for the answers on 2 and 3, but also about the key input method (for one thread to put the key commands in a PipedStream and the running thread to read them from there) is that a bad idea?
- 12-19-2011, 06:52 PM #4
Re: Design choices...
Why does your animation involve any calls to Thread.sleep? What I would do is create a Timer that fires however often you want your FPS to be (20-30 fps is probably reasonable), each time calling a step function. That step function updates whatever it needs to update and tells your animation JComponent to repaint.
Your step function can update the animation based on which step it's on, instead of using Thread.sleep. It can also update whatever it needs to based on booleans (such as upArrowPressed), which you set in keyPressed and keyReleased functions of a KeyListener, instead of the model you were describing.
From there, if you want to do more complicated logic or animation, you can move to using multiple threads. But for most "simple" games, a Swing Timer will work just fine.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-19-2011, 06:53 PM #5
Re: Design choices...
Java Code:void actionPerformed(ActionEvent e){ Thread t = new Thread(new Runnable() { public void run() { doTheAnimation(); // do on own thread } }); t.start(); }
- 12-19-2011, 08:00 PM #6
- 12-19-2011, 08:02 PM #7
Similar Threads
-
Program design
By phil128 in forum New To JavaReplies: 0Last Post: 01-24-2011, 05:48 PM -
multiple choices in oracle
By eng_hyzoom in forum New To JavaReplies: 2Last Post: 01-11-2011, 03:24 PM -
GUI design
By kennyblue in forum AWT / SwingReplies: 6Last Post: 11-10-2010, 05:59 PM -
help: I want to design 2D map?
By mofaker in forum AWT / SwingReplies: 4Last Post: 10-29-2009, 12:34 AM -
JSP Design.
By makpandian in forum NetBeansReplies: 0Last Post: 04-20-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks