Posted by : Unknown
Thursday, 18 June 2015
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomCircles extends JPanel implements ActionListener
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)
public void drawFrame(Graphics g, int frameNumber, int width, int height)
{
int centerX,centerY;
int colorChoice;
int count;
for (count = 0; count < 500; count++)
int centerX,centerY;
int colorChoice;
int count;
for (count = 0; count < 500; count++)
{
colorChoice = (int)(3*Math.random());
switch (colorChoice)
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)
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)
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)
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}
Output:
Related Posts :
- Back to Home »
- Simple Application Program »
- Random Circles