Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The link to drawingPanel.java http://www.buildingjavaprograms.com/code-files/4ed/DrawingPanel.java Will need to download DrawingPanel.java and place it in the same directory as the program that will be using it.

The link to drawingPanel.java http://www.buildingjavaprograms.com/code-files/4ed/DrawingPanel.java

Will need to download DrawingPanel.java and place it in the same directory as the program that will be using it.

For the first part of this project, you will create a program that draws a pattern made up of 1 - 10 rows of bricks. Each brick is 80 pixels wide and 40 pixels high. The brick is drawn in a custom color entered by the user with a black border that is 4 pixels wide - the overall brick dimensions of 80 pixels wide and 40 pixels high include the black border. The first (bottommost) row contains 6 bricks. The second row (if there is one) contains a half-brick, followed by 5 whole bricks, followed by a half-brick. Subsequent rows alternate between having 6 bricks and having a half-brick followed by 5 bricks followed by another half-brick. The bricks are contiguous and the (x, y) coordinates of upper left-hand point of the first brick in the first (bottommost) row must be (20, 420). The user will create a custom color for the bricks by specifying the amount of red, green, and blue used to draw them. The user will also specify the number of rows of bricks.

Your program must be named Bricks and use the DrawingPanel class provided by the textbook to create a drawing panel (canvas) with a width of 520 pixels and a height of 520 pixels and then draw the bricks pattern. Prompt the user for a number of rows between 1 and 10 for the bricks pattern. Convert numbers below 1 to 1 and numbers above 10 to 10. Then prompt the user for the Red, Green, and Blue values used to create the custom color for the bricks. These values should be between 0 and 255 convert numbers below 0 to 0 and numbers above 255 to 255.

After drawing the bricks pattern, output a message to the user to close the Drawing Panel in order to exit the program.

For example, the input below,

csc$ java Bricks Number of rows (1-10): 5 Red value (0-255): 255 Green value (0-255): 5 Blue value (0-255): 5 *CLOSE the Drawing Panel to exit the program* 

would result in the bricks pattern shown below:

image text in transcribed

Reminder: Compiling and Running with src and bin directories.

With src and bin directory structure, to compile and run Bricks, you will compile DrawingPanel prior to compiling Bricks:

% pwd ~/csc116/csc116-001-P2-40/Project2 % javac -d bin src/DrawingPanel.java % javac -d bin -cp bin src/Bricks.java % java -cp bin Bricks 

OR you can compile all files at once:

% pwd ~/csc116/csc116-001-P2-40/Project2 % javac -d bin -cp bin src/*.java % java -cp bin Bricks 

Input/Error Handling

If the user enters a number of rows below 1, convert the value to 1. Likewise, if the user enters a number of rows above 10, convert the value to 10. If the user enters a color value below 0, convert it to 0, and if the user enters a color value above 255, convert it to 255.

For example, the input below,

csc$ java Bricks Number of rows (1-10): 12 Red value (0-255): -5 Green value (0-255): 300 Blue value (0-255): 210 *CLOSE the Drawing Panel to exit the program* 

would result in the pattern shown below:

image text in transcribed

If the user enters something other than an integer value, the program will throw an exception:

csc$ java Bricks Number of rows (1-10): 5 Red value (0-255): 56 Green value (0-255): abc Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Bricks.main(Bricks.java:30) 

This behavior is fine for this program. Later we will learn to handle this situation more gracefully!

Design Your Software

The main method must prompt the user for the input values as integers and use the DrawingPanel class to create the panel. It must call the following drawBrick() method to draw the bricks from within a nested loop. This method must be defined as shown below. Pass the custom color to the method. Replace the comments with appropriate Javadoc.

 //Draws a brick at (x,y) with the given color, width, height, and border width //The brick should be drawn in the given color with a black border public static void drawBrick(Graphics g, Color color, int x, int y, int width, int height, int borderWidth) { } 

Implement Your Software

The DrawingPanel file should be compiled and in the same directory with your Bricks.java file.

Import the java.util and java.awt packages so that your program can use the Scanner and Graphics classes.

Use named constants in your program rather than magic numbers. You do not have to define constants for the numbers 0, 1, or 2.

Use a nested for loop within your main method to draw the bricks. Using loops that start at 0 rather than 1 to draw the rows as well as the bricks within each row may make calculating the upper left-hand coordinates of each brick easier.

The Math.min and Math.max methods may be used to bound the numbers of rows as well as the color values entered by the user. You may also use if-else statements, if you prefer.

Test Your Software

Thoroughly test your program using various numbers of rows with different color values. Use the input values given in the example above and compare your bricks pattern with the provided drawing panel output.

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

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions