Results 1 to 9 of 9
- 11-18-2012, 05:41 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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
-
Re: how to control the speed of random characters with repaint method
You forgot to post your code attempt:
First off, never call repaint() from within paint or paintComponent.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(); } }
Next off, I think that you may be looking to use a Swing Timer to allow you to do a simple animation with Swing.
- 11-18-2012, 05:55 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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
-
Re: how to control the speed of random characters with repaint method
- 11-18-2012, 06:06 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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
-
Re: how to control the speed of random characters with repaint method
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.
- 11-18-2012, 05:40 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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...
-
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:
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.Java Code:timer = new Timer(speed, new ActionListener() { public void actionPerformed(ActionEvent aEvt) { // your code goes in here } });
- 11-18-2012, 06:02 PM #9
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Why are Random Characters appearing in my output??
By dragstang86 in forum New To JavaReplies: 21Last Post: 09-23-2011, 03:15 AM -
Need help generating random numbers atleast 2 characters apart
By ruby&oliver in forum New To JavaReplies: 12Last Post: 09-23-2009, 09:14 AM -
Remove control characters in txt file
By trivektor in forum New To JavaReplies: 7Last Post: 09-23-2008, 04:22 PM -
How to remove Control Characters from an input file?
By renjan in forum Advanced JavaReplies: 0Last Post: 08-01-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks