Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Printing different text each second (Timers etc)

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Printing different text each second (Timers etc)

    Hello, let me just start with saying that this is my first post and I just registered, so forgive me if this is in the wrong section or if I'm doing something wrong.

    I have created a simple game which writes a few lines of text if you press a "fight"-button. It basically says "Player hits enemy for 2 dmg" and "Enemy hits player for 3 dmg" and repeats that until either the player or the enemy has gone below 1 hp and lost. As of now, all the lines of text are printed instantly after pressing the button. What I want to do is have my program print "Player hits enemy for 2 dmg" after one second, and a second after that print "Enemy hits player for 3 dmg" and repeat for as long as the fight keeps going.

    All this is done in a JFrame, with JTextAreas, if that makes any difference.

    I've experimented a lot with "Sleep", "Wait" and different timers and I haven't been able to make it work correctly, so I came here to ask for help. Again, this is my first post and I just came to these forums so forgive me if I did something wrong.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Printing different text each second (Timers etc)

    I recommend you read through this tutorial: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Then post an SSCCE demonstrating what you've tried with the Swing Timer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    3dAndersson (April 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing different text each second (Timers etc)

    Quote Originally Posted by KevinWorkman View Post
    I recommend you read through this tutorial: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Then post an SSCCE demonstrating what you've tried with the Swing Timer.
    Thanks! I'll try this.

Similar Threads

  1. problem with printing to text area
    By gatorsgirl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 25th, 2012, 10:06 AM
  2. printing simple 3d text
    By cutie in forum Java Theory & Questions
    Replies: 3
    Last Post: September 1st, 2011, 07:31 AM
  3. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM

Tags for this Thread