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. 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, you 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 64, 86, 53 flags as depicted; note that the historical 15 star flag didnt 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 doesnt work, youll 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.
Use this starter code,***
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class StarsAndStripes {
public static void drawFlag(int stars, int stripes, java.awt.Graphics g, int x, int y, int width, int height) {
// Fill this in according to the assignment!
}
public static void drawStar(java.awt.Graphics g, int x, int y, int size) {
// Fill this in according to the assignment!
}
// Only alter the "drawFlag" part of the paintComponent
// code to call it in different ways. You can also test
// drawing multiple flags at once!
public static void main(String[] args) {
JFrame window = new JFrame("Graphics window");
window.setLocationByPlatform(true);
final JLabel coords = new JLabel(" ");
@SuppressWarnings("serial")
final JPanel panel = new JPanel() {
protected void paintComponent(Graphics gx) {
coords.setText(" ");
Graphics2D g = (Graphics2D) gx;
int width = getWidth();
int height = getHeight();
g.setBackground(Color.BLACK);
// To make sure you cover the base rectangle!
g.clearRect(0, 0, width, height);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
// You could alter this code to try different flags!
drawFlag(15, 13, g, 0, 0, width/2, height/2);
drawFlag(20, 14, g, width/2, 0, width/2, height/2);
drawFlag(24, 15, g, 0, height/2, width/2, height/2);
drawFlag(48, 16, g, width/2, height/2, width/2, height/2);}
};
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
coords.setText(e.getX()+", "+e.getY());
}
});
window.setLayout(new BorderLayout());
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(d.width / 2, d.height / 2);
JPanel coordPanel = new JPanel();
coordPanel.setLayout(new BorderLayout());
coordPanel.setBorder(newBevelBorder(BevelBorder.LOWERED));
window.add(coordPanel, BorderLayout.SOUTH);
coordPanel.add(coords, BorderLayout.WEST);
window.setBackground(Color.WHITE);
// To make sure you cover the base rectangle!
panel.setBackground(Color.BLACK);
window.add(panel, BorderLayout.CENTER);
//window.setContentPane(panel);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
}
Greatly appreciate your time and effort thanks.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
