Scott Grissom
School of Computing & Info Systems
Grand Valley State University
picture

Lab: Ch 5 Bulls Eyes & Checker Boards

Preparation

Do the following before arriving at lab:

  • Read chapter 4 and understand for loops (4.12.6) and nested loops (lecture notes)
  • Bring your textbook to lab

Objectives

After completing this lab, you should be able to:

  • draw objects within an applet
  • use loops and nested loops to solve a problem

Exercises

Getting Started
  1. Create a new APPLET class in a BlueJ project of your choice. Call it "CheckerBoard".
  2. Compile the class.
  3. Right click and select "run applet".
  4. Click "OK".
  5. The "paint" method is invoked automatically when you run the applet. Find and study the method for a few minutes.
Graphic Basics

Note that the "paint" method has a parameter of type Graphics. The Java Graphics class has a number of methods to draw things. You only need to understand two of the methods for this assignment.

  • g.setColor(Color.red) - sets the current pen color to red. Other choices include: Color.black, Color.white, Color.blue, Color.green, Color.cyan.
  • g.fillRect(left, top, width, height) - draws a rectangle with the upper left corner at coordinates (left, top) with a prescribed width and height in integers. Recall that the origin (0, 0) is in the upper left corner of the screen.
  • g.fillOval(left, top, width, height) - draws an oval with the upper left corner at coordinates (left, top) with a prescribed width and height in integers. Recall that the origin (0, 0) is in the upper left corner of the screen.

For practice, draw a few different colored rectangles of differnent sizes and different locations..

  1. Remove the existing statements in the "paint" method.
  2. For practice, add additional statements as desired to draw rectangles and ovals
Bullseye

Add code to the paint method to draw a bullseye. Concentric circles using two colors. Show your solution to the instructor.

Checkerboard - Start with one row
  • Create a new Applet class to draw a Checkerboard
  • Add code to the paint method to draw one row of alternating red and black squares (50 x 50). There are a total of eight squares.
  • Use a for loop and limit yourself to no more than two "fillRect" commands.
Now draw a full Checker Board

Now place the above code in a loop that iterates eight times to draw eight rows. You will need to add clever logic to alternate between black and red appropriately.

Getting More Sophisticated

Improve your solution so that you call "fillRect" only once.

Already Done?

Now place three round green checkers and five round blue checkers one the board in any positions you choose. You will want to use the g.fillOval( ) method that uses the same parameters as the fillRect( ) method.

Sample Results

Screen shot

Grading Criteria

This lab is worth a possible 10 points. Turn in a printout of the source code for Checkerboard.

  • methods (8 pts)
  • coding style (2 pts)