/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/logPolar/Controller.java

1    package bookExamples.ch26Graphics.carl.logPolar; 
2     
3    // package mosaics ; 
4     
5    import gui.ClosableJFrame; 
6    import gui.run.RunButton; 
7    import gui.run.RunTextFieldOld; 
8     
9    import java.awt.*; 
10    
11   import bookExamples.ch26Graphics.carl.logPolar.BackGroundColorButton; 
12   import bookExamples.ch26Graphics.carl.logPolar.ColorArray; 
13   import bookExamples.ch26Graphics.carl.logPolar.ConcentricRadialButton; 
14    
15   //import Jgui.*; 
16    
17   public class Controller { 
18       //Image aBuffer = createImage(400,400); 
19       private javax.swing.JPanel panControls; 
20    
21       private gui.run.RunTextFieldOld txt_shuffle; 
22       private gui.run.RunTextFieldOld txt_rings; 
23       private gui.run.RunTextFieldOld txt_rays; 
24    
25       private java.awt.GradientPaint gp; 
26       private javax.swing.Timer myTimer = new javax.swing.Timer(100, 
27               new TimerCycleColor(this) 
28       ); 
29       private final Logic logic; 
30    
31       //------------------------ 
32       // Constructor 
33       //------------------------ 
34       Controller() { 
35           logic = new Logic("Log Polar JFrame"); 
36           java.awt.Container c = logic.getClosableJFrame().getContentPane(); 
37    
38           // Construct default arrays ------ 
39           newArray(logic.getRings(), logic.getRays(), logic.isRadially()); 
40    
41           // Set up display JPanel---------------- 
42           setupDisplay(); 
43           //panDisplay.setPreferredSize(new Dimension(300,300) ); 
44    
45           // ----- Set up control JPanel ----------------- 
46           setupControlPanel(); 
47    
48           // ---------------------------------------- 
49           getPanControls().add(new ResetRaysButton(this) 
50           ); 
51    
52           // ---------------------------------------- 
53           getPanControls().add(new ShuffleButton()); 
54    
55           // ---------------------------------------- 
56           setTxt_shuffle( 
57                   new ShuffleTextfield()); 
58           getPanControls().add(getTxt_shuffle()); 
59    
60           // ---------------------------------------- 
61           getPanControls().add(new RingRaysButton(this) 
62           ); 
63    
64           // ---------------------------------------- 
65           setTxt_rings(new RingNumberTextFieldOld(this)); 
66           getPanControls().add(getTxt_rings()); 
67    
68           // ---------------------------------------- 
69           setTxt_rays(new RaysTextFieldOld(this)); 
70           getPanControls().add(getTxt_rays()); 
71    
72           getPanControls().add(new BackGroundColorButton(this) 
73           ); 
74    
75           getPanControls().add( 
76                   new TimerToggleButton(this) 
77           ); 
78    
79           getPanControls().add( 
80                   new ConcentricRadialButton(this) 
81           ); 
82    
83           // ----- Add panels to JFrame, and show --------- 
84           addPanelsToFrameAndShow(c); 
85    
86       } // End constructor ---------------------- 
87    
88       private void setupDisplay() { 
89           logic.setPanDisplay(new PaintPanel(this)); 
90           logic.getPanDisplay().setBackground(java.awt.Color.darkGray); 
91       } 
92    
93       private void setupControlPanel() { 
94           setPanControls(new javax.swing.JPanel()); 
95           getPanControls().setOpaque(false); // avoid repainting background 
96           getPanControls().setBackground(java.awt.Color.lightGray); 
97           getPanControls().setLayout(new java.awt.FlowLayout()); 
98       } 
99    
100      private void addPanelsToFrameAndShow(java.awt.Container c) { 
101          logic.getClosableJFrame().setSize(400, 400); 
102          logic.getPanDisplay().setLayout(new java.awt.FlowLayout()); 
103          getPanControls().setLayout(new java.awt.FlowLayout()); 
104          c.setLayout(new java.awt.GridLayout(0, 1)); 
105          c.add(logic.getPanDisplay()); 
106          ClosableJFrame pf = new ClosableJFrame("controls"); 
107          pf.setSize(200, 200); 
108          pf.getContentPane().add(getPanControls()); 
109          pf.setVisible(true); 
110          logic.getClosableJFrame().show(); 
111      } 
112   
113   
114      public void newArray(int _rings, int _rays, boolean _radial) { 
115          java.awt.Dimension d = logic.getClosableJFrame().getSize(); 
116          int w = d.width; 
117          int h = d.height; 
118          double r = w / 2; //to do, use size of screen 
119          // to compute the inner radius 
120          logic.setLppArray(new PointArray(_rings, _rays, 
121                  new java.awt.geom.Point2D.Float(300f, 300f), 20f)); 
122          logic.setCArray(new ColorArray(_rings, _rays, _radial)); 
123      } // -------------------------------------- 
124   
125   
126      // ---------------------------------------- 
127      public static void draw(Controller controller, java.awt.Graphics g) { 
128          java.awt.Graphics2D g2 = (java.awt.Graphics2D) g; 
129          float x, y, r; 
130          int rays = controller.logic.getLppArray().get_rayCount(); 
131          int rings = controller.logic.getLppArray().get_ringCount(); 
132          controller.setRenderingHints(g2); 
133   
134          //Draw circlets 
135          controller.drawCirclets(rays, rings, g2); 
136      } // End draw() 
137   
138      private void setRenderingHints(java.awt.Graphics2D g2) { 
139          java.awt.RenderingHints qualityHints; 
140          qualityHints = new 
141                  java.awt.RenderingHints(RenderingHints.KEY_ANTIALIASING, 
142                          RenderingHints.VALUE_ANTIALIAS_ON); 
143          g2.setRenderingHints(qualityHints); 
144      } 
145   
146      private void drawCirclets(int rays, int rings, java.awt.Graphics2D g2) { 
147          float x; 
148          float y; 
149          float r; 
150          for (int i_ray = 0; i_ray < rays; i_ray++) { 
151              drawRings(rings, i_ray, rays, g2); 
152          } 
153      } 
154   
155      private void drawRings(int rings, 
156                             int i_ray, 
157                             int rays, 
158                             java.awt.Graphics2D g2) { 
159          float x; 
160          float y; 
161          float r; 
162          for (int i_ring = 0; i_ring < rings; i_ring++) { 
163              x = logic.getLppArray().getPts()[i_ring][i_ray].x; 
164              y = logic.getLppArray().getPts()[i_ring][i_ray].y; 
165              r = logic.getLppArray().getRadii()[i_ring]; 
166              logic.e2d.setFrame( 
167                      x - r, y - r, 2f * r, 2f * r); 
168  //                  g2.setPaint(cArray.colorArray[i_ring][i_ray]); 
169              logic.p2.setLocation(x - r, y + r); 
170              logic.p1.setLocation(x + r, y + r); 
171              logic.color1 = logic.getCArray().getColorArray()[i_ring][(i_ray + logic.getIOffset()) % rays]; 
172              logic.color2 = logic.getCArray().getColorArray()[i_ring][(i_ray + logic.getIOffset() + rays / 6) % rays]; 
173  //                  color2 = color1.brighter().brighter().brighter(); 
174  // 
175   
176              g2.setPaint(java.awt.Color.white); 
177              g2.draw(logic.e2d); 
178              //      g2.setPaint(color1); 
179              gp 
180                      = new java.awt.GradientPaint(logic.p1, logic.color1, logic.p2, logic.color2); 
181              g2.setPaint(gp); 
182              g2.fill(logic.e2d); 
183  //              GradientPaint gp = new GradientPaint(p1, color1, p2, color2); 
184   
185              //  GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic) 
186   
187          } 
188      } 
189   
190      public javax.swing.JPanel getPanControls() { 
191          return panControls; 
192      } 
193   
194      public void setPanControls(javax.swing.JPanel panControls) { 
195          this.panControls = panControls; 
196      } 
197   
198      public RunTextFieldOld getTxt_shuffle() { 
199          return txt_shuffle; 
200      } 
201   
202      public void setTxt_shuffle(RunTextFieldOld txt_shuffle) { 
203          this.txt_shuffle = txt_shuffle; 
204      } 
205   
206      public RunTextFieldOld getTxt_rings() { 
207          return txt_rings; 
208      } 
209   
210      public void setTxt_rings(RunTextFieldOld txt_rings) { 
211          this.txt_rings = txt_rings; 
212      } 
213   
214      public RunTextFieldOld getTxt_rays() { 
215          return txt_rays; 
216      } 
217   
218      public void setTxt_rays(RunTextFieldOld txt_rays) { 
219          this.txt_rays = txt_rays; 
220      } 
221   
222      public void toggleTimer() { 
223          if (logic.isTimerON()) { 
224              getMyTimer().stop(); 
225              logic.setTimerON(false); 
226  //              iOffset = 0; 
227              return; 
228          } 
229          logic.setTimerON(true); 
230          getMyTimer().start(); 
231          logic.getClosableJFrame().repaint(); 
232      } 
233   
234      public javax.swing.Timer getMyTimer() { 
235          return myTimer; 
236      } 
237   
238      public void setMyTimer(javax.swing.Timer myTimer) { 
239          this.myTimer = myTimer; 
240      } 
241   
242      public Logic getLogic() { 
243          return logic; 
244      } 
245   
246      class ShuffleButton extends RunButton { 
247   
248          public ShuffleButton() { 
249              super("Shuffle by:"); 
250          } 
251   
252          public void run() { 
253              logic.getCArray().shuffleRays(logic.getShuffleBundle()); 
254              logic.getPanDisplay().repaint(); 
255          } 
256      } 
257   
258      class ShuffleTextfield extends RunTextFieldOld { 
259          public ShuffleTextfield() { 
260              super("1", 6); 
261          } 
262   
263          public void run() { 
264              logic.setShuffleBundle(Integer.parseInt(getText())); 
265          } 
266      } 
267   
268  } 
269