home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / Applet1.java < prev    next >
Text File  |  1997-09-06  |  6KB  |  241 lines

  1. /*
  2.  *  Applet1.java
  3.  *
  4.  *  This file contains class Applet1 and class OnOffButton.
  5.  *
  6.  *  class Applet1 is an Applet that contains a ScrollingText display.
  7.  *  with the following controls, an OnOffButton that can pause and start
  8.  *  the display; a textfield that controls the text being displayed;
  9.  *  and 2 checkboxes that selects the position of the ScrollingText.
  10.  */
  11.  
  12. package samples.conversion;
  13.  
  14. import java.applet.*;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import symantec.itools.awt.*;
  18.  
  19. public class Applet1 extends Applet
  20. {
  21.     ScrollingText st;
  22.     OnOffButton button;
  23.     TextField textField;
  24.     Checkbox cb_center;
  25.     Checkbox cb_random;
  26.  
  27.     public void init()
  28.     {
  29.         setLayout( null );
  30.         setSize( 360, 420 );
  31.         setBackground( Color.lightGray );
  32.  
  33.        /*
  34.         *  add a ScrollingText object.
  35.         */
  36.  
  37.         st = new ScrollingText( "SYMANTEC.   Visual Cafe. ", 320, 240 );
  38.         add( st );
  39.         st.setLocation( 20, 20 );
  40.         st.start();
  41.  
  42.         Cursor handCursor = new Cursor( Cursor.HAND_CURSOR );
  43.  
  44.        /*
  45.         *  add a text field with corresponding label beside it.
  46.         */
  47.  
  48.         Label textLabel = new Label( "Text" );
  49.         textLabel.setFont( new Font( "Dialog", Font.PLAIN, 12 ) );
  50.         add( textLabel );
  51.         textLabel.setBounds( 20, 280, 30, 20 );
  52.  
  53.         textField = new TextField( st.getText() );
  54.         textField.setFont( new Font( "TimesRoman", Font.PLAIN, 12 ) );
  55.         add( textField );
  56.         textField.setBounds( 60, 280, 280, 20 );
  57.         textField.addTextListener( new TextAdapter() );
  58.  
  59.        /*
  60.         *  add checkboxes "Center" and "Random".
  61.         *  place them in a symantec.itools.awt.BorderPanel.
  62.         */
  63.  
  64.         BorderPanel borderPanel = new BorderPanel( "Position", BorderPanel.ALIGN_LEFT, BorderPanel.BEVEL_LOWERED );
  65.         borderPanel.setLayout( new FlowLayout() );
  66.         CheckboxGroup checkboxGrp = new CheckboxGroup();
  67.         cb_center = new Checkbox( "Center", checkboxGrp, false );
  68.         cb_random = new Checkbox( "Random", checkboxGrp, true );
  69.         cb_center.addItemListener( new ItemAdapter1() );
  70.         cb_random.addItemListener( new ItemAdapter2() );
  71.         cb_center.setCursor( handCursor );
  72.         cb_random.setCursor( handCursor );
  73.         borderPanel.add( cb_center );
  74.         borderPanel.add( cb_random );
  75.  
  76.         add( borderPanel );
  77.         borderPanel.setBounds( 20, 310, 180, 75 );
  78.  
  79.        /*
  80.         *  add an OnOffButton.
  81.         */
  82.  
  83.         button = new OnOffButton( OnOffButton.ON );
  84.         add( button );
  85.         button.setBounds( 220, 320, 120, 60 );
  86.         button.addActionListener( new Action() );
  87.         button.setCursor( handCursor );
  88.     }
  89.  
  90.     public void start()
  91.     {
  92.         repaint();
  93.     }
  94.  
  95.     public void stop()
  96.     {
  97.         st.stop();
  98.     }
  99.  
  100.     public void paint( Graphics g )
  101.     {
  102.        /*
  103.         *  draw borders for scrolling text display.
  104.         */
  105.  
  106.         g.setColor( Color.white );
  107.         g.drawRect( 20, 20, st.getSize().width + 2, st.getSize().height + 2 );
  108.         g.setColor( Color.darkGray );
  109.         g.drawRect( 19, 19, st.getSize().width + 2, st.getSize().height + 2 );
  110.     }
  111.  
  112.     class Action implements ActionListener
  113.     {
  114.        /*
  115.         *  inner class that handles the button's event.
  116.         */
  117.  
  118.         public void actionPerformed( ActionEvent e )
  119.         {
  120.             if( button.getState() == OnOffButton.ON )
  121.                 st.start();
  122.             else
  123.                 st.stop();
  124.         }
  125.     }
  126.  
  127.     class TextAdapter implements TextListener
  128.     {
  129.        /*
  130.         *  inner class that handles the text field's event.
  131.         */
  132.  
  133.         public void textValueChanged( TextEvent e )
  134.         {
  135.             st.stop();
  136.             button.setState( OnOffButton.OFF );
  137.             st.setText( textField.getText() );
  138.         }
  139.     }
  140.  
  141.     class ItemAdapter1 implements ItemListener
  142.     {
  143.        /*
  144.         *  inner class that handles the checkbox cb_center's event.
  145.         */
  146.  
  147.         public void itemStateChanged( ItemEvent e )
  148.         {
  149.             st.stop();
  150.             button.setState( OnOffButton.OFF );
  151.  
  152.             if( e.getStateChange() == ItemEvent.SELECTED )
  153.                 st.setPosition( ScrollingText.CENTER );
  154.         }
  155.     }
  156.  
  157.     class ItemAdapter2 implements ItemListener
  158.     {
  159.        /*
  160.         *  inner class that handles the checkbox cb_random's event.
  161.         */
  162.  
  163.         public void itemStateChanged( ItemEvent e )
  164.         {
  165.             st.stop();
  166.             button.setState( OnOffButton.OFF );
  167.  
  168.             if( e.getStateChange() == ItemEvent.SELECTED )
  169.                 st.setPosition( ScrollingText.RANDOM );
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. class OnOffButton extends Button
  177. {
  178.    /*
  179.     *  OnOffButton provides a Button that
  180.     *  is a switch between an on and an off state.
  181.     */
  182.  
  183.     final static boolean ON = true;
  184.     final static boolean OFF = false;
  185.     protected boolean state;
  186.     protected String onText = "Go";
  187.     protected String offText = "Stop!";
  188.  
  189.     OnOffButton( boolean s )
  190.     {
  191.         setFont( new Font( "Dialog", Font.PLAIN, 12 ) );
  192.         setBackground( Color.lightGray );
  193.         state = s;
  194.         if( state == ON )
  195.             setLabel( offText );
  196.         else
  197.             setLabel( onText );
  198.  
  199.         addActionListener( new Action() );
  200.         repaint();
  201.     }
  202.  
  203.     public boolean getState()
  204.     {
  205.         return state;
  206.     }
  207.  
  208.     public void setState( boolean s )
  209.     {
  210.         state = s;
  211.         if( state == ON )
  212.             setLabel( offText );
  213.         else
  214.             setLabel( onText );
  215.         repaint();
  216.     }
  217.  
  218.     class Action implements ActionListener
  219.     {
  220.         public void actionPerformed( ActionEvent e )
  221.         {
  222.             state = !state;
  223.             if( state == ON )
  224.                 setLabel( offText );
  225.             else
  226.                 setLabel( onText );
  227.             repaint();
  228.         }
  229.     }
  230.  
  231.     public void update( Graphics g )
  232.     {
  233.         paint( g );
  234.     }
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.