import java.awt.*;
import java.applet.*;
import java.util.*;

// this is a Java applet that draws random rectangles
// Assignment 1, CSci 1211
// Created by Elena Machkasova, 1/16/04
// Modified by Elena Machkasova Feb. 2004

public class RandomRectanglesColor extends Applet {

    public void paint(Graphics g) {
	// set the background to white
	g.setColor(Color.yellow);
	g.fillRect(0, 0, 400, 400);

	// create a random number generator
	Random r = new Random();
	
	// generate a random color:
	int c = r.nextInt(5);
	switch (c) {
	   case 0: g.setColor(Color.black); break;
	   case 1: g.setColor(Color.gray); break;
	   case 2: g.setColor(Color.white); break;
	   case 3: g.setColor(Color.red); break;
	   default: g.setColor(Color.blue); break;
	}

	int x = r.nextInt(200);
	int y = r.nextInt(200);
	int h = r.nextInt(200);
	int w = r.nextInt(200);
	g.fillRect(x, y, h, w);

	c = r.nextInt(5);
	switch (c) {
	   case 0: g.setColor(Color.black); break;
	   case 1: g.setColor(Color.gray); break;
	   case 2: g.setColor(Color.white); break;
	   case 3: g.setColor(Color.red); break;
	   default: g.setColor(Color.blue); break;
	}
	x = r.nextInt(200);
	y = r.nextInt(200);
	h = r.nextInt(200);
	w = r.nextInt(200);
	g.fillRect(x, y, h, w);

	c = r.nextInt(5);
	switch (c) {
	   case 0: g.setColor(Color.black); break;
	   case 1: g.setColor(Color.gray); break;
	   case 2: g.setColor(Color.white); break;
	   case 3: g.setColor(Color.red); break;
	   default: g.setColor(Color.blue); break;
	}
	x = r.nextInt(200);
	y = r.nextInt(200);
	h = r.nextInt(200);
	w = r.nextInt(200);
	g.fillRect(x, y, h, w);
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.