A software company that writes applications used to land- scape property wants to add a feature to its application that helps users design brick walls around their property. Write an application that will display a brick wall that is 10 bricks high and 9 bricks wide. You must leave some room between each brick, and every other row must be offset horizontally by half a brick from the bricks in the adjacent row. The completed application should appear as in Fig. 20.32.



a) Copying the template to your working directory. Copy the C:Examples Tutorial20ExercisesBrickWall directory to your C:SimplyJava directory.

b) Opening the DrawJPanel template file. Open the template file DrawJPanel.java in your text editor.

c) Cycling through each row and column of bricks. Inside the drawBricks method (starts at line 45), start an outer for loop to cycle through each row of bricks before the repaint method call (line 58). The counter should start at 0 and iterate through 9 (inclusive), because there are a total of 10 rows. Inside the outer for loop, declare a new int variable y. Initialize this variable to 25 times the row variable. Then begin an inner for loop to iterate through each column of bricks. This for loop should also start at 0 and iterate through 9 (inclusive). Since some rows only have 9 columns of bricks, some bricks will be printed outside of the drawingJPanel. However, these bricks will be invisible to the application user, so you will not remov

```
1 // BrickWall.java
2 // Application builds a brick wall using rectangles.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class BrickWall extends JFrame
8 {
9 // DrawJPanel for displaying bricks
10 private DrawJPanel drawingJPanel;
11
12 // no-argument constructor
13 public BrickWall()
14 {
15 createUserInterface();
16 }
17
18 // create and position GUI components; register event handlers
19 private void createUserInterface()
20 {
21 // get content pane for attaching GUI components
22 Container contentPane = getContentPane();
23
24 // enable explicit positioning of GUI components
25 contentPane.setLayout( null );
26
27 // set up drawingJPanel
28 drawingJPanel = new DrawJPanel();
29 drawingJPanel.setBounds( 0, 0, 450, 250 );
30 drawingJPanel.setBackground( Color.WHITE );
31 contentPane.add( drawingJPanel );
32
33 // set properties of application’s window
34 setTitle( "Brick Wall" ); // set title bar text
35 setSize( 454, 275 ); // set window size
36 setVisible( true ); // display window
37
38 } // end method createUserInterface
39
40 // main method
41 public static void main( String[] args )
42 {
43 BrickWall application = new BrickWall();
44 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
45
46 } // end method main
47
48 } // end class BrickWall
```
```
1 // DrawJPanel.java
2 // This class defines the DrawJPanel object.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.Random;
8 import javax.swing.*;
9
10 public class DrawJPanel extends JPanel
11 {
12 // ArrayList object to hold MyRectangle objects
13 private ArrayList brickArrayList = new ArrayList();
14
15 // no-argument constructor
16 public DrawJPanel()
17 {
18 super();
19
20 drawBricks(); // draw the brick wall
21
22 } // end constructor
23
24 // add randomNumber brick to ArrayList and draw all brick
25 public void paintComponent( Graphics g )
26 {
27 super.paintComponent( g );
28
29 // create iterator
30 Iterator traverse = brickArrayList.iterator();
31
32 // iterate through ArrayList and draw all MyRectangles
33 while ( traverse.hasNext() )
34 {
35 MyRectangle currentRectangle =
36 ( MyRectangle ) traverse.next();
37
38 currentBrick.drawMyBrick( g ); // draw brick
39
40 } // end while loop
41
42 } // end method paintComponent
43
44 // draw 10 rows and 9 columns of bricks
45 public void drawBricks()
46 {
47 // declare a new MyRectangle object
48 MyRectangle brick;
49
50 // set the bricks color
51 Color myColor = Color.RED;
52
53 // initialize width and height variables
54 int width = 45;
55 int height = 20;
56
57 // set the y position
58 for ( int row = 0; row <= 9; row++ )
59 {
60 int y = row * 25;
61
62 // set the y position
63 for ( int column = 0; column <= 9; column++ )
64 {
65 int x = column * 50;
66
67 // if row is odd
68 if ( row % 2 == 1 )
69 {
70 brick = new MyRectangle( x, y, width, height,
71 myColor );
72 brickArrayList.add( brick );
73 }
74 else
75 {
76 brick = new MyRectangle( x - 25, y, width,
77 height, myColor );
78 brickArrayList.add( brick );
79 }
80
81 } // end inner for
82
83 } // end outer for
84
85 // repaint JPanel
86 repaint();
87
88 } // end method drawBricks
89
90 } // end class DrawJPanel
```
```
1 // MyRectangle.java
2 // This class defines the MyRectangle object.
3 import java.awt.*;
4
5 public class MyRectangle extends Rectangle
6 {
7 // instance variable to hold fillColor of MyRectangle
8 private Color fillColor;
9
10 // constructor
11 public MyRectangle( int xValue, int yValue, int widthValue,
12 int heightValue, Color colorValue )
13 {
14 // call constructor of superclass
15 super( xValue, yValue, widthValue, heightValue );
16
17 // set fillColor of MyRectangle
18 setFillColor( colorValue );
19
20 } // end constructor
21
22 // set fillColor value
23 public void setFillColor( Color colorValue )
24 {
25 fillColor = colorValue;
26
27 } // end method setColor
28
29 // get fillColor value
30 public Color getFillColor()
31 {
32 return fillColor;
33
34 } // end method getColor
35
36 // draw MyRectangle
37 public void drawMyRectangle( Graphics g )
38 {
39 g.setColor( fillColor );
40 g.fillRect( x, y, width, height );
41
42 } // end method drawMyRectangle
43
44 } // end class MyRectangle
```

Computer Science & Information Technology

You might also like to view...

The class JFrame has a method named __________ that eliminates the invoking JFrame without terminating the program.

a. close b. dispose c. eliminate d. None of the above

Computer Science & Information Technology

Windows 2000 and later applications store passwords in the form of hash values in a database called ______________________________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology