Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Documentation for this project is stated here: http://www.cs.umd.edu/class/fall2

ID: 3563904 • Letter: D

Question

Documentation for this project is stated here:

http://www.cs.umd.edu/class/fall2014/cmsc131-34/Projects/Flags/p2.html

EXAMPLE DRIVER

import GridTools.MyGrid;

import java.util.Scanner;
/**
* THIS CLASS IS PROVDED FOR YOU. USE IT TO TEST THE LETTERMAKER
* CLASS THAT YOU ARE WRITING.
*   
* This driver relies on the "drawFlag" method of the "FlagMaker"
* class. It prompts the user to enter information about what letter
* he/she would like to see. Then it creates a DrawingGrid2, and calls
* the drawLetter method of the FlagMaker class to actually draw
* the letter.
*/
public class ExampleDriver {
  
   public static void main(String[] args) {
      
       Scanner scanner = new Scanner(System.in);
      
       /* Get input from user about what flag to draw */
       System.out.println("Type the number corresponding to the name: ");
       System.out.println("1 Poland");
       System.out.println("2 Ukraine");
       System.out.println("3 Czech Republic");
       System.out.println("4 Benin");
       System.out.println("5 Rwanda");
       System.out.println("6 Bahamas");
       System.out.println("7 Afghanistan");
       System.out.println("8 Jersey");
       System.out.println("9 Scotland");
       System.out.print("Your choice here:");
       int choice = scanner.nextInt();
       System.out.print("Choose a size (4 or larger): ");
       int size = scanner.nextInt();
       if (size < 4 || size >30){
           size = 4;
           choice = 99;
       }
      
       /* Create drawing grid of the height requested */
       MyGrid grid = new MyGrid(size);

       /* Draw the letter on the grid */
       FlagMaker.drawFlag(grid, choice);
   }
}

-----------------------------------------------------------------------------------------------------------------------------------

Project (Where I'll actually be typing in my program)

import java.awt.Color;
import GridTools.MyGrid;

public class FlagMaker {

   /* *************************************************
   * Draws a single flag in the already created grid
   * that is passed as the first parameter
   * according to the countryCode passed as the
   * second parameter.      
   *
   * (Student: you will need to modify this method   - the code
   * provided here is for demonstration purposes only.          
   * *************************************************/
   public static void drawFlag(MyGrid grid, int flagNumber) {
       grid.setColor(3,3,Color.RED);
       drawLine(grid, 2, Color.YELLOW);
       grid.setColor(2,3,Color.BLUE);

   }
  
   /*Helper functions for the drawFlag method appear below this line*/
  
   /*
   * This method takes an already created grid, a row number that
   * will be assumed to be a valid row on that grid and a color. It
   * then draws on that grid, in the indicated row, a full horizontal
   * line in the color indicated.
   * (Student: you may or may not wish to keep this method -- it is just
   * provided as a demonstration of what you may want to do.)
   */
   public static void drawLine(MyGrid grid, int row,
           Color myColor){
       for (int col = 0; col < grid.getWd(); col++){
           grid.setColor(row,col,myColor);
       }      
   }
}

---------------------------------------------------------------------------------------------------------------------------------------

My Grid class

package GridTools;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Color;

/**
* WARNING: READING THIS CODE MAY CONFUSE YOU PRETTY BADLY!!!
*
* I've decided to include this source code in the project in case you
* are curious to see an example of more advanced Java programming.
*
* You should not modify this file in any way.
*
* You should not even READ this file unless you are very curious and
* not easily scared!
*
*/
public class MyGrid extends JPanel {
  
   protected static final long serialVersionUID = 0L;
   protected static final int MARGIN_SIZE = 5;
   protected int scaleH;
   protected int scaleW;
   protected Color[][] colors;
   private static final int SQUARE_SIZE = 15; // pixel size of each square
   private JFrame frame;
  
   protected MyGrid() {
   }
  
   public MyGrid(int scale) {
       this.scaleH = scale;
       this.scaleW = 2*scale;
       int overallSizeH = scaleH + 2 * MARGIN_SIZE;
       int overallSizeW = scaleW + 2 * MARGIN_SIZE;
       colors = new Color[overallSizeH][overallSizeW];
       for (int i = 0; i < overallSizeH; i++)
           for (int j = 0; j < overallSizeW; j++)
               colors[i][j] = Color.WHITE;
      
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
               createAndShowFrame();
           }
       });  
   }
   public int getScale(){
       return scaleH;
   }
   public int getHt() {
       return scaleH;
   }
   public int getWd(){
       return scaleW;
   }
  
   private void createAndShowFrame() {
       frame = new JFrame("Drawing Grid");
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       setSize((scaleW + 2 * MARGIN_SIZE) * SQUARE_SIZE,
               ((scaleH) + 2 * MARGIN_SIZE) * SQUARE_SIZE);
       frame.setContentPane(this);
       frame.pack();
       frame.setVisible(true);
       frame.toFront();
       frame.setAlwaysOnTop(true);
   }
  
   public void setColor(int row, int col, Color color) {
       colors[row + MARGIN_SIZE][col + MARGIN_SIZE] = color;
       this.repaint();
   }
  
   public void kill() {
       if (frame != null)
           frame.dispose();
   }
  
   public Dimension getPreferredSize() {
       return new Dimension((scaleW + 2 * MARGIN_SIZE) * SQUARE_SIZE + 1,
               ((scaleH) + 2 * MARGIN_SIZE) * SQUARE_SIZE + 1);
   }
  
   public void paint(Graphics g) {
       super.paint(g);
  
       int offset = MARGIN_SIZE * SQUARE_SIZE;
      
       for (int i = 0; i < scaleH + 2 * MARGIN_SIZE; i ++)
           for (int j = 0; j < scaleW + 2 * MARGIN_SIZE; j++) {
               g.setColor(colors[i][j]);
               g.fillRect(j * SQUARE_SIZE + 1, i * SQUARE_SIZE + 1,
                       SQUARE_SIZE, SQUARE_SIZE);
           }
       g.setColor(Color.BLACK);
       for (int i = 0; i < scaleW + 1; i++)
           g.drawLine(offset + i * SQUARE_SIZE, offset,
                   offset + i * SQUARE_SIZE, offset + scaleH * SQUARE_SIZE);
       for (int j = 0; j < scaleH + 1; j++)
           g.drawLine(offset, offset + j * SQUARE_SIZE,
                   offset + scaleW * SQUARE_SIZE, offset + j * SQUARE_SIZE);
   }
}

Explanation / Answer

import java.awt.Color;

import GridTools.MyGrid;

public class FlagMaker {

    /* *************************************************

    * Draws a single flag in the already created grid

    * that is passed as the first parameter

    * according to the countryCode passed as the

    * second parameter.     

    *

    * (Student: you will need to modify this method   - the code

    * provided here is for demonstration purposes only.         

    * *************************************************/

    public static void drawFlag(MyGrid grid, int countryCode){

        //Color red = Color.RED;

        int height = grid.getHt();       

        int width = grid.getWd();

        boolean draw = false;

       

        switch (countryCode){ //switch on country code (shorthand for nested if ifelse else)

            case 1: //POLAND

            case 2: //UKRAINE

            case 3: //CZECH

            case 6: if(height %2 == 0) //BAHAMAS

                        draw = true;

                    break;

            case 4: if(height %6 ==0)//BENIN

                        draw = true;

                    break;

            case 5: if(height %4 ==0)//RWANDA

                        draw = true;

                    break;

            case 7: if(height %3 ==0)//AFGHANISTAN

                        draw = true;

                    break;

            case 8:

            case 9: if(height %2 != 0)//JERSEY

                        draw = true;

                    break;

            default: errorFlag(grid); //if invalid country code

        }

        if(draw){

            switch (countryCode){

                //POLAND

                case 1: colorRowRange(grid, height/2, (height - 1), Color.RED); //height-1 = last row

                        break;

                //UKRAINE

                case 2: colorRowRange(grid, 0, (height/2 - 1), Color.BLUE); //color top half

                        colorRowRange(grid, height/2, (height-1), Color.YELLOW); //color bottom half

                        break;

                //CZECH

                case 3: colorRowRange(grid, height/2, (height - 1), Color.RED);//color bottom half

                        colorTriangle(grid, Color.BLUE);//color triangle, overwrites any space colored RED

                        break;

                //BENIN

                case 4: colorRowRange(grid, 0, (height/2 - 1), Color.YELLOW); //color top half yellow 0 to half the rows

                        colorRowRange(grid, (height/2 - 1), (height -1), Color.RED);//Color from half the rows to end

                        colorColRange(grid, 0, (width/3 - 1), Color.GREEN);//Color from left side to 1/3 the width                 

                        break;

                //RWANDA

                case 5: colorRowRange(grid, 0, (height/2 -1), Color.BLUE);//color half the rows

                        colorRowRange(grid, (height/2), (height - height/4) - 1, Color.YELLOW); //color from halfway to 3/4 the way (height - (1/4)height)

                        colorRowRange(grid, (3* (height/4)), (height - 1), Color.GREEN); //another way to think about, color from 3/4 the height to end               

                        break;

                //BAHAMAS

                case 6: colorRowRange(grid, 0, height - 1, Color.BLUE); //color whole thing blue

                        colorRowRange(grid, (height/2 - 1), (height/2), Color.YELLOW);//color two yellow stripes in the middle

                        colorTriangle(grid, Color.BLACK);

                        break;

                //AFGHANISTAN

                case 7: colorColRange(grid, 0, (width/3 - 1), Color.BLACK);//color from 0 to 1/3 width

                        colorColRange(grid, (width/3), (2 * (width/3) - 1), Color.RED);//color from 1/3 width to 2/3

                        colorColRange(grid, (2 * (width/3)), (width - 1), Color.GREEN); //Color from 2/3 width to end

                        break;

                //JERSEY

                case 8: colorX(grid, Color.RED); //call the colorX function

                        break;

                //Scotland

                case 9: colorColRange(grid, 0, (width - 1), Color.BLUE); //color the whole background blue

                        colorX(grid, Color.WHITE); //color the X in

                        break;

        }  

    }

        else

            errorFlag(grid); //if country code was valid, but size was incorrect

    }

    /*Helper functions for the drawFlag method appear below this line*/

    /*

    * This method takes an already created grid, a row number that

    * will be assumed to be a valid row on that grid and a color. It

    * then draws on that grid, in the indicated row, a full horizontal

    * line in the color indicated.

    * (Student: you may or may not wish to keep this method -- it is just

    * provided as a demonstration of what you may want to do.)

    */

    //Color a whole row

    private static void colorRow(MyGrid grid, int row, Color myColor){

        for (int col = 0; col < grid.getWd(); col++){

            grid.setColor(row,col,myColor);

        }     

    }

  

    //Colors a range of rows from rowStart to rowEnd

    private static void colorRowRange(MyGrid grid, int rowStart, int rowEnd, Color myColor){

        for(int i=rowStart; i<=rowEnd; i++){ //Loop through each row and call colorRow() for that row

            colorRow(grid, i, myColor);

        }

    }

   

    //Color a whole column, send to colorCol for range of 0 to Height of grid (all rows)

    private static void colorCol(MyGrid grid, int col, Color myColor){

        colorCol(grid, col, 0, (grid.getHt() - 1), myColor);

    }

   

    //Color a column from rows rowStart to rowEnd

    private static void colorCol(MyGrid grid, int col, int rowStart, int rowEnd, Color myColor){

        for(int i=rowStart; i<=rowEnd; i++){

            grid.setColor(i, col, myColor);

        }

    }

   

    //Color a range of columns from colStart to colEnd

    private static void colorColRange(MyGrid grid, int colStart, int colEnd, Color myColor){

        for(int i=colStart; i<=colEnd; i++){

            colorCol(grid, i, myColor); //set color of column i from start to finish

        }

    }

  

    //Create a triangle starting at left side of screen in column

    private static void colorTriangle(MyGrid grid, Color myColor){

        int width = grid.getWd();

        int height = grid.getHt();

       

        //Create first base column

        colorCol(grid, 0, myColor);

        //Create rest of triangle

        int rowStart = 0;

        int rowEnd = height - 1; //last row cell is height-1

        int columnEnd = (width/2) - 1; //ends one column before halfway point

        for(int i=1; i<columnEnd; i++){ //loop through each column starting with column 1 (the one after the first one 0)

            colorCol(grid, i, rowStart, rowEnd, myColor);

            rowStart += 1;

            rowEnd -= 1;

        }

    }

   

    //Color an X across the screen starting at edges and working way in

    private static void colorX(MyGrid grid, Color myColor){

        int height = grid.getHt();

        int width = 2*height;

       

        //Color the screen starting at each outside edge and working way in

        int rowTop = 0;

        int rowBot = height - 1;

        for(int i=0; i<width; i+=2){ //loop through each column

            colorCol(grid, i, rowTop, rowTop, myColor);//color starting from left going right of the top

            colorCol(grid, i+1, rowTop, rowTop, myColor);

            colorCol(grid, ((width-1) - i), rowTop, rowTop, myColor); //color starting from right going left of the top

            colorCol(grid, ((width-2) - i), rowTop, rowTop, myColor);

           

            colorCol(grid, i, rowBot, rowBot, myColor); //color starting from left going right of the bottom

            colorCol(grid, i+1, rowBot, rowBot, myColor);

            colorCol(grid, ((width-1) - i) , rowBot, rowBot, myColor); //color starting from right going to the left of the bottom

            colorCol(grid, ((width-1) - i), rowBot, rowBot, myColor);

            rowTop++;

            rowBot--;

        }

    }

   

    private static void errorFlag(MyGrid grid){

        int height = grid.getHt();

        int width = grid.getWd();

        //Color the four corners

        grid.setColor(0, 0, Color.RED);

        grid.setColor(height-1, 0, Color.RED);

        grid.setColor(0, width-1, Color.RED);

        grid.setColor(height-1, width-1, Color.RED);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote