Posted by : Unknown Thursday, 18 June 2015

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomCircles extends JPanel implements ActionListener 
{
    public void drawFrame(Graphics g, int frameNumber, int width, int height) 
   {       
        int centerX,centerY;    
        int colorChoice;
        int count;    
        for (count = 0; count < 500; count++) 
       {         
            colorChoice = (int)(3*Math.random());
            switch (colorChoice) 
           {
            case 0:
                g.setColor(Color.RED);
                break;
            case 1:
                g.setColor(Color.GREEN);
                break;
            case 2:
                g.setColor(Color.BLUE);
                break;
            }           
            centerX = (int)(width*Math.random());
            centerY = (int)(height*Math.random());           
            g.fillOval( centerX - 50, centerY - 50, 100, 100 );
            g.setColor(Color.BLACK);
            g.drawOval( centerX - 50, centerY - 50, 100, 100 );           
        }
    }   
    public static void main(String[] args) 
    {       
        JFrame window = new JFrame("Random Disks");
        RandomCircles drawingArea = new RandomCircles();
        drawingArea.setBackground(Color.WHITE);
        window.setContentPane(drawingArea);
        drawingArea.setPreferredSize(new Dimension(500,500));
        window.pack();
        window.setLocation(100,50);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        Timer frameTimer = new Timer(3000,drawingArea);
        window.setVisible(true);
        frameTimer.start();
    }
    private int frameNum;   
    public void actionPerformed(ActionEvent evt) 
    {
        frameNum++;
        repaint();
    }
    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        drawFrame(g, frameNum, getWidth(), getHeight());
    }
}

Output:

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -