On this page:
Problem 1: Mobiles
3.1 Using the javalib library
3.1.1 Basic Images
3.1.2 Combining Images
3.1.3 Pinholes
8.5

Lab 3: Problem Solving and Accumulators

Goals: The goals of this lab are to write methods in union data, and specifically to practice writing accumulator-style methods.

Submission: There is no submission for this lab, but you will complete this problem for Assignment 3 with your new homework/lab partner.

For this lab, there are no starter files. For each problem, start a new project and build the files from scratch.

Problem 1: Mobiles

Alexander Calder was an American artist well known for his hanging sculptures called mobiles. Look up some of his work; it is quite pretty.

We would like to help other artists who may want to design mobiles, so they can check ahead of the time whether their mobile will fit in the desired space (height, width, weight), and whether it will be properly balanced.

The following drawing shows two mobiles, one simple and the other more complex:

image

image

Each horizontal bar is a strut (a very thin, very lightweight rod), and each vertical bar is a string. We will restrict the mobile to two items hanging from each strut, and record for any item hanging at the end of the line its weight and its color.

We have decided on the following data representation for mobiles (the length refers to the length of the vertical string, the leftside and rightside refer to the two parts of the strut):

             +---------+
             | IMobile |<---------------+
             +---------+                |
             +---------+                |
                 |                      |
                / \                     |
                ---                     |
                 |                      |
       ---------------------            |
       |                   |            |
+--------------+   +---------------+    |
| Simple       |   | Complex       |    |
+--------------+   +---------------+    |
| int length   |   | int length    |    |
| int weight   |   | int leftside  |    |
| Color color  |   | int rightside |    |
+--------------+   | IMobile left  |----+
                   | IMobile right |----+
                   +---------------+

The remaining two problems are harder. Do them now, if you have the time, or finish them for homework. Think about them, but move on to the next problem.

3.1 Using the javalib library

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files.

To use the library, download the javalib.jar file above and add it to your project the same way you have added the tester library

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import javalib.worldcanvas.*; // so we can view our images import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)

The first one is familiar; the rest are needed to import and use the definitions from javalib.

Much like the image library for ISL, our image library for Java also comes with extensive documentation.

3.1.1 Basic Images

Note: The purpose of this section of the lab is to get you exposed to our image library. Be sure to actually run the examples given so you get a feel for it before you have to use it on homework!

Here is a snippet of code that will let us draw and look at a circle:

class ExamplesShapes {
 
WorldImage circle = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
 
boolean testDrawMyShapes(Tester t) {
return c.drawScene(s.placeImageXY(circle, 250, 250))
&& c.show();
}
}

And here is a snippet of code that will let us draw and look at two circles:

class ExamplesShapes {
 
WorldImage circle = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
 
boolean testDrawMyShapes(Tester t) {
return c.drawScene(s.placeImageXY(circle, 250, 250))
&& c.drawScene(s.placeImageXY(circle, 100, 300))
&& c.show();
}
}

However, it would be nice to be able to look at separate images separately, and to be able to identify which image is which. It would also be nice to not have to construct new canvases and scenes over and over again by hand. Here is a snippet of code that you can use to look at multiple images and tell them apart with much less work needing to be done for each image.

class ExamplesShapes {
 
// shows image at the center of an equally-sized canvas, // and the text at the top of the frame is given by description boolean showImage(WorldImage image, String description) {
int width = (int) Math.ceil(image.getWidth());
int height = (int) Math.ceil(image.getHeight());
WorldCanvas canvas = new WorldCanvas(width, height, description);
WorldScene scene = new WorldScene(width, height);
return canvas.drawScene(scene.placeImageXY(image, width / 2, height / 2))
&& canvas.show();
}
 
WorldImage circleBlue = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldImage circleRed = new CircleImage(100, OutlineMode.SOLID, Color.RED);
 
boolean testDrawMyShapes(Tester t) {
return showImage(circleBlue, "Blue circle")
&& showImage(circleRed, "Red circle");
}
}

Being able to look at your images will be helpful for debugging in future assignments as well as this lab. Note that this is not a replacement for proper tests, and that methods that output images can be tested in the tester library just like any other method.

Use the documentation to help you with these exercises:
  • Draw at least two other basic shapes, such as a square and a triangle

  • Give your lab partner a friendly message by drawing some text

  • Draw the empty image

3.1.2 Combining Images
3.1.3 Pinholes