Computer Science 120
Introduction to Programming

Spring 2012, Siena College

Programming Project 2: Getting Warmer
Due: 11:59 PM, Friday, February 24, 2012

Your task for this programming project is to write a Java program that plays a "getting warmer" game. The basic idea is simple: we "hide" a point on the canvas and drag around a circle until it passes over the hidden point. To aid in this search, we give some feedback. When the circle is dragged closer to the hidden point, it becomes more red. When it is dragged further away, it becomes more blue.

Ground Rules

You must work individually on this project. You may consult only Dr. Small and Dr. Teresco for help. You may not discuss the programs with anyone else including your classmates and the computer science tutors until after everyone has submitted their programs. You may consult your own graded or ungraded lab submissions, your own class notes, and any class examples or notes available from the course web page. Think of this as an open-book lab exam. Any collaboration or consultation of unauthorized sources will be considered a breach of academic integrity and will be dealt with according to the course policies outlined on the syllabus.

While no written design will be graded for these programs, coming up with a written design is likely to be a beneficial first step.

Note that a significant portion of the credit for the project is for style, program design, documentation, and efficiency. While everyone should strive to earn all of the correctness points, you may find that a well-designed, thoroughly-commented program that makes appropriate use of variables and constants but does not quite implement all functionality will earn a higher grade than a perfectly-functioning program that uses poor variable names, does not use named constants, is uncommented, or is poorly formatted.

Have fun and good luck!


Your "getting warmer" game will work as follows.

When the game starts, a random location on the canvas is chosen for the "target", and the "searcher" circle is created, also at a random location. The searcher should be created completely within the bounds of the canvas. Two text labels are added: one with a message that should start out with brief instructions, and one that will act as a scoreboard to indicate the number of targets that have been found.

To play the game, the user can drag the searcher around, trying to drop it onto the target. While being dragged, the searcher's color is changed to indicate how close to or far from the target it is. When the searcher is right over the target, it should be red ("you're red hot!"). When it is very far from the target, it should be blue ("you're ice cold!"). Otherwise, it should be a shade of purple: more red and less blue as it gets closer to the target ("you're getting warmer!"), less red and more blue as it gets further away ("you're getting colder!").

Computing Distances

You need not compute precise (i.e. the standard Euclidean) distances to have a playable game. One possible way to compute a meaningful distance metric is to take the average of the distances in the x and y dimensions. You should, however, take care to use the center location (rather than the upper-left corner or the mouse drag location) of the searcher to compute the distance.

In computing the distances, you may need to take the absolute value of a difference. If you want to compute the distance between two numbers a and b and store it into a number d, you might be tempted to write:

  if (a < b) {
    d = a - b;
  else {
    d = b - a;

Fortunately, Java provides a method that will compute the absolute value for us. So the same result can be accomplished with:

  d = Math.abs(a - b);

Once you have a distance, you will need to use it to obtain appropriate red, green, and blue intensity values to create a custom Color object. You will need to be able to take the value of the distance, which is a double value, and use it to compute the intensities of the color components, which must be specified as int values in the range 0..255. If you find that you have computed a value that Java is treating as a double, but which you need to treat as an int, you can use a cast to force Java to do the appropriate conversion. For example, if you have a value d which is a double and you wish to add it to 25 and store the result in an int called s, you could write:

   s = (int)(d + 25);

You will need to think about how to use your computed distance to determine the color (i.e., the RGB value) to use for the searcher. It might help to treat any distance greater than 255 as a distance of 255. That would mean that the searcher remains completely blue when at any position further than 255 from the target.

Determining "Wins"

When the searcher is being dragged and the mouse is released, check for a "win". The player wins if the target's location is contained in the searcher.

If the player wins (i) an appropriate congratulatory message should be displayed and the scoreboard should be updated to indicate the new number of targets found, (ii) a new random location should be chosen for the target, and (iii) the searcher should be colored black until the mouse is first pressed on it.

If the player does not yet win, a message should be displayed and the player is given another chance to drag the searcher to the target.


A working solution for the program will appear below. Click inside the applet to interact with it.

Development and Debugging Tips

One of the great advantages of learning to program in a graphical environment like that provided by ObjectDraw is the visual feedback that can help you figure out what is going wrong when your program does not work. But in this case, one of the most important parts of your program, the target location, is invisible. To aid in the development, testing, and debugging of your program, you may consider these techniques:

If you do any of these, be sure to remove them before you submit your program for grading.

Submitting Your Work

Before 11:59 PM, Friday, February 24, 2012, submit your Java program for grading. There are three things you need to do to complete the submission: (i) place a copy of your Java program into your csis120/hw folder under hw6, (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor.

Don't forget to check your programs for compliance with the Style Guide for CSIS 120 Programs


This assignment is worth 100 points, which are distributed as follows:

> FeatureValueScore
Style, Design, and Efficiency
Appropriate comments 6
Good variable names 4
Appropriate variable declarations 6
Good use of constants 8
Appropriate formatting 2
Does not generate new objects unnecessarily 4
Good overall design and methods 6
Initial message and scoreboard 4
Initial target position chosen randomly 8
Initial searcher position chosen randomly 8
Random positions based on actual canvas dimensions 5
Searcher can be dragged smoothly 6
Searcher color changes appropriately based on distance from target 12
Correct check for win 4
Message updates when searcher dropped not on target 4
Message updates on win 4
Scoreboard updates on win 4
New target position chosen after each "win" 5
Total 100