Results 1 to 11 of 11
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question 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 :

    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);
    			
    		
    	}
    }
    }
    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.
    would you help me on this ? tnx :)

  2. #2
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    anyone can help me please ?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, do you know what happen on the following line?

    Java Code:
    g.drawString(message, 180, 100);

  4. #4
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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 !

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    Java Code:
    g.drawString(message, 180, 100);
    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.

    Is that clear to you, in vary basic level.

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    so that means i have to replace x with Java GUI , right ? but how can i convert string to int ?

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by pinkdreammsss View Post
    so that means i have to replace x with Java GUI , right ? but how can i convert string to int ?
    Integer.parseInt(String...) :rolleyes:

  8. #8
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    ive done some changes in code as below :

    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);
    			
    		
    	}
    }
    }
    applet viewer shows somethin really small , i cant really say whats that !! could you guyz try the code n let me know the problem ?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Why did you change init() method.

    Java Code:
        public void init() {
            font = new Font("Arial", Font.BOLD, 45);
        }

  10. #10
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    PHP 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);
    			
    		
    	}
    }
    }
    ive done the changes , however still very small ! have you tried it yourself ? could you try and let me know ? tnx

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  2. Mersenne twister random generator
    By mental in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:21 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 02:06 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 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
  •