Question: 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
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 Javas 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 g, 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, 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 rectangles 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 rectangles height should cover up the bottom white pixels of the underlying base rectangle but not beyond. Next, draw a filled blue rectangle (starfield). The starfields height should be equal to the stripe height times the number of red stripes. Given this height, the starfields width should be proportional to the flag (starfield height 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 to the left: 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.
I appreciate your time and effort greatly.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
