/Users/lyon/j4p/src/graphics/Bounce.java

1    package graphics; 
2     
3    import javax.swing.JFrame; 
4    import javax.swing.JPanel; 
5    import java.awt.Color; 
6    import java.awt.Component; 
7    import java.awt.Container; 
8    import java.awt.Graphics; 
9    import java.awt.Point; 
10    
11   /** 
12    * DocJava, Inc. User: lyon Date: Aug 31, 2004 
13    * Time: 9:41:52 AM 
14    */ 
15   public class Bounce extends JPanel { 
16       public static void main(String[] args) { 
17           JFrame jf = new JFrame(); 
18           jf.setSize(200, 200); 
19           Container c = jf.getContentPane(); 
20           c.add(new Bounce()); 
21           jf.show(); 
22       } 
23    
24       // Maximum speed for line points 
25       private final int MAX_SPEED = 4; 
26    
27       // First point on the line 
28       private Point p1 = new Point(); 
29    
30       // Second point on the line 
31       private Point p2 = new Point(); 
32    
33       // Direction of first point (vector) 
34       private Point dir1; 
35    
36       // Direction of second point (vector) 
37       private Point dir2; 
38    
39       // Color of line 
40       private Color lineColor = Color.blue; 
41    
42       private void randomizePoint(Point p, 
43                                   int width, 
44                                   int height) { 
45           p.x = (int) (Math.random() * width); 
46           p.y = (int) (Math.random() * height); 
47       } 
48    
49       private void bounce(Point p, 
50                           Point dir, 
51                           int width, 
52                           int height) { 
53           p.x += dir.x; 
54           if ((p.x < 0) || (p.x > width)) { 
55               p.x -= dir.x; 
56               if (dir.x < 0) { 
57                   dir.x = 
58                           Math.abs(randomVector()); 
59               } else { 
60                   dir.x = 
61                           -Math.abs(randomVector()); 
62               } 
63           } 
64           p.y += dir.y; 
65           if ((p.y < 0) || (p.y > height)) { 
66               p.y -= dir.y; 
67               if (dir.y < 0) { 
68                   dir.y = 
69                           Math.abs(randomVector()); 
70               } else { 
71                   dir.y = 
72                           -Math.abs(randomVector()); 
73               } 
74           } 
75    
76           // If we're still out of bounds, pick a brand new point: 
77           if ((p.x < 0) || (p.x > width) || 
78                   (p.y < 0) || 
79                   (p.y > height)) { 
80               randomizePoint(p, width, height); 
81           } 
82       } 
83    
84       /** 
85        * Returns a random number between -MAX_SPEED 
86        * and MAX_SPEED, inclusive, but excluding 0. 
87        */ 
88       private int randomVector() { 
89           int result = (int) (Math.random() * 
90                   MAX_SPEED) + 
91                   1; 
92           if (Math.random() > 0.5) { 
93               result = -result; 
94           } 
95           return result; 
96       } 
97    
98       public Bounce() { 
99           Component c = this; 
100          int width = c.getWidth(); 
101          int height = c.getHeight(); 
102          randomizePoint(p1, width, height); 
103          randomizePoint(p2, width, height); 
104          dir1 = 
105                  new Point(randomVector(), 
106                          randomVector()); 
107          dir2 = 
108                  new Point(randomVector(), 
109                          randomVector()); 
110          String colorOption = "#FF0000"; 
111          if (colorOption != null) { 
112              String COLOR_FORMAT = "Color must be in #rrggbb format"; 
113              if (colorOption.length() != 7) { 
114                  throw new RuntimeException(COLOR_FORMAT); 
115              } 
116              try { 
117                  int r = Integer.parseInt(colorOption.substring(1, 
118                          3), 
119                          16); 
120                  int g = Integer.parseInt(colorOption.substring(3, 
121                          5), 
122                          16); 
123                  int b = Integer.parseInt(colorOption.substring(5, 
124                          7), 
125                          16); 
126                  lineColor = new Color(r, g, b); 
127              } catch (NumberFormatException e) { 
128                  throw new RuntimeException(COLOR_FORMAT); 
129              } 
130          } 
131      } 
132      // for homework, make it so that bounce draws with 
133      // random colors 
134      public void paint(Graphics g) { 
135          Component c = this; 
136          int width = c.getWidth(); 
137          int height = c.getHeight(); 
138   
139          // Erase old line: 
140          erase(g, c); 
141   
142          // Move points and bounce off walls: 
143          bounce(p1, dir1, width, height); 
144          bounce(p2, dir2, width, height); 
145   
146          // Draw new line: 
147          drawNewLine(g); 
148          try { 
149              Thread.sleep(10); 
150          } catch (InterruptedException e) { 
151              e.printStackTrace(); 
152          } 
153          repaint(); 
154      } 
155      // implement the selection of the random 
156      // colors so that they come from the 
157      // ColorGridCanvas. 
158      private Color getRandomColor() { 
159          return Color.cyan; 
160      } 
161      private void drawNewLine(Graphics g) { 
162          lineColor = getRandomColor(); 
163          g.setColor(lineColor); 
164          g.drawLine(p1.x, p1.y, p2.x, p2.y); 
165          g.drawString("DocJava", p1.x, p1.y); 
166          g.drawString("DocJava", p2.x, p2.y); 
167      } 
168   
169      private void erase(Graphics g, Component c) { 
170          g.setColor(c.getBackground()); 
171          g.drawLine(p1.x, p1.y, p2.x, p2.y); 
172          g.drawString("DocJava", p1.x, p1.y); 
173          g.drawString("DocJava", p2.x, p2.y); 
174      } 
175  } 
176