/Users/lyon/j4p/src/bookExamples/ch26Graphics/radar/Radar.java

1    package bookExamples.ch26Graphics.radar; 
2     
3    import java.awt.*; 
4     
5     
6    public class Radar extends java.awt.Canvas { 
7     
8        private double thetaInRadians = 0; 
9        private Target targets[] = { 
10           new Target("t1", 30, 50) 
11       }; 
12    
13       Radar(int w, int h) { 
14           setSize(w, h); 
15       } 
16    
17       public void paint(java.awt.Graphics g) { 
18           java.awt.Dimension d = getSize(); 
19           double deltaTheta = 
20                   3 * Math.PI / 180.0; 
21           int width = d.width; 
22           int height = d.height; 
23           int xc = width / 2; 
24           int yc = height / 2; 
25           g.setColor(java.awt.Color.red); 
26    
27           int r = Math.min(xc, yc); 
28           double xr = 
29                   r * Math.cos(getThetaInRadians()) + xc; 
30           double yr = 
31                   r * Math.sin(getThetaInRadians()) + yc; 
32           setThetaInRadians(getThetaInRadians() + deltaTheta); 
33           //for (int i = 0; i < getTargets().length; i++) 
34           //    getTargets()[i].draw(g); 
35          // g.setXORMode(Color.red); 
36           g.drawLine(xc, yc, (int) xr, (int) yr); 
37           g.setColor(java.awt.Color.BLUE); 
38           g.drawString("target", (int) xr, (int) yr); 
39           sleep(50); 
40           repaint(); 
41       } 
42    
43       public static void sleep(long l) { 
44           try { 
45               Thread.sleep(l); 
46           } catch (InterruptedException e) { 
47               e.printStackTrace(); 
48           } 
49       } 
50    
51       public static void main(String args[]) { 
52           gui.ClosableJFrame cf = new gui.ClosableJFrame(); 
53           java.awt.Container c = cf.getContentPane(); 
54           Radar r = new Radar(200, 200); 
55           c.add(r); 
56           c.setLayout( 
57                   new GridLayout(1, 0)); 
58           cf.setSize(200, 200); 
59           cf.setVisible(true); 
60       } 
61    
62       public double getThetaInRadians() { 
63           return thetaInRadians; 
64       } 
65    
66       public void setThetaInRadians(double thetaInRadians) { 
67           this.thetaInRadians = thetaInRadians; 
68       } 
69    
70       public Target[] getTargets() { 
71           return targets; 
72       } 
73    
74       public void setTargets(Target[] targets) { 
75           this.targets = targets; 
76       } 
77   } 
78