public class Triangle extends Figure
1:
2: /**
3: Class for triangles to be drawn on the screen. For this class,
4: a triangle points up and is completely determined by the size of
5: its base. (Screen character spacing determines the length of the
6: sides, given the base.)
7: Inherits getOffset, setOffset, and drawAt from the class Figure.
8: */
9: public class Triangle extends Figure
10: {
11: private int base;
12:
13: public Triangle( )
14: {
15: super( );
16: base = 0;
17: }
18:
19: public Triangle(int theOffset, int theBase)
20: {
21: super(theOffset);
22: base = theBase;
23: }
24:
25: public void reset(int newOffset, int newBase)
26: {
27: setOffset(newOffset);
28: base = newBase;
29: }
30:
31: /**
32: Draws the figure at current line.
33: */
34: public void drawHere( )
35: {
36: drawTop( );
37: drawBase( );
38: }
39:
40: private void drawBase( )
41: {
42: spaces(getOffset( ));
43: int count;
44: for (count = 0; count < base; count++)
45: System.out.print('*');
46: System.out.println( );
47: }
48:
49: private void drawTop( )
50: {
51: //startOfLine will be the number of spaces to the
52: //first '*' on a line. Initially set to the number
53: //of spaces before the top '*'.
54: int startOfLine = getOffset( ) + (base/2);
55: spaces(startOfLine);
56: System.out.println('*');//top '*'
57: int count;
58: int lineCount = (base/2) - 1;//height above base
59: //insideWidth == number of spaces between the
60: //two '*'s on a line.
61: int insideWidth = 1;
62: for (count = 0; count < lineCount; count++)
63: {
64: //Down one line, so the first '*' is one more
65: //space to the left.
66: startOfLine--;
67: spaces(startOfLine);
68: System.out.print('*');
69: spaces(insideWidth);
70: System.out.println('*');
71: //Down one line, so the inside is 2 spaces wider.
72: insideWidth = insideWidth + 2;
73: }
74: }
75:
76: private static void spaces(int number)
77: {
78: int count;
79: for (count = 0; count < number; count++)
80: System.out.print(' ');
81: }
82: }
83: