Results 1 to 9 of 9
  1. #1
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default how to control the speed of random characters with repaint method

    I have a question: I made a program which generates random characters with different font sizes and colors...

    The program works fine but the characters appear and disappear very fast. Is there a way to control the speed with which the characters appear?
    I used repaint() to update the screen and draw the next character..
    Please advise...

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to control the speed of random characters with repaint method

    You forgot to post your code attempt:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RandomCharacters extends JPanel {
    
      public void paintComponent( Graphics g ){
        super.paintComponent( g );
    
        int fontSize = ( int ) ( Math.random() * 200 );
        int x = ( int ) ( Math.random() * 1000 );
        int y = ( int ) ( 300 + Math.random() * 1000 );
        char characters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    
        //Create a Font object and set the font to serif plain	
        Font font = new Font( "Serif", Font.PLAIN, fontSize );
    
        //call methods to set the color, the font and draw the characters on screen
        g.setColor( new Color( ( float ) Math.random(),
        ( float ) Math.random(), ( float ) Math.random() ) );
        g.setFont( font );
        g.drawChars( characters, ( int ) ( Math.random() * 26 ), 1, x, y );
    
        repaint();
      }
    }
    First off, never call repaint() from within paint or paintComponent.

    Next off, I think that you may be looking to use a Swing Timer to allow you to do a simple animation with Swing.

  3. #3
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to control the speed of random characters with repaint method

    I didn't forget to add the code..I just didn't want to post it for integrity reasons.
    Why shouldn't I use repaint inside a paintcomponent?
    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to control the speed of random characters with repaint method

    Quote Originally Posted by dksellou View Post
    I didn't forget to add the code..I just didn't want to post it for integrity reasons.
    Why shouldn't I use repaint inside a paintcomponent?
    Thanks
    Because paintComponent and paint are for painting only and should not be used for animation as you're trying or for program logic. You do not have control over when or if that method gets called. Just please believe me and don't do this.

  5. #5
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to control the speed of random characters with repaint method

    Thanks
    please help me with the Timer ....I am all day in front of a java program today! It is not part of the assignment (deitel 15.10) so it is not about doing the homework for me...just curious!
    Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to control the speed of random characters with repaint method

    Quote Originally Posted by dksellou View Post
    please help me with the Timer ....I am all day in front of a java program today! It is not part of the assignment (deitel 15.10) so it is not about doing the homework for me...just curious!
    No problem, I'd be happy to help you to better understand use of Swing Timer. Just have a look at the Swing Timer Tutorial as it will show you pretty much all that you need to know with sample code as well. Then after studying the tutorial, give it a go in your own code. If you still are stuck, then please feel free to come back here, post your new code and you're questions, and we'll be more than happy and better able to give you specific help.

  7. #7
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to control the speed of random characters with repaint method

    I cannot figure out...
    I know I have to use this code..
    timer = new Timer(speed, this);
    timer.setInitialDelay(pause);
    timer.start();

    but what about the inner class?
    Do I have to change my whole code?
    I am so confused...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to control the speed of random characters with repaint method

    Yes you have to change the code. You should not use this for your Timer's ActionListener but rather simply create an anonymous inner class:

    Java Code:
    timer = new Timer(speed, new ActionListener() {
       public void actionPerformed(ActionEvent aEvt) {
          // your code goes in here
       }
    });
    But better to experiment with Swing Timers in small sample programs before trying to put it into your main program. Get used to using them first.

  9. #9
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to control the speed of random characters with repaint method

    I will do.
    Thanks for your quick response and help.
    :)

Similar Threads

  1. Why are Random Characters appearing in my output??
    By dragstang86 in forum New To Java
    Replies: 21
    Last Post: 09-23-2011, 04:15 AM
  2. Replies: 12
    Last Post: 09-23-2009, 10:14 AM
  3. Remove control characters in txt file
    By trivektor in forum New To Java
    Replies: 7
    Last Post: 09-23-2008, 05:22 PM
  4. How to remove Control Characters from an input file?
    By renjan in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2007, 04:33 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
  •