Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Any clever solution?

  1. #1
    Callofdudey is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Any clever solution?

    I want to draw a simple animation, a huge problem would be, as we all know, today's computer are lightening fast, so if I do a loop, I'll have to use the thread.sleep method.


    I think this method, though textbook-simple, is still pointless. I mean why wasting all those precious CPU time on something completely meaningless?

    So what I'm thinking is to pull something like a timer or some sort, an "active" timer, when the timer hit its set value, it tells the program to draw a new frame actively and positively, and during this period, the program can do something else, if there is something else to do.


    You don't have to provide no code, all I'm asking is a clue, a lead, a link or the name of a framework or a library.

    Thanks in advance!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Any clever solution?

    Bookmark (or even better: download) the Java SE API documentation and read about the several Timer classes; you define an interval time and a piece of code to be run and give it to a Timer object; at regular intervals your piece of code will be run so it can make your GUI draw a next image or whatever.

    kind regards,

    Jos
    sunde887 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Any clever solution?

    Swing includes a Timer class which this. You create a timer which "ticks" every n milliseconds and you tell the timer what action to perform on each tick.

    Look for "timer" in Oracle's JavaTutorial.

  4. #4
    Callofdudey is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Any clever solution?

    Thanks, esp. JosAH, hope you don't mind being bombarded with these stupid questions.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Any clever solution?

    I don't mind questions, and I'm sure others don't. If you'd like a fairly decent tutorial to get you a bit familiar with some animations, check out: Programing An Applet Game Of Pong - Java Tutorials | Dream.In.Code

    It's a fairly good tutorial, some things to consider if you do this:
    1. Define only one class for both paddles(abstract the class).
    2. Don't use setters in the constructor, and avoid recursive calls in the setter(it's unnecessary).
    3. Draw on a buffered image and make it work on a JPanel instead of an applet.

    It's a pretty good exercise and should be simple enough to follow and will give you some good ideas for later projects.

Similar Threads

  1. Clever glimpse needed with this recursion
    By Yakg in forum New To Java
    Replies: 14
    Last Post: 02-10-2011, 10:15 PM
  2. Replies: 3
    Last Post: 07-01-2010, 09:39 PM
  3. Please I need the solution to this
    By debobbt in forum New To Java
    Replies: 4
    Last Post: 12-18-2009, 05:34 AM
  4. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 02:43 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 02:27 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
  •