Results 1 to 7 of 7
  1. #1
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default 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:

    1. 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:

      Java Code:
      while(true){
            keyObject.wait();
            doTheAnimation();
      }
      
      //The button's action listener method:
      void ActionPerformed(ActionEvent e){
           keyObject.notify()
      }
      Is this how everyone does it?
    2. Is Thread.sleep() the best method to use for doing animations?
    3. 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...:)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Design choices...

    1- Why would you not just do:

    Java Code:
    void actionPerformed(ActionEvent e){
         doTheAnimation();
    }
    2- It really depends. For simple animations, I would just use a Swing Timer.

    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!

  3. #3
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default 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?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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();
    }

  6. #6
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Design choices...

    ^ That won't do because it will create a new thread each time the start button is pressed...

    Quote Originally Posted by KevinWorkman View Post
    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.
    Thanks for the info, I guess I'll need to look into those timers then...

    Thanks again...

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Design choices...

    You could disable the "Start" button if you do NOT want another thread to be started. It all depends on how you have designed your app and how you want it to run.

Similar Threads

  1. Program design
    By phil128 in forum New To Java
    Replies: 0
    Last Post: 01-24-2011, 05:48 PM
  2. multiple choices in oracle
    By eng_hyzoom in forum New To Java
    Replies: 2
    Last Post: 01-11-2011, 03:24 PM
  3. GUI design
    By kennyblue in forum AWT / Swing
    Replies: 6
    Last Post: 11-10-2010, 05:59 PM
  4. help: I want to design 2D map?
    By mofaker in forum AWT / Swing
    Replies: 4
    Last Post: 10-29-2009, 12:34 AM
  5. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •