|
Box |
|
1 // joi/3/shapes/Box.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 /**
7 * A Box has a width, a height and a paintChar used
8 * used to paint the Box on a Screen.
9 *
10 * Examples:
11 * <pre>
12 * new Box( 3, 4, 'G' ) new Box( 1, 1, '$' )
13 *
14 * GGG $
15 * GGG
16 * GGG
17 * GGG
18 * </pre>
19 *
20 * @version 3
21 */
22
23 public class Box
24 {
25 private int width; // width in (character) pixels
26 private int height; // height in (character) pixels
27 private char paintChar; // character used for painting
28
29 /**
30 * Construct a box.
31 *
32 * @param width width in (character) pixels.
33 * @param height height in (character) pixels.
34 * @param paintChar character used for painting this Box.
35 */
36
37 public Box( int width, int height, char paintChar )
38 {
39 this.width = width;
40 this.height = height;
41 this.paintChar = paintChar;
42 }
43
44 /**
45 * Paint this Box on Screen s at position (x,y).
46 *
47 * @param s the screen on which this box is to be painted.
48 * @param x the x position for the box.
49 * @param y the y position for the box.
50 */
51
52 public void paintOn( Screen s, int x, int y )
53 {
54 HLine hline = new HLine( width, paintChar );
55 for ( int i = 0; i < height; i++ ) {
56 hline.paintOn( s, x, y+i );
57 }
58 }
59
60 /**
61 * Paint this Box on Screen s at position (0,0).
62 *
63 * @param s the Screen on which this box is to be painted.
64 */
65
66 public void paintOn( Screen s )
67 {
68 paintOn( s, 0, 0 ); // or this.paintOn(s,0,0);
69 }
70
71 /**
72 * Get the width of this Box.
73 *
74 * @return width of box (expressed as a number
75 * of characters).
76 */
77
78 public int getWidth()
79 {
80 return width;
81 }
82
83 /**
84 * Get the height of this Box.
85 *
86 * @return the height in (character) pixels.
87 */
88
89 public int getHeight()
90 {
91 return height;
92 }
93
94 /**
95 * Set the width of this Box.
96 *
97 * @param width the new width in (character) pixels.
98 */
99
100 public void setWidth( int width )
101 {
102 this.width = width;
103 }
104
105 /**
106 * Set the height of this Box.
107 *
108 * @param height the new height in (character) pixels.
109 */
110
111 public void setHeight( int height )
112 {
113 this.height = height;
114 }
115
116 /**
117 * Unit test for class Box,
118 * assuming Screen and Terminal work.
119 */
120
121 public static void main( String[] args )
122 {
123 Terminal terminal = new Terminal();
124
125 terminal.println( "Unit test of Box.");
126 terminal.println( "You should see this Screen twice: " );
127 terminal.println( "++++++++++++++++++++++");
128 terminal.println( "+RRRR +");
129 terminal.println( "+RRRR +");
130 terminal.println( "+RRGGG +");
131 terminal.println( "+RRGGG +");
132 terminal.println( "+RRGGG +");
133 terminal.println( "+ GGRRRRRRR +");
134 terminal.println( "++++++++++++++++++++++");
135 terminal.println();
136
137 Screen screen = new Screen( 20, 6 );
138
139 Box box1 = new Box( 4, 5, 'R' );
140 Box box2 = new Box( 3, 4, 'G' );
141
142 box1.paintOn( screen );
143 box2.paintOn( screen, 2, 2 );
144
145 // test reference model for objects
146 box2 = box1;
147 int oldWidth = box2.getWidth();
148 box1.setWidth(oldWidth+3);
149 box2.paintOn( screen, 4, 5 );
150
151 screen.draw( terminal );
152 }
153 }
154
|
Box |
|