8
RandomSquares
- // <applet code="RandomSquares" width="300" height="200"></applet>
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.Color;
- public class RandomSquares extends Applet {
- protected int numberOfSquares = 8;
- public void paint(Graphics g) {
- int width = getSize().width, height = getSize().height;
- int maxsize = Math.min(width, height);
- // System.out.println(g.getClip());
- g.setClip(null);
- g.setColor(Color.white);
- g.fillRect(0, 0, width, height);
- for (int n = 0; n < numberOfSquares; n++) {
- Color c = new Color(rint(256), rint(256), rint(256));
- int l = rint(maxsize / 2);
- int x = rint(width - l);
- int y = rint(height - l);
- g.setColor(c);
- g.fillRect(x, y, l, l);
- }
- }
- public void update(Graphics g) {
- paint(g);
- }
- private int rint(int max) {
- return (int) Math.floor(Math.random() * max);
- }
- }
$ javac RandomSquares.java
$ appletviewer RandomSquares.java
Comments