Overview

The project consists of several classes working together -- most of them are provided for you -- only one class (the one named FlagMaker) will be written by you.  The final product will draw flags of different countries and at different sizes as selected by the input from the console.This project is considered "CLOSED". Please visit the course web page for information regarding the open/closed policy for projects of this course

Objectives

This project will give you some practice with loops and methods, and also introduces the notion of having multiple classes working together in a single project.

Grading

Clarifications

Any clarifications or corrections associated with this project will be available at Clarifications

Code Distribution

The project's code distribution is available by checking out the project named Flags. The code distribution provides you with the following:

Specifications

Color Class

One of the classes  you will use for this project is provided in one of the available Java graphics packages.  It is the "Color" class, and it is part of a package called "java.awt".  That means that any class that wants to use Color objects must begin with the following statement at the top of the file:

 import java.awt.Color;

If you wanted to create your own Color object you could use a statement like the one below:

Color myColor = Color.BLACK;  // myColor now refers to the color "black"

There are other ways to generate just about any color you could imagine, but the simple syntax shown above works for the following built-in colors that are needed for the flags:  BLACK, BLUE, GREEN, ORANGE, RED, WHITE, YELLOW.  In real life some flags are made from different variations of these colors for example one flag might have a dark green and another might have a light green - for ease on this project we will use the built in green for both.

MyGrid Class

The MyGrid class has been written for you.  It is part of a package called "GridTools".  That means that any class that wants to use MyGrid objects must begin with the following import statement at the top of the file:

import GridTools.MyGrid;

A MyGrid object is a window that you can draw on.  In the center of the window there is a rectangular grid (twice as wide as it is high).  You can color in each region of the grid with any color you want.  Below is an example of an "empty" MyGrid object of size 8 (that means 8 high and 16 wide).

Flag1

Creating a DrawingGrid Object

A MyGrid object is created in the example driver provided for you in your CVS repository; if you need to create a new MyGrid object in another driver, you could use a statement like this one:

MyGrid aGrid = new MyGrid(8);  //aGrid will refer to a 8 by 16 drawing grid

This statement will create a MyGrid object named aGrid where each of the 128 squares are already colored white.

Coloring the Squares on the Grid

Once you have a MyGrid object, you can color in any of the squares you want using any color you want.  To do this, you use the method of the MyGrid class called setColor.  The setColor method of the MyGrid class takes three parameters.  The prototype for the method appears below. 

public void setColor(int row, int col, Color color)

The parameters "row" and "col" specify the location of the square on the grid that you would like to color in; the parameter "color" refers to the color you want to use. Note that the first row is row number 0 (not row number 1), and the first column is column number 0.  For example, below is a picture of what you would see if you executed the code fragment that follows.

MyGrid aGrid = new MyGrid(6);  // creates the 6 by 12 MyGrid object

aGrid.setColor(3, 3, Color.RED);      // colors in the square at row 3, col 3, using red
aGrid.setColor(0, 2, Color.BLUE);     // colors in the square at row 0, col 2, using blue

Flag2

If a particular square on the grid has already been colored, there is no harm in re-coloring that same square -- the previous color will simply be replaced with the new color.  This will be a very useful fact when you look for similarities between the flags so you can reuse code you have already written.

Determining the size of the MyGrid object

There is are two other methods of the MyGrid class that you will need.  Suppose you have a variable called "aGrid" that refers to an existing MyGrid, and you want to know how big it is.  You can use the methods "getHt" and "getWd" to find out.  Warning - do not use "getHeight" and "getWidth".  The prototypes for these method appears below:

public int getHt()    // the return value is the height of the grid

public int getWd()    // the return value is the width of the grid

For example, if the variable "aGrid" refers to an existing 7 by 14 MyGrid object, then the following expressions would be equal to the integer 7 and 14 respectively:

aGrid.getHt() //returns the value 7

aGrid.getWd() //returns the value 14

Be careful - DO NOT USE getWidth() and getHeight() - these are defined but will not give you the numbers you are looking for.

FlagMaker Class

OK, this is where YOU come in!  We have provided a skeleton for this class that you must complete.  There is basically just one method that you must fill in, although it is okay for you to create other methods in this class if you find it useful to do so.  You may find that writing additional methods can help to eliminate duplicative code.  Since we will not be creating any FlagMaker objects, any methods you choose to write in this class should be declared using the keyword "static".  Since these other methods should only be called from another method in the one class you are writing, it should also have the keyword private.

The method that you must write has the following prototype:

public static void drawFlag(MyGrid grid, int countryCode)

Remember that the two parameters (grid and countryCode) are provided to your method by whoever called it.  Those parameters contain the information that your method needs in order to do its job.

The parameter "grid" will refer to an existing MyGrid object that has already been created by someone else.  Your method should not create a MyGrid object anywhere within the methods of this class, it is already there!  Your method will draw on the grid that is passed in via this parameter.  You may assume that the grid starts off empty (all grey squares).

The parameter "countryCode" will be an integer.  The number should correspond to one of the country flags listed in the menu of the example driver provided for you in your CVS repository (also corresponding to the numbers of The Flag Information Pages).  If it is not one of these values, it should display "the error flag" - more description of this below.  If the height specified by the user is invalid (as described for that flag) for the flag corresponding to that country code, the error flag should be displayed.  If it is a valid countryCode and a valid height for that country, the drawFlag method should display the flag indicated by that countryCode in the grid that was passed as a parameter to the method.

The style for this flag must conform exactly to the examples and descriptions below using the basic colors of the Color class (even though some are not  exactly the accurate shade for the flag chosen).  Note that the size of the grid can be determined by calling the "getHt" and "getWd" methods of the MyGrid class.  The size of the grid dictates how big a flag you are supposed to draw (the flag must fill up the entire grid, as in the examples shown below.)  The size typed by the user when it is requested in the console corresponds to the height of the flag - the width is always twice the height.

Examples and descriptions of flags

For examples and further descriptions of the flags click here. These are the exact style that you must use for drawing your flags. The hyperlinked file shows two examples of different sizes for each flag.   Your flag must conform exactly to the styles below.  The flag must take up the entire MyGrid, no matter what size the grid is. Your drawFlag method must be capable of drawing flags of any size that is appropriate for that country's flag (as defined here).  If the grid size given is not appropriate for the flag selected, an error flag should be drawn instead.  The smallest flag size allowed will be 4 high by 8 wide; if a size smaller than 4 is requested, the example driver creates a MyGrid object of size 4x8 in which you must display the error flag.  The maximum size for any flag is one with a height of 30; if a size larger than 30 is requested, the example driver creates a MyGrid object of size 4x8 in which you must display the error flag.  These limits are taken care of in the ExampleDriver class and you do not have to check it again in your code (read the exampleDriver for more explanation) - the example driver simply passes an invalid country code so you know that the error flag should be displayed.

Please note that the ExampleDriver class is not really part of the project assignment!  It is only provided to help you test out your code.  In fact, when we test your FlagMaker class ourselves, we will be using a completely different driver class.  Feel free to write your own driver class to test your FlagMaker class a different way, if you want to.

There will be one automated test for each type of flag.  The tests will go in the same order they appear on the menu of the ExampleDriver.  We believe the ones listed first are the easiest so we suggest you draw your flags in the order specified there.

Requirements

Additional Requirements for Students in Honors Section

There is a special section of this course that is for honor students. If you are registered in cmsc131H then you are expected to complete this part. Otherwise, feel free to ignore it (unless you want extra practice, what we always encourage :)). If you are registered in a non-honor section, you will not receive additional points if you complete this part.

Option 10 in the ExampleDriver class represents the honors design. Your task is to add a design of yours (similar to a flag or a flag). We should be able to resize your design (same resizing we can do with the flags we have presented). Keep in mind that no tests have been provided in the submit server for this part. We will test this separately and deduce -10 from your total score if you have not implemented this requirement. Your design should not be trivial (for example, just use one color for the design). If you have doubts regarding the validity of your design, then discuss this matter with your instructor.

Here are some examples of what students have done in the past HonorsFlagsExamples.pdf.