1  import java.awt.*;
  2  import java.lang.Math;
  3  import java.util.Random;
  4  
  5  public class Sliders extends java.applet.Applet  {
  6    MyCanvasClass MyCanvas;
  7    MyControlPanel MyPanel;
  8    public void init() {
  9      setLayout(new GridLayout(1,2,15,10));
 10      MyCanvas = new  MyCanvasClass(this);
 11      MyPanel  = new  MyControlPanel(this);
 12      add(MyCanvas);
 13      add(MyPanel);
 14    }
 15    
 16    public void paint(Graphics g)  {
 17      MyCanvas.setBackground(Color.black);
 18      MyPanel.setBackground(Color.lightGray);
 19      MyCanvas.paint(g);
 20      MyPanel.paint(g);
 21    }
 22    
 23    public  void  start()  {
 24      MyCanvas.start();
 25      MyPanel.start();
 26    }
 27    
 28    public  void  stop()  {
 29      MyCanvas.stop();
 30    }
 31    
 32    public  void  update(Graphics g)  {
 33      MyCanvas.paint(g);
 34    }
 35  }
 36  
 37  class MyCanvasClass extends  Canvas {
 38    Sliders outerparent;
 39    public int W, H;
 40    public int N = 1;
 41    public final Color bg = Color.black;
 42    boolean threadSuspended = false;
 43    Image im;
 44    Graphics offscreen;
 45    Random rand;
 46    NBrick nbricks;
 47    Color  color;
 48    
 49    MyCanvasClass(Sliders outerparent) {
 50      this.outerparent = outerparent;
 51      Rectangle r=outerparent.bounds();
 52      W=(r.width)/2;
 53      H=r.height;        
 54      this.outerparent= outerparent;
 55      rand = new Random((long)System.currentTimeMillis());
 56      try {
 57        im = createImage(W, H);
 58        offscreen = im.getGraphics();
 59      } catch (Exception e) {
 60        offscreen = null;
 61      }
 62    }
 63    
 64    
 65    public  void  start()  {
 66      if (nbricks == null)  {
 67        nbricks = new NBrick(this, 10, 10, Color.pink, bg, bounds());
 68        nbricks.start();
 69      }
 70      
 71      repaint();
 72    }
 73    
 74    public  void paint(Graphics g) {
 75      if  (offscreen  != null) {
 76        paintCanvas(offscreen);
 77        g.drawImage(im, 0, 0, this);
 78      } else {
 79        paintCanvas(g);
 80      }
 81    }
 82    
 83    
 84    public  void  stop()  {
 85      nbricks.stop();
 86      nbricks=null;
 87    }
 88    
 89    public void paintCanvas(Graphics g) {
 90      boolean changed = false;
 91      g.setColor(bg);
 92      g.fillRect(0, 0, W, H);
 93      nbricks.paint(g);
 94    }
 95  }
 96  
 97  class NBrick  implements  Runnable {
 98    static int threadNum = 1;   
 99    public  int num_item=1;
100    private Color color = null;
101    private Color bg = Color.black;
102    private int size = 50;
103    private int x = 0;
104    private int y = 0;
105    private int dx=0;
106    private int dy=3;
107    private int naptime;
108    private Rectangle rect;
109    private int w, h;
110    public Thread thread = null;
111    public boolean changed = true;
112    public MyCanvasClass tempcanvas;
113    final int const_naptime=200;
114    final int start_width=20;
115    final int start_height=10;
116    Color  colorarray[] = { Color.lightGray, Color.darkGray,  Color.red,
117  			  Color.green, Color.blue,  Color.yellow,  Color.magenta, 
118  			  Color.cyan,  Color.pink,  Color.orange};
119    
120    public NBrick(MyCanvasClass tempcanvas, 
121  		int _x, int _y, Color c, Color bg, Rectangle r) {
122      this.tempcanvas = tempcanvas;
123      x = _x;
124      y = _y;
125      w = start_width;
126      h = start_height;
127      color = c;
128      rect = r;
129      naptime = const_naptime;
130      if (color == null) {
131        color = Color.black;
132      }
133    }
134    
135    public void run() {
136      thread.setPriority(Thread.MIN_PRIORITY+1);
137      while (thread != null) {
138        changed = true;
139        update();
140        try { thread.sleep(naptime); } catch (InterruptedException e) {};
141        tempcanvas.repaint();
142      }
143      thread = null;
144    }
145    
146    public void update() {
147      checkBoundry();
148      move();
149    }
150    
151    public void move() {
152      x += dx;    
153      y += dy;
154    }
155    
156    public  void  change_velocity(int v_factor)  {
157      naptime = const_naptime / v_factor;
158    }
159    
160    public  void  change_width(int w_factor)  {
161      w = start_width * w_factor;
162    }
163    
164    public  void  change_height(int h_factor)  {
165      h = start_height * h_factor;
166    }
167    
168    public  void change_color(int tempcolor)  {
169      color = colorarray[tempcolor];
170    }         
171    
172    public void start() {
173      if (thread == null) {
174        thread = new Thread(this, Integer.toString(threadNum++));
175        thread.start();
176      }
177    }
178    
179    public void stop() {
180      thread = null;
181    }
182    
183    public void paint(Graphics g) {
184      g.setColor(color);
185      g.fillRect(x, y, w, h);
186      g.setColor(Color.white);
187      g.drawRect(x, y, w, h);
188    }
189    
190    public void checkBoundry() {
191      if(y>=rect.height) {
192        y=0;
193      }
194    }
195  }
196  
197  class  MyControlPanel extends  Panel{
198    Scrollbar  vf, wf, hf;
199    TextField  tvf, twf, thf;
200    Button  info;
201    List lst;
202    Sliders  parent;    
203    final int  start_width = 20;
204    final int  start_height = 10;
205    
206    MyControlPanel(Sliders parent)  {
207      this.parent  = parent;
208      vf = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,10);
209      wf = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,10);
210      hf = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,10);
211      tvf = new TextField("0", 5);
212      twf = new TextField("0", 5);
213      thf = new TextField("0", 5);
214      
215      lst  = new List(4, false);
216      lst.addItem("LightGray");
217      lst.addItem("DarkGray");
218      lst.addItem("Red");
219      lst.addItem("Green");
220      lst.addItem("Blue");
221      lst.addItem("Yellow");
222      lst.addItem("Magenta");
223      lst.addItem("Cyan");
224      lst.addItem("Pink");
225      lst.addItem("Orange");
226      
227      
228      GridBagLayout gridbag = new GridBagLayout();
229      GridBagConstraints c = new GridBagConstraints ();
230      setFont(new  Font("Helvetica", Font.BOLD, 12));
231      setLayout(gridbag);      
232      c.fill = GridBagConstraints.BOTH;
233      c.weightx = 1.0;      
234      c.gridwidth=1;
235      
236      make_label("           ",gridbag,c);
237      make_label("VELOCITY", gridbag,c);
238      make_scroll(vf,gridbag,c);
239      make_textedit(tvf,gridbag,c);
240      tvf.setText(String.valueOf(1));
241      make_label("WIDTH", gridbag,c);
242      make_scroll(wf,gridbag,c);
243      make_textedit(twf,gridbag,c);
244      twf.setText(String.valueOf(20));
245      make_label("HEIGHT",gridbag, c);
246      make_scroll(hf,gridbag,c);
247      make_textedit(thf,gridbag,c); 
248      thf.setText(String.valueOf(10));
249      make_label("COLOR", gridbag,c);
250      make_list(lst,gridbag,c);
251    }
252    
253    public void start() {
254      vf.setValue(1);
255      wf.setValue(1);
256      hf.setValue(1);
257      tvf.setText("1");		
258      twf.setText("20");
259      thf.setText("10");
260    }
261    public  void  paint(Graphics g)  {
262      Dimension   d = size();
263      g.setColor(new Color(0,0,0));
264      g.draw3DRect(0, 7,d.width-1,
265  		 d.height-10,false);
266    }
267    
268    protected  void  make_scroll(Scrollbar tempsc, GridBagLayout gridbag, 
269  			       GridBagConstraints c) {
270      c.weightx = 0.0;
271      c.gridwidth = GridBagConstraints.REMAINDER;
272      gridbag.setConstraints(tempsc,c);
273      add(tempsc);
274    }
275    
276    protected  void  make_list(List templist, GridBagLayout gridbag, 
277  			     GridBagConstraints c) {
278      c.weightx = 0.0;
279      c.gridwidth = GridBagConstraints.REMAINDER;
280      gridbag.setConstraints(templist,c);
281      add(templist);
282    }
283    
284    protected  void  make_label(String  name, GridBagLayout gridbag, 
285  			      GridBagConstraints c) {
286      Label label = new Label(name, Label.CENTER);
287      c.weightx = 0.0;
288      c.gridwidth = GridBagConstraints.REMAINDER;
289      gridbag.setConstraints(label,c);
290      add(label);
291    }
292    
293    protected  void  make_textedit(TextField temptex,
294  				 GridBagLayout gridbag, GridBagConstraints c) {
295      c.weightx = 1.0;
296      c.gridwidth = GridBagConstraints.REMAINDER;
297      gridbag.setConstraints(temptex,c);
298      add(temptex);
299    }
300    
301    protected  void  make_button(Button infobutton, GridBagLayout gridbag, 
302  			       GridBagConstraints c) {
303      c.gridwidth = GridBagConstraints.REMAINDER;
304      gridbag.setConstraints(infobutton,c);
305      add(infobutton);
306    }
307    
308    public Insets insets() {
309      return  new Insets(3,3,3,3);
310    }
311    
312    public  boolean  handleEvent(Event evt)  {
313      int  temp_velocity, temp_width, temp_height, temp_select;
314      if  (evt.target  instanceof Scrollbar)  {
315        if  ((Scrollbar) evt.target == vf) {
316  	temp_velocity = vf.getValue();
317  	tvf.setText(String.valueOf(temp_velocity));
318  	parent.MyCanvas.nbricks.change_velocity(temp_velocity);
319        }
320        if  ((Scrollbar) evt.target == wf) {
321  	temp_width = wf.getValue();  
322  	twf.setText(String.valueOf(temp_width*start_width));
323  	parent.MyCanvas.nbricks.change_width(temp_width);
324        }
325        if  ((Scrollbar) evt.target == hf) {
326  	temp_height = hf.getValue();
327  	thf.setText(String.valueOf(temp_height*start_height));
328  	parent.MyCanvas.nbricks.change_height(temp_height);
329        }
330        return true;
331      }
332      else if (evt.target  instanceof  List)  {
333        if  ((List) evt.target == lst) {
334  	temp_select = lst.getSelectedIndex();
335  	parent.MyCanvas.nbricks.change_color(temp_select);
336        }
337        return true;
338      }
339      else return false;
340    }
341  }