home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example-1.1 / PhilAnimator1_1.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  88 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. import java.awt.*;
  19.  
  20. // for dealing with the start stop button
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23.  
  24. // for dealing with the delay slider
  25. import java.awt.event.AdjustmentListener;
  26. import java.awt.event.AdjustmentEvent;
  27.  
  28. public class PhilAnimator1_1 extends java.applet.Applet
  29.     implements ActionListener, AdjustmentListener {
  30.  
  31.     Button stopStartButton = new Button("start");
  32.  
  33.         // delays can go from 500 to 10,000 (they get multiplied by 100 in Philosopher
  34.     Scrollbar grabDelaySlider = new Scrollbar(Scrollbar.HORIZONTAL, 5, 1, 0, 100);
  35.     Label label = new Label("  500 milliseconds");
  36.  
  37.     FramedArea framedArea;
  38.  
  39.     public void init() {
  40.         GridBagLayout gridBag = new GridBagLayout();
  41.         GridBagConstraints c = new GridBagConstraints();
  42.  
  43.         setLayout(gridBag);
  44.  
  45.         framedArea = new FramedArea(this);
  46.         c.fill = GridBagConstraints.BOTH;
  47.         c.weighty = 1.0;
  48.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  49.         gridBag.setConstraints(framedArea, c);
  50.         add(framedArea);
  51.  
  52.         c.fill = GridBagConstraints.HORIZONTAL;
  53.         c.weightx = 1.0;
  54.         c.weighty = 0.0;
  55.         gridBag.setConstraints(stopStartButton, c);
  56.         add(stopStartButton);
  57.  
  58.  
  59.         c.gridwidth = GridBagConstraints.RELATIVE; //don't end row
  60.         c.weightx = 1.0;
  61.         c.weighty = 0.0;
  62.         gridBag.setConstraints(grabDelaySlider, c);
  63.         add(grabDelaySlider);
  64.  
  65.         c.weightx = 0.0;
  66.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  67.         gridBag.setConstraints(label, c);
  68.         add(label);
  69.  
  70.         validate();
  71.     stopStartButton.addActionListener(this);
  72.     grabDelaySlider.addAdjustmentListener(this);
  73.     }
  74.  
  75.     public void actionPerformed(ActionEvent e) {
  76.         if (stopStartButton.getLabel().equals("stop/reset")) {
  77.             framedArea.stopButton();
  78.             stopStartButton.setLabel("start");
  79.         } else if (stopStartButton.getLabel().equals("start")) {
  80.             framedArea.startButton();
  81.             stopStartButton.setLabel("stop/reset");
  82.         }
  83.     }
  84.     public void adjustmentValueChanged(AdjustmentEvent e) {
  85.         label.setText(String.valueOf(100*grabDelaySlider.getValue()) + " milliseconds");
  86.     }
  87. }
  88.