CS 151 - Day 14

Cover page image

Cay S. Horstmann

Day 14 Clicker Question 1

The JUnit5 framework shows the following features: (Click all that apply.)

  1. Inversion of control
  2. Customization by forming subclasses of framework classes
  3. The template method pattern
  4. Annotations

Day 14 Clicker Question 2

A bag is like a set, except that each element can occur multiple times. As with a set, order doesn't matter. What must you do to add a Bag class to the Java collections framework? Click all that apply.

  1. Make Bag implement Collection
  2. Make Bag extend AbstractCollection
  3. Implement the size and iterator methods
  4. Add a count method to the Collection interface to count how many times an element is contained in the bag

Day 14 Clicker Question 3

Why does the graph editor framework use the Prototype pattern? Click all that apply.

  1. So that the framework can create nodes and edges
  2. To populate the tool bar
  3. So that each node and hedge can carry out “hit testing”
  4. To compute the connection points at node boundaries

Lab

lab

A Social Network Editor

  1. Copy the ch08/graphed2 directory into your lab14 directory, Then make an Eclipse project from existing sources with the files in ~/lab14.
  2. Use Refactor → Rename to rename SimpleGraph and SimpleGraphEditor to NetworkGraph and NetworkGraphEditor.
  3. Use the Ctrl+Space key to add a setSize method to CircleNode.
  4. Add a class PersonNode that extends CircleNode. Supply a default constructor that sets the superclass color to white and the size to 40. Add a field imageURL, and use Ctrl+Space to generate a getter and setter for it.
  5. In NetworkGraph, change the prototypes so you no longer have the circle nodes but instead have one PersonNode.
  6. Run the program. Add a person (which still looks like a circle). Select Edit → Properties. What happens? Why?
  7. Supply an image URL in the dialog. Here are a few (right-click to copy address):
    trump clinton sanders ryan cruz mcconnell default
    What happens? Why?

Drawing the Image

  1. Let's draw the image. Which method do you have to define?
  2. The easiest way is to make an image icon:
    public class PersonNode
    {
       public void setImageURL()
       {
          ...
          try
          {
             icon = new ImageIcon(new URL(imageURL));
          } 
          catch (IOException ex)
          {         
          }
       }
    
       private ImageIcon icon;
    }
    
  3. Call the paintIcon method of the ImageIcon. You'll need to find the bounds to put it in the right place. What did you do?
  4. Make it so that a default PersonNode isn't blank but uses a default icon such as this one: default
  5. You should now be able to make a network with some friends and not-so-friends.
  6. What did you need to know in order to make this program? What did you get for free from the framework?

Extra Time?

  1. It would be nice if the nodes would be circular since they extend CircularNode and the line attachments are at the circle boundary. Do this:
    Shape oldClip = g2.getClip();
    g2.setClip(new Ellipse2D.Double(...));
    Paint
     g2.setClip(oldClip);
  2. It would be nice if one could use any image, not just one that was pre-scaled to the correct size. Replace the ImageIcon icon field with a field Image image. In setImageURL, call
    image = new ImageIcon(new URL(imageURL)).getImage();
    In the draw method, draw the image like this:
    double width = image.getWidth(null);
    double height = image.getHeight(null);
    Rectangle2D bounds = getBounds();
    double scale = Math.min(bounds.getWidth() / width, bounds.getHeight() / height);
    width = scale * width;
    height = scale * height;
    g2.drawImage(image, (int) (bounds.getX() + bounds.getWidth() - width), 
                (int) (bounds.getY() + bounds.getHeight() - height), 
                (int) width, (int) height, null);
    
    What does this code do?
  3. Try it out with some larger image such as this one: trump-large.jpg
  4. Now try to save your graph. What happens? Why?
  5. How do you fix it?