Monday 19 August 2013

Java program to draw 20 circles with the help of random()


/**
 * @(#)Random_Circles.java
 *
 *
 * @author - Anant Mahale
 * @version 1.00 2013/8/19
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Random_Circles
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("20 Circles");
        m.setSize(500,500);
        m.setVisible(true);
        m.setLocation(150,150);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel
    {
        Color randomColor;
        mypanel()
        {
        }
        public void paint(Graphics g)
        {
            super.paint(g);           
              for (int idx = 1; idx <= 20; ++idx)
              {
                  Random randomGenerator = new Random();
                  int R = (int) (Math.random( )*256);
                   int G = (int)(Math.random( )*256);
                int B= (int)(Math.random( )*256);
                randomColor = new Color(R, G, B);
                g.setColor(randomColor);
                int randomInt = randomGenerator.nextInt(100);
                g.drawOval(R,G,randomInt,randomInt);
   
            }
        }
    }
}

No comments: