home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
Source.bin
/
NervousText.java
< prev
next >
Wrap
Text File
|
1998-03-18
|
9KB
|
336 lines
package symantec.itools.multimedia;
import java.awt.Canvas;
import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.beans.PropertyChangeListener;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
// 01/29/97 TWB Integrated changes from Macintosh
// 05/31/97 RKM Updated to support Java 1.1
// Made properties bound & constrained
// 07/15/97 CAR marked fields transient as needed
// 07/15/97 CAR added preferredSize
/**
* NervousText control.
* Creates animated text in which each letter moves independently of all other letters.
*
* @version 1.0, Nov 26, 1996
* @author Symantec
*/
public class NervousText
extends Canvas
implements Runnable
{
char separated[];
String text;
boolean paused;
transient Thread jitter = null;
/**
* Create defalut NervousText object. Default object contains the word "text".
*/
public NervousText()
{
paused = false;
try
{
setText("text");
}
catch (PropertyVetoException veto) {}
}
//
// Properties
//
/**
* Specify the text that will be displayed by NervousText.
*
* @param str text to be shown as nervous text
*
* @exception PropertyVetoException
* if the specified property value is unacceptable
*/
public void setText(String newText)
throws PropertyVetoException
{
if (!symantec.itools.util.GeneralUtils.objectsEqual(text,newText))
{
String oldText = text;
vetos.fireVetoableChange("Text", oldText, newText);
text = newText;
changes.firePropertyChange("Text", oldText, newText);
//Extract String into char[] (handle null String)
int length = text != null ? text.length() : 0;
separated = new char[length];
if (text != null)
text.getChars(0, length, separated, 0);
}
}
/**
* Obtain the text that is currently being displayed.
*
* @return text that is being shown by this component
*/
public String getText()
{
return text;
}
/**
* Temporarily suspend the animation of the nervous text. Identical
* to pause(boolean).
* @param f pause the animation of true
*
* @exception PropertyVetoException
* if the specified property value is unacceptable
*/
public void setPaused(boolean newIsPaused)
throws PropertyVetoException
{
if (paused != newIsPaused)
{
Boolean oldPausedBool = new Boolean(paused);
Boolean newPausedBool = new Boolean(newIsPaused);
vetos.fireVetoableChange("Paused", oldPausedBool, newPausedBool);
paused = newIsPaused;
changes.firePropertyChange("Paused", oldPausedBool, newPausedBool);
}
}
/**
* Obtain animator's current state.
*
* @return true if the animator is paused, false if it is running
*/
public boolean isPaused()
{
return paused;
}
//
// Methods
//
/**
* @deprecate
* @see setPaused
*/
public void pause(boolean f)
{
//!!!RKM!!! Silent catch veto and deprecate the method
try
{
setPaused(f);
}
catch (PropertyVetoException veto) {}
}
/**
* NervousText thread body. This method is called by the Java virtual
* machine to when this thread starts.
*/
public void run()
{
while (true)
{
try
{
Thread.sleep(150);
}
catch(Exception e)
{
}
if (!paused)
{
repaint();
}
}
}
/**
* Tells this component that it has been added to a container.
* This is a standard Java AWT method which gets called by the AWT when
* this component is added to a container. Typically, it is used to
* create this component's peer.
*
* It has been overridden here to start the nervous text thread.
*
* @see #removeNotify
*/
public void addNotify()
{
super.addNotify();
jitter = new Thread(this);
jitter.start();
}
/**
* Tells this component that it is being removed from a container.
* This is a standard Java AWT method which gets called by the AWT when
* this component is removed from a container. Typically, it is used to
* destroy the peers of this component and all its subcomponents.
*
* It has been overridden here to stop the nervous text thread.
*
* @see #addNotify
*/
public synchronized void removeNotify() {
if (jitter != null)
{
jitter.stop();
jitter = null;
}
super.removeNotify();
}
/**
* Makes this component visible.
* This is a standard Java AWT method which gets called to show this
* component. If this component was invisible due to a previous hide()
* call it make this component visible again.
*
* @see #hide
*/
public synchronized void show() {
super.show();
if (isVisible()) {
if (jitter != null)
jitter.resume();
}
}
/**
* Makes this component invisible.
* This is a standard Java AWT method which gets called to hide
* this component. A hidden component cannot be seen by the user nor
* does it take up space in its container, but it does continue to
* exist.
*
* @see #show
*/
public synchronized void hide() {
super.hide();
if (!isVisible()) {
if (jitter != null)
jitter.suspend();
}
}
/**
* Paints this component using the given graphics context.
* This is a standard Java AWT method which typically gets called
* by the AWT to handle painting this component. It paints this component
* using the given graphics context. The graphics context clipping region
* is set to the bounding rectangle of this component and its <0,0>
* coordinate is this component's top-left corner.
*
* @param g the graphics context used for painting
* @see java.awt.Component#repaint
* @see java.awt.Component#update
*/
public void paint(java.awt.Graphics g)
{
for(int i = 0;i < text.length(); i++)
{
int x_coord = (int) (Math.random() * 10 + 20 * i);
int y_coord = (int) (Math.random() * 10 + 36);
g.drawChars(separated, i, 1, x_coord, y_coord);
}
}
/**
* Returns the recommended dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the recommended size of this component.
*
*/
public Dimension getPreferredSize()
{
Dimension dim = size();
Dimension min = getMinimumSize();
return new Dimension(Math.max(dim.width, min.width), Math.max(dim.height, min.height));
}
/**
* Returns the minimum dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the recommended size of this component.
*
* @return 10x10 per letter
*
*/
public Dimension getMinimumSize()
{
return new Dimension(separated.length * 10, separated.length * 10);
}
/**
* Adds a listener for all event changes.
* @param PropertyChangeListener listener the listener to add.
* @see #removePropertyChangeListener
*/
public void addPropertyChangeListener(PropertyChangeListener listener)
{
//super.addPropertyChangeListener(listener);
changes.addPropertyChangeListener(listener);
}
/**
* Removes a listener for all event changes.
* @param PropertyChangeListener listener the listener to remove.
* @see #addPropertyChangeListener
*/
public void removePropertyChangeListener(PropertyChangeListener listener)
{
//super.removePropertyChangeListener(listener);
changes.removePropertyChangeListener(listener);
}
/**
* Adds a vetoable listener for all event changes.
* @param VetoableChangeListener listener the listener to add.
* @see #removeVetoableChangeListener
*/
public void addVetoableChangeListener(VetoableChangeListener listener)
{
//super.addVetoableChangeListener(listener);
vetos.addVetoableChangeListener(listener);
}
/**
* Removes a vetoable listener for all event changes.
* @param VetoableChangeListener listener the listener to remove.
* @see #addVetoableChangeListener
*/
public void removeVetoableChangeListener(VetoableChangeListener listener)
{
//super.removeVetoableChangeListener(listener);
vetos.removeVetoableChangeListener(listener);
}
// Private members
private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
}