Applets :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    Applets :(

    hi all you guys out there!

    Im trying to solve this que, and its kind of bothering me..........I hate applets:(

    - Write a program to draw n number of randomly generated circles on the applet where n is accepted as a parameter.

    i did this but i dnt know how i can "randomly" generate them

    Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    
    public class circles extends Applet{
    	public void init(){
    //		 setting the applet size
    		setSize(85,200);
    		
    		// setting the background color on applet panel
    		this.setBackground(Color.WHITE);
    				
    		// setting the layout on applet panel
    		setLayout(new FlowLayout());
    	} 
    	
    	public void paint(Graphics g){
    		g.drawOval(100,100,60,60);
    		//g.drawOval(100,100,60,60);
    	} 
    }
    thanks and i hope that you guys can help me out!!

    outofmymind
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by outofmymind
    hi all you guys out there!

    Im trying to solve this que, and its kind of bothering me..........I hate applets:(

    - Write a program to draw n number of randomly generated circles on the applet where n is accepted as a parameter.

    i did this but i dnt know how i can "randomly" generate them

    Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
     
    public class circles extends Applet{
    	public void init(){
    //		 setting the applet size
    		setSize(85,200);
     
    		// setting the background color on applet panel
    		this.setBackground(Color.WHITE);
     
    		// setting the layout on applet panel
    		setLayout(new FlowLayout());
    	} 
     
    	public void paint(Graphics g){
    		g.drawOval(100,100,60,60);
    		//g.drawOval(100,100,60,60);
    	} 
    }
    thanks and i hope that you guys can help me out!!

    outofmymind


    Code:
     
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    public class circles extends JApplet{
     private int number;
     public void init(){
    //   setting the applet size
      setSize(500,500);
      number = Integer.parseInt(JOptionPane.showInputDialog(this, "Enter number of circles"));
      // setting the background color on applet panel
      this.setBackground(Color.green);
      // setting the layout on applet panel
      //setLayout(new FlowLayout());
     }
     public void paint(Graphics g){
      g.setColor(Color.blue);
      for(int i = 0; i < number; i++) {
       int diameter = 30 + (int)(Math.random()*50);
       int xpos = diameter + (int)(Math.random()*(getWidth() - diameter));
       int ypos = diameter + (int)(Math.random()*(getHeight() - diameter));
       g.drawOval(xpos,ypos,diameter,diameter);
      }
      System.out.println(getWidth());
      System.out.println(getHeight()); 
     }
    }
    Like this?

    Comment

    • outofmymind
      New Member
      • Oct 2006
      • 45

      #3
      Thanks r035198x....... .....your the best :D

      outofmymind

      Comment

      Working...