Results 1 to 11 of 11
Thread: Random Numbers generator
- 04-06-2010, 11:23 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Random Numbers generator
Hi guyz , ive been learning applet for 3 weeks,
i got a question , im tryin to create an application that generates random numbers n then show the generated numbers in applet viewer .
first lets take a look at my code :
as you can i already know the way to generate random numbers , however i dont know the correct way to show'em in applet viewer , as i try to run the project the numbers are shown in java output but not in the applet viewer.Java Code:import java.awt.*; import java.applet.*; import static java.lang.Math.*; public class Random extends Applet { static final String message="JAVA GUI"; private Font font; public void init() { font=new Font("Arial",Font.BOLD,13); } public void paint(Graphics g) { int num,z,a; for (z=1;z<=10;z++){ int x=(int)(Math.random()*10000%10); for (a=1;a<=x;a++){ } g.setFont(font); g.drawString(message, 180,100); } } }
would you help me on this ? tnx :)
- 04-06-2010, 01:55 PM #2
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
anyone can help me please ?
- 04-06-2010, 02:18 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Okay, do you know what happen on the following line?
Java Code:g.drawString(message, 180, 100);
- 04-06-2010, 04:21 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
that suppose to show the text inside > " Text " < could you make this clear for me ? i wanna know how to show those numbers on viewer for god sake !
- 04-06-2010, 06:11 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, what happen when you run your code? It print text, JAVA GUI on the applet viewer. From where it print, or how the VM found that text to print into the applet.
Where message is a string variable that holds the text JAVA GUI. So what you've to do, replace that message with your number x. But how? Because the drawString() expecting string literal, not an int. You have to do a conversion there from int to string.Java Code:g.drawString(message, 180, 100);
Is that clear to you, in vary basic level.
- 04-07-2010, 05:03 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
so that means i have to replace x with Java GUI , right ? but how can i convert string to int ?
- 04-07-2010, 05:18 AM #7
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
- 04-07-2010, 12:00 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
ive done some changes in code as below :
applet viewer shows somethin really small , i cant really say whats that !! could you guyz try the code n let me know the problem ?PHP Code:import java.awt.*; import java.applet.*; import static java.lang.Math.*; public class K extends Applet { public void init() { } public void paint(Graphics g) { int num,z,a; for (z=1;z<=10;z++){ int x=(int)(Math.random()*10000%10); for (a=1;a<=x;a++){ } g.drawString(Integer.toString(x),50,50); } } }
- 04-07-2010, 01:12 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why did you change init() method.
Java Code:public void init() { font = new Font("Arial", Font.BOLD, 45); }
- 04-07-2010, 02:19 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
ive done the changes , however still very small ! have you tried it yourself ? could you try and let me know ? tnxPHP Code:import java.awt.*; import java.applet.*; import static java.lang.Math.*; public class Damn extends Applet { private Font font; public void init() { } public void paint(Graphics g) { font=new Font("Arial",Font.BOLD,24); int num,z,a; for (z=1;z<=10;z++){ int x=(int)(Math.random()*10000%10); for (a=1;a<=x;a++){ } g.drawString(Integer.toString(x),50,50); } } }
- 04-08-2010, 03:38 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Put the following in init() method.
Java Code:font=new Font("Arial",Font.BOLD,24);
Similar Threads
-
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04-02-2010, 06:47 PM -
Mersenne twister random generator
By mental in forum New To JavaReplies: 6Last Post: 03-23-2010, 02:21 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Random Shape generator
By scheng12 in forum New To JavaReplies: 1Last Post: 03-09-2009, 02:06 AM -
Random Generator
By padutch2 in forum New To JavaReplies: 1Last Post: 12-03-2007, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks