CS 201 - Computer Science I - Fall 2003
Examples


Loyola College > Department of Computer Science > CS 201 > Examples > Drawing with Loops

source code
Inferior browsers on some platforms may have difficulty displaying some applets.

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

/**
 * Examples of drawing using loops.
 *
 * @author Jim Glenn
 * @version 0.1 3/31/2003
 */

public class LoopDrawing extends Applet
{
    /**
     * Draws one of three picutres chosen at random.
     *
     * @param g the graphics context to draw on
     */

    public void paint(Graphics g)
    {
	// randomly choose which picture to draw
	int which = (int)(Math.random() * 3);

	switch (which)
	    {
	    case 0:
		drawBubbles(g, 10);
		break;

	    case 1:
		drawGrid(g, 10);
		break;

	    case 2:
		drawCheckerboard(g, 8);
		break;
	    }
    }

    /**
     * Draws a border of circles.
     *
     * @param g the graphics context to draw on
     * @param bubbleSize the size, in pixels, of the circles to draw
     */

    private void drawBubbles(Graphics g, int bubbleSize)
    {
	// get the width and height of the applet
	// (note: this.getWidth() and this.getHeight() require Java 1.2)

	int w;
	w = (int)(this.getSize().getWidth());

	int h;
	h = (int)(this.getSize().getHeight());

	// compute number of bubbles that fit horizontally and vertically

	int bubblesWide;
	bubblesWide = (w - 1) / bubbleSize;

	int bubblesHigh;
	bubblesHigh = (h  - 1) / bubbleSize;

	// compute top of bottom border and left of right border

	int bottomBorder;
	bottomBorder = (bubblesHigh - 1) * bubbleSize;

	int rightBorder;
	rightBorder = (bubblesWide - 1) * bubbleSize;

	// draw the bubbles at the top and bottom

	int bubble;
	for (bubble = 0; bubble < bubblesWide; bubble++)
	    {
		g.drawOval(bubble * bubbleSize, 0, bubbleSize, bubbleSize);
		g.drawOval(bubble * bubbleSize, bottomBorder, bubbleSize, bubbleSize);
	    }

	// draw the bubbles at the left and right

	for (bubble = 0; bubble < bubblesHigh; bubble++)
	    {
		g.drawOval(0, bubble * bubbleSize, bubbleSize, bubbleSize);
		g.drawOval(rightBorder, bubble * bubbleSize, bubbleSize, bubbleSize);
	    }
    }

    /**
     * Draws a grid with square cells.
     *
     * @param g the graphics context to draw in
     * @param cols the number of columns in the grid
     */

    private void drawGrid(Graphics g, int cols)
    {
  	// get the width and height of the applet

	int w;
	w = (int)(this.getSize().getWidth());

	int h;
	h = (int)(this.getSize().getHeight());

	// figure out the size, in pixels, of each grid square

	int sizeInPixels;
	sizeInPixels = (w - 1) / cols;

	// figure out the number of rows in the grid

	int rows;
	rows = (h - 1) / sizeInPixels;

	// draw the horizontal grid lines

	int r;
	for (r = 0; r <= rows; r++)
	    g.drawLine(0, r * sizeInPixels, cols * sizeInPixels, r * sizeInPixels);

	// draw the vertical grid lines

	int c;
	for (c = 0; c <= cols; c++)
	    g.drawLine(c * sizeInPixels, 0, c * sizeInPixels, rows * sizeInPixels);
    }

    /**
     * Draws a square checkerboard.
     *
     * @param g the graphics context to draw in
     * @param squares the width and height, in squares, of the checkerboard
     */

    private void drawCheckerboard(Graphics g, int squares)
    {
  	// get the width and height of the applet

	int w;
	w = (int)(this.getSize().getWidth());

	int h;
	h = (int)(this.getSize().getHeight());

	// figure out the size, in pixels, of each square on the board

	int squareSize;

	if (h < w)
	    squareSize = h / squares;
	else
	    squareSize = w / squares;

	// draw the checkerboard

	int r;
	for (r = 0; r < squares; r++)
	    {
		int c;
		for (c = 0; c < squares; c++)
		    {
			// draw red or black based on where we're drawing

			if (r % 2 == c % 2)
			    g.setColor(Color.BLACK);
			else
			    g.setColor(Color.RED);
		    
			g.fillRect(c * squareSize, r * squareSize, squareSize, squareSize);
		    }
	    }
    }
}