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