Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have trouble to do the flag problem; --------------------- My code, just start; 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

Have trouble to do the flag problem;

image text in transcribed

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

My code, just start;

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 javax.swing.JFrame;

import javax.swing.JPanel;

public class StarsAndStripes {

public static void drawFlag(int stars, int stripes, Graphics g, int x, int y, int width, int height) {

}

public static void main(String[] args) {

JFrame window = new JFrame("Graphics window");

window.setLocationByPlatform(true);

@SuppressWarnings("serial")

final JPanel panel = new JPanel() {

protected void paintComponent(Graphics gx) {

Graphics2D g = (Graphics2D) gx;

int width = getWidth();

int height = getHeight();

g.clearRect(0, 0, width, height);

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setBackground(Color.WHITE);

g.setColor(Color.BLACK);

drawFlag(13, 13, g, 0, 0, width, height);

}

};

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

window.setSize(d.width/2, d.height/2);

window.setBackground(Color.WHITE);

panel.setBackground(Color.WHITE);

window.setContentPane(panel);

window.setVisible(true);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

In this assignment, you will write a class StarsAndStripes that can draw a generalized United States flag Throughout its history, the flag has varied in its number of stars and stripes and proportions. Here are a few Write this 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. ine Va hic olor.WH public static void drawFlag(int stars, int stripes, java.awt.Graphics g, int x, int y, int width, int height) 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 to the nearest integer. 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 an odd number of stripes are drawn, 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)). Calculate using exact arithmetic but round to the nearest integer Finally, we will draw white stars. Each star will be defined by its own upper left x, y, width and height and will be drawn as line segments in a similar manner as done by hand: 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 1/5 of the way from the right, up to the top center, and down to the starting point. Round to the nearest integer Your program should draw stars using the first of these configuration rules that works: (1) a rectangular grid of stars whose number of columns is greater than the number of rows but less than two times the number of rows (the 6x4 or 8x6 flags), or two interleaved grids of stars in a checkerboard pattern. Either (2) the number of rows and columns of the grids are equal, but the number of rows and columns of the second grid is one less than the first (the first flag of 3x3 and 2x2 13 stars). Or, (3) the number of rows of the first grid is one less than the number of columns of the first grid, and the number of rows of the second grid is one less than that (the current flag of 6x5 and 5x4 stars). Divide the starfield evenly into boxes (no extended size of the last star) and draw the stars according to the formula. Each box should be square, and the boxes should be centered horizontally and vertically in the starfield. If your program can't find a good configuration, it should leave the starfield empty EXTRA CREDIT: We will specify a few additional star configurations to consider

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

Step: 3

blur-text-image

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago