cs106a – Assignment #6 – Task #4

The complete specification of assignment #6 can be found as part of the stream at iTunes.

Create the background grid for the NameSurferGraph class

The next step in the process is to begin the implementation of the NameSurferGraph class, which is responsible for displaying the graph in the window by building the underlying model.
There are a couple of important items in the NameSurferGraph starter file that are worth noting:

  1. This class extends GCanvas, which means that every NameSurferGraph object is not only a GCanvas but also an instance of all the superclasses of the GCanvas class. GCanvas is a subclass of Component in the standard java.awt package and therefore is part of the hierarchy of classes that can be added to the display area of a program. Moreover, it means we can call any of the GCanvas methods, such as adding or removing GObjects, from within NameSurferGraph.
  2. The starter file includes a tiny bit of code that monitors the size of the window and calls update whenever the size changes. This code requires only a couple of lines to implement, but would be hard to explain well enough for you to implement on your own. Writing a page of description so that you could add a couple of lines seemed like overkill, particularly given that the strategy is easiest to learn by example.

To start the process of adding the graphing code, go back to the NameSurfer class and change its definition so that it extends Program rather than the temporary expedient of extending ConsoleProgram (if you were using that for debugging). At the same time, you should remove the various println calls that allowed you to trace the operation of the interactors in the earlier milestones.
Now, you’ll need to declare a NameSurferGraph private instance variable in your main NameSurfer class:

private NameSurferGraph graph;

You should then change the constructor of the NameSurfer class so that it creates a new
NameSurferGraph object and adds that object to the display, as follows:

graph = new NameSurferGraph();
add(graph);

If you run the program with only these changes, it won’t actually display anything on the screen. To create the graph, you need to implement the update method, which will almost certainly involve defining private helper methods as well. As a first step, write the code to create the background grid for the graph, which consists of the vertical line separating each decade, the horizontal lines that provide space for the top and bottom borders (which are there to ensure that the text labels stay within the window bounds), and the labels for the decades. As with all the graphical applications you’ve written, the lines and labels are represented using GLine and GLabel objects, which you add to the graph in the appropriate positions.

Change the parent class like described above:

public class NameSurfer extends Program implements NameSurferConstants {

Add the instance variable for the graph an initialize it:

	private NameSurferGraph graph;
	
	public void init() {
		dataBase = new NameSurferDataBase(NAMES_DATA_FILE);
		
		graph = new NameSurferGraph();
		add(graph);
                ...

Adjust the update method to draw the grid:

	public void update() {
		removeAll();
		drawGrid();
	}

	private void drawGrid() {
		double height = getHeight();
		double width = getWidth();
		double x = 0;
		double columnWidth = width * 1.0 / NDECADES;
		int year = START_DECADE;
		double yText = height - GRAPH_MARGIN_SIZE / 3;
		for (int i = 0; i < NDECADES; i++) {
			add(new GLine(x, 0, x, height));
			add(new GLabel(" " + year, x, yText));
			x += columnWidth;
			year += DECADE;
		}
		double yTopLine = GRAPH_MARGIN_SIZE;
		double yBottomLine = height - GRAPH_MARGIN_SIZE;
		add(new GLine(0, yTopLine, width, yTopLine));
		add(new GLine(0, yBottomLine, width, yBottomLine));
	}

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *