Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Open with CS141 ASSIGNMENT 2 - STARS AND STRIPES FOREVER PIERCE COLLEGE DUE MODAY FEBRUARY 10, 2020 In this assignment, you will write a class

image text in transcribedimage text in transcribed

Open with CS141 ASSIGNMENT 2 - STARS AND STRIPES FOREVER PIERCE COLLEGE DUE MODAY FEBRUARY 10, 2020 In this assignment, you will write a class StarsAndStripes that can draw a generalized United States flag. The following image represents some of the kinds of flags that your program will be able to draw: XXX Write the following method which draws a US flag representation with the given number of stars and stripes starting at the given (x, y) coordinate (upper left corner in Java's coordinate system) and of the given width and height. Notice that a flag may only fill part of the window like the six flags drawn above! public static void drawFlag(int stars, int stripes, java.awt.Graphics &, int x, int y, int width, int height) Assume stars, stripes, width, and height are at least one. You should only call fillRect, drawLine, and setColor methods on the java.awt.Graphics object. Color.red, Color white, and Color.blue are the only colors allowed. To reduce confusion and differences in calculation, you should only perform math with int, not with double. In addition, another method is specified on the back of this worksheet. Your drawFlag method should follow the following procedure so that it can be properly tested: First: Draw a filled white rectangle (base) of the given x, y, width, and height. Next: Draw filled red rectangles (stripes) across the entire width. The first rectangle should begin in the upper left corner. Each red rectangle's height should be of height divided by stripes, rounded down. Then, skip down according to the stripe height which will expose the white base rectangle. Draw a number of stripes equal to half of stripes rounded up. If stripes is odd, the final red rectangle's height should cover up the bottom white pixels of the underlying base rectangle but not beyond. Next: Draw a filled blue rectangle (starfield). The starfield's height should be equal to the stripe height times the number of red stripes. Given this height, the starfield's width should be proportional to the flag (starfield height x base width / base height). You should round down for the width calculation. Finally: We will draw white stars. Each star will have its own upper left (x, y) as well as an equal width and height and will be drawn as line segments resembling the diagram below: from the bottom 1/5 of the way from the left to 2/5 of the way from the top on the right, straight across to the left, down to the bottom 4/5 of the way from the left, up to the top center, and back down to the starting point. Page 1 1 2 0 + Open with Each calculation to draw the star line coordinates should round down. You should implement the following helper method which draws a single star and call it from your drawFlag method when it needs to draw stars (note that size denotes both width and height): public static void drawStar (java.awt.Graphics g, int x, int y, int size) This will allow the test program to separately test your star drawing procedure and make sure that it works correctly. Once you know how to draw a star using the above method, vou will have to place a bunch of them on your flag! Your program will have to plan out a strategy to figure out where the stars should be drawn. Determine whether the stars can be drawn in a rectangular grid whose number of columns is greater than the number of rows but less than two times the number of rows (the 6 x 4,8 x 6,5 x 3 flags as depicted; note that the historical 15 star flag didn't actually use this arrangement). Given the number of stars as an argument, your code should search for all possible ways to divide it up. If so, draw the stars in the rectangular grid pattern of stars inside the starfield. When drawing stars, each star should be the same size, the stars should be as large as they can be within their grid while still fitting in the starfield, and if there is leftover space, try to center the stars within the starfield. Note that due to line thickness, the edges of the lines may go beyond the starfield, but the lines coordinates should stay within it. Otherwise, for extra credit, if the above strategy doesn't work, you'll have to draw the stars in a checkerboard grid. Notice that this is the case for the 13 star flag and the 50 star flag. First, try to consider an odd-by-odd checkerboard of stars where the columns is equal to the rows or two more than the rows and the upper left corner is in use: this is the case for the 13 star flag (5 by 5 checkerboard of stars) and the 50 star flag (11 by 9 checkerboard of stars). To receive extra credit, your code should not hardcode the 13 and 50 star flag arrangements but should find them algorithmically using this approach, which should work for other sizes. Otherwise, for additional extra credit, consider any possible checkerboard arrangement where the number of columns is greater than or equal to the number of rows but less than three times the number of rows. You should prefer odd numbers of rows and columns and prefer using the upper left corner of the checkerboard. The bottom center checkerboard is the arrangement of stars on the 49 star flag. Here are examples of 51, 52, 55, 58, 59, and 61 star staggered flags with increasing numbers of stripes: PETE Page 21 2 - Q + Open with CS141 ASSIGNMENT 2 - STARS AND STRIPES FOREVER PIERCE COLLEGE DUE MODAY FEBRUARY 10, 2020 In this assignment, you will write a class StarsAndStripes that can draw a generalized United States flag. The following image represents some of the kinds of flags that your program will be able to draw: XXX Write the following method which draws a US flag representation with the given number of stars and stripes starting at the given (x, y) coordinate (upper left corner in Java's coordinate system) and of the given width and height. Notice that a flag may only fill part of the window like the six flags drawn above! public static void drawFlag(int stars, int stripes, java.awt.Graphics &, int x, int y, int width, int height) Assume stars, stripes, width, and height are at least one. You should only call fillRect, drawLine, and setColor methods on the java.awt.Graphics object. Color.red, Color white, and Color.blue are the only colors allowed. To reduce confusion and differences in calculation, you should only perform math with int, not with double. In addition, another method is specified on the back of this worksheet. Your drawFlag method should follow the following procedure so that it can be properly tested: First: Draw a filled white rectangle (base) of the given x, y, width, and height. Next: Draw filled red rectangles (stripes) across the entire width. The first rectangle should begin in the upper left corner. Each red rectangle's height should be of height divided by stripes, rounded down. Then, skip down according to the stripe height which will expose the white base rectangle. Draw a number of stripes equal to half of stripes rounded up. If stripes is odd, the final red rectangle's height should cover up the bottom white pixels of the underlying base rectangle but not beyond. Next: Draw a filled blue rectangle (starfield). The starfield's height should be equal to the stripe height times the number of red stripes. Given this height, the starfield's width should be proportional to the flag (starfield height x base width / base height). You should round down for the width calculation. Finally: We will draw white stars. Each star will have its own upper left (x, y) as well as an equal width and height and will be drawn as line segments resembling the diagram below: from the bottom 1/5 of the way from the left to 2/5 of the way from the top on the right, straight across to the left, down to the bottom 4/5 of the way from the left, up to the top center, and back down to the starting point. Page 1 1 2 0 + Open with Each calculation to draw the star line coordinates should round down. You should implement the following helper method which draws a single star and call it from your drawFlag method when it needs to draw stars (note that size denotes both width and height): public static void drawStar (java.awt.Graphics g, int x, int y, int size) This will allow the test program to separately test your star drawing procedure and make sure that it works correctly. Once you know how to draw a star using the above method, vou will have to place a bunch of them on your flag! Your program will have to plan out a strategy to figure out where the stars should be drawn. Determine whether the stars can be drawn in a rectangular grid whose number of columns is greater than the number of rows but less than two times the number of rows (the 6 x 4,8 x 6,5 x 3 flags as depicted; note that the historical 15 star flag didn't actually use this arrangement). Given the number of stars as an argument, your code should search for all possible ways to divide it up. If so, draw the stars in the rectangular grid pattern of stars inside the starfield. When drawing stars, each star should be the same size, the stars should be as large as they can be within their grid while still fitting in the starfield, and if there is leftover space, try to center the stars within the starfield. Note that due to line thickness, the edges of the lines may go beyond the starfield, but the lines coordinates should stay within it. Otherwise, for extra credit, if the above strategy doesn't work, you'll have to draw the stars in a checkerboard grid. Notice that this is the case for the 13 star flag and the 50 star flag. First, try to consider an odd-by-odd checkerboard of stars where the columns is equal to the rows or two more than the rows and the upper left corner is in use: this is the case for the 13 star flag (5 by 5 checkerboard of stars) and the 50 star flag (11 by 9 checkerboard of stars). To receive extra credit, your code should not hardcode the 13 and 50 star flag arrangements but should find them algorithmically using this approach, which should work for other sizes. Otherwise, for additional extra credit, consider any possible checkerboard arrangement where the number of columns is greater than or equal to the number of rows but less than three times the number of rows. You should prefer odd numbers of rows and columns and prefer using the upper left corner of the checkerboard. The bottom center checkerboard is the arrangement of stars on the 49 star flag. Here are examples of 51, 52, 55, 58, 59, and 61 star staggered flags with increasing numbers of stripes: PETE Page 21 2 - Q +

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Given A = [E1, E3, E6, E9], define A.

Answered: 1 week ago