/Users/lyon/j4p/src/bookExamples/ch08ArraysAndVectors/array/CharacterGraphics.java

1    package bookExamples.ch08ArraysAndVectors.array; 
2     
3    public class CharacterGraphics { 
4        char screen[][] = new char[20][20]; 
5     
6        public void dot(int x, int y) { 
7            if (x < 0) return; 
8            if (y < 0) return; 
9            if (x >= screen.length) return; 
10           if (y >= screen[0].length) return; 
11           screen[y][x] = '*'; 
12       } 
13    
14       public void circle(int xc, int yc, int r) { 
15           for (double t = 0; t < 1; t = t + .01) { 
16               int ix = (int) (t * 20); 
17               double x = t * 2 * Math.PI; 
18               ix = (int) (r * Math.sin(x) + xc); 
19               int iy = (int) (r * Math.cos(x) + yc); 
20               dot(ix, iy); 
21           } 
22       } 
23    
24       public void diag() { 
25           for (int i = 0; i < screen.length; i++) 
26               for (int j = 0; j < screen[i].length; j++) 
27                   if (i == j) 
28                       screen[i][j] = '*'; 
29       } 
30    
31       public void fillScreen(char fillChar) { 
32           for (int i = 0; i < screen.length; i++) 
33               for (int j = 0; j < screen[i].length; j++) 
34                   screen[i][j] = fillChar; 
35       } 
36    
37       public void printScreen() { 
38           for (int i = screen.length - 1; i > 0; i--) { 
39               for (int j = 0; j < screen[i].length; j++) 
40                   System.out.print(screen[i][j]); 
41               System.out.println(); 
42           } 
43       } 
44    
45       public void axis() { 
46           for (int i = 0; i < screen.length; i++) 
47               for (int j = 0; j < screen[i].length; j++) { 
48                   if (i == 1) 
49                       screen[i][j] = '-'; 
50                   if (j == 0) 
51                       screen[i][j] = '|'; 
52    
53               } 
54           screen[1][screen[1].length - 2] = '>'; 
55           screen[1][screen[1].length - 1] = 'x'; 
56    
57           screen[screen[0].length - 2][0] = '^'; 
58           screen[screen[0].length - 1][0] = 'y'; 
59       } 
60    
61       public void print(Object a[]) { 
62           for (int i = 0; i < a.length; i++) 
63               System.out.println(a[i]); 
64       } 
65    
66    
67       public static void main(String args[]) { 
68           CharacterGraphics at = new CharacterGraphics(); 
69           at.fillScreen('.'); 
70           at.axis(); 
71           at.circle(5, 5, 5); 
72           at.diag(); 
73           at.printScreen(); 
74       } 
75   } 
76