|
HLine |
|
1 // joi/3/shapes/HLine.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 /**
7 * A horizontal line has a length and a paintChar used
8 * used to paint the line on a Screen.
9 *
10 * @version 3
11 */
12
13 public class HLine
14 {
15 private int length; // length in (character) pixels.
16 private char paintChar; // character used for painting.
17
18 /**
19 * Construct an HLine.
20 *
21 * @param length length in (character) pixels.
22 * @param paintChar character used for painting this Line.
23 */
24
25 public HLine( int length, char paintChar )
26 {
27 this.length = length;
28 this.paintChar = paintChar;
29 }
30
31 /**
32 * Paint this HLine on Screen s at position (x,y).
33 *
34 * @param s the Screen on which this line is to be painted.
35 * @param x the x position for the line.
36 * @param y the y position for the line.
37 */
38
39 public void paintOn( Screen s, int x, int y )
40 {
41 for ( int i = 0; i < length; i = i+1 ) {
42 s.paintAt( paintChar, x+i , y );
43 }
44 }
45
46 /**
47 * Paint this HLine on Screen s at position (0,0).
48 *
49 * @param s the Screen on which this line is to be painted.
50 */
51
52 public void paintOn( Screen s )
53 {
54 paintOn( s, 0, 0 );
55 }
56
57 /**
58 * Get the length of this line.
59 *
60 * @return the length in (character) pixels.
61 */
62
63 public int getLength()
64 {
65 return length;
66 }
67
68 /**
69 * Set the length of this line.
70 *
71 * @param length the new length in (character) pixels.
72 */
73
74 public void setLength( int length )
75 {
76 this.length = length;
77 }
78
79 /**
80 * Unit test for class HLine,
81 * assuming Screen and Terminal work.
82 */
83
84 public static void main( String[] args )
85 {
86 Terminal terminal = new Terminal();
87
88 terminal.println( "Unit test of HLine.");
89 terminal.println( "You should see this Screen twice: " );
90 terminal.println( "++++++++++++++++++++++");
91 terminal.println( "+xxxxxxxxxx +");
92 terminal.println( "+xxxxx +");
93 terminal.println( "+ +");
94 terminal.println( "+ ***** +");
95 terminal.println( "+ 1 +");
96 terminal.println( "+ +");
97 terminal.println( "++++++++++++++++++++++");
98 terminal.println( "");
99
100 Screen screen = new Screen( 20, 6 );
101
102 HLine hline1 = new HLine( 10, 'x' );
103 HLine hline2 = new HLine( 5, '*' );
104 HLine hline3 = new HLine( 1, '1' );
105
106 hline1.paintOn( screen );
107 hline1.setLength(5);
108 hline1.paintOn( screen, 0, 1 );
109 hline2.paintOn( screen, 3, 3 );
110 hline3.paintOn( screen, 4, 4 );
111
112 screen.draw( terminal );
113 }
114 }
115
|
HLine |
|