home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
Applet1.java
< prev
next >
Wrap
Text File
|
1997-09-06
|
6KB
|
241 lines
/*
* Applet1.java
*
* This file contains class Applet1 and class OnOffButton.
*
* class Applet1 is an Applet that contains a ScrollingText display.
* with the following controls, an OnOffButton that can pause and start
* the display; a textfield that controls the text being displayed;
* and 2 checkboxes that selects the position of the ScrollingText.
*/
package samples.conversion;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import symantec.itools.awt.*;
public class Applet1 extends Applet
{
ScrollingText st;
OnOffButton button;
TextField textField;
Checkbox cb_center;
Checkbox cb_random;
public void init()
{
setLayout( null );
setSize( 360, 420 );
setBackground( Color.lightGray );
/*
* add a ScrollingText object.
*/
st = new ScrollingText( "SYMANTEC. Visual Cafe. ", 320, 240 );
add( st );
st.setLocation( 20, 20 );
st.start();
Cursor handCursor = new Cursor( Cursor.HAND_CURSOR );
/*
* add a text field with corresponding label beside it.
*/
Label textLabel = new Label( "Text" );
textLabel.setFont( new Font( "Dialog", Font.PLAIN, 12 ) );
add( textLabel );
textLabel.setBounds( 20, 280, 30, 20 );
textField = new TextField( st.getText() );
textField.setFont( new Font( "TimesRoman", Font.PLAIN, 12 ) );
add( textField );
textField.setBounds( 60, 280, 280, 20 );
textField.addTextListener( new TextAdapter() );
/*
* add checkboxes "Center" and "Random".
* place them in a symantec.itools.awt.BorderPanel.
*/
BorderPanel borderPanel = new BorderPanel( "Position", BorderPanel.ALIGN_LEFT, BorderPanel.BEVEL_LOWERED );
borderPanel.setLayout( new FlowLayout() );
CheckboxGroup checkboxGrp = new CheckboxGroup();
cb_center = new Checkbox( "Center", checkboxGrp, false );
cb_random = new Checkbox( "Random", checkboxGrp, true );
cb_center.addItemListener( new ItemAdapter1() );
cb_random.addItemListener( new ItemAdapter2() );
cb_center.setCursor( handCursor );
cb_random.setCursor( handCursor );
borderPanel.add( cb_center );
borderPanel.add( cb_random );
add( borderPanel );
borderPanel.setBounds( 20, 310, 180, 75 );
/*
* add an OnOffButton.
*/
button = new OnOffButton( OnOffButton.ON );
add( button );
button.setBounds( 220, 320, 120, 60 );
button.addActionListener( new Action() );
button.setCursor( handCursor );
}
public void start()
{
repaint();
}
public void stop()
{
st.stop();
}
public void paint( Graphics g )
{
/*
* draw borders for scrolling text display.
*/
g.setColor( Color.white );
g.drawRect( 20, 20, st.getSize().width + 2, st.getSize().height + 2 );
g.setColor( Color.darkGray );
g.drawRect( 19, 19, st.getSize().width + 2, st.getSize().height + 2 );
}
class Action implements ActionListener
{
/*
* inner class that handles the button's event.
*/
public void actionPerformed( ActionEvent e )
{
if( button.getState() == OnOffButton.ON )
st.start();
else
st.stop();
}
}
class TextAdapter implements TextListener
{
/*
* inner class that handles the text field's event.
*/
public void textValueChanged( TextEvent e )
{
st.stop();
button.setState( OnOffButton.OFF );
st.setText( textField.getText() );
}
}
class ItemAdapter1 implements ItemListener
{
/*
* inner class that handles the checkbox cb_center's event.
*/
public void itemStateChanged( ItemEvent e )
{
st.stop();
button.setState( OnOffButton.OFF );
if( e.getStateChange() == ItemEvent.SELECTED )
st.setPosition( ScrollingText.CENTER );
}
}
class ItemAdapter2 implements ItemListener
{
/*
* inner class that handles the checkbox cb_random's event.
*/
public void itemStateChanged( ItemEvent e )
{
st.stop();
button.setState( OnOffButton.OFF );
if( e.getStateChange() == ItemEvent.SELECTED )
st.setPosition( ScrollingText.RANDOM );
}
}
}
class OnOffButton extends Button
{
/*
* OnOffButton provides a Button that
* is a switch between an on and an off state.
*/
final static boolean ON = true;
final static boolean OFF = false;
protected boolean state;
protected String onText = "Go";
protected String offText = "Stop!";
OnOffButton( boolean s )
{
setFont( new Font( "Dialog", Font.PLAIN, 12 ) );
setBackground( Color.lightGray );
state = s;
if( state == ON )
setLabel( offText );
else
setLabel( onText );
addActionListener( new Action() );
repaint();
}
public boolean getState()
{
return state;
}
public void setState( boolean s )
{
state = s;
if( state == ON )
setLabel( offText );
else
setLabel( onText );
repaint();
}
class Action implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
state = !state;
if( state == ON )
setLabel( offText );
else
setLabel( onText );
repaint();
}
}
public void update( Graphics g )
{
paint( g );
}
}