home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 April A
/
Pcwk4a98.iso
/
Lotus
/
Beanmach
/
DATA1.CAB
/
Sample_Files
/
samples
/
newbeans
/
BlinkingText.java
< prev
Wrap
Text File
|
1997-11-05
|
9KB
|
306 lines
//------------------------------------------------------------------------------
// BeanMachine
// Copyright IBM 1996, 1997
//
// Class: BlinkingText
//
// Description:
// A sample class that illustrates how to write a Java class
// for the BeanMachine Palette.
//
//------------------------------------------------------------------------------
import java.awt.*;
import java.util.StringTokenizer;
import ibm.appauthor.*;
public class BlinkingText extends Canvas implements Runnable, IBMCustomListener
{
// Each of these properties will appear in the Bean Wizard.
// They must be declared as private, and must have get and set methods.
private boolean autoStart;
private String text;
private Font font;
private int speed; // 1 is slow, 10 is fast
// these variables will not be used as properties
private Thread blinkThread = null;
static final int gap = 2;
//--------------------------------------------------------------------------
// Constructor
// This method should not take any arguments, and should set all the
// default values for the properties.
//--------------------------------------------------------------------------
public BlinkingText()
{
setAutoStart(true);
setBackground(Color.white);
setForeground(Color.blue);
setSpeed(4);
setText("Blink");
setFont(new Font("TimesRoman", Font.PLAIN, 24));
}
//------------------------------------------------------------------------------
// Get and Set methods for each of the properties.
// The Bean Wizard will mark text, font, and speed
// as properties because it finds these method names, which match the
// text, font, and speed instance variables.
//
// Since this class inherits foreground and background color variables, which
// also have get and set methods, foreground and background will also be marked
// as properties.
//
// Notice the use of repaint() and validateAll() in set methods to ensure
// that the bean behaves correctly at runtime when properties change due to
// connections.
//
// Marking the text property as this bean's default property in the Bean
// Wizard will enable direct editing of the bean in the Composer.
//------------------------------------------------------------------------------
public boolean getAutoStart()
{
return autoStart;
}
public void setAutoStart (boolean b)
{
autoStart = b;
}
public void setBackground(Color c)
{
super.setBackground(c);
repaint();
}
public void setForeground(Color c)
{
super.setForeground(c);
repaint();
}
public Font getFont()
{
return font;
}
public void setFont (Font f)
{
font = f;
validateAll();
repaint();
}
public String getText()
{
return text;
}
public void setText (String s)
{
text = s;
validateAll();
repaint();
}
public int getSpeed()
{
return speed;
}
public void setSpeed (int s)
{
speed = s;
}
//------------------------------------------------------------------------------
// These methods tell BeanMachine what size the bean should be when it is
// first dropped in the Composer
//------------------------------------------------------------------------------
public Dimension getPreferredSize()
{
FontMetrics fm = getFontMetrics(getFont());
Dimension d = new Dimension(fm.stringWidth(text) + (gap * 2),
fm.getHeight() + (gap * 2));
return(d);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
//------------------------------------------------------------------------------
// These methods handle custom events of the IBMCustomListener interface.
// BeanMachine sends an autostart event to each bean, which we capture here
// to know to start the text blinking.
//------------------------------------------------------------------------------
public void autoStart(IBMCustomEvent evt)
{
if (autoStart)
start();
}
//----------------------------------------------------------------------------
public void autoStop(IBMCustomEvent evt)
{
stop();
}
//----------------------------------------------------------------------------
public void ended(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void errorOccurred(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void itemSelected(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void refreshed(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void startedOpened(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void stoppedClosed(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void timerAwake(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void timerElapsed(IBMCustomEvent evt)
{
}
//----------------------------------------------------------------------------
public void transitionEnded(IBMCustomEvent evt)
{
}
//------------------------------------------------------------------------------
// This method draws the text. The blinking effect is caused by randomly
// changing the color we use to draw the text, switching it between the
// background color and the value of the color property.
//------------------------------------------------------------------------------
public void paint (Graphics g)
{
int x = gap;
int y = font.getSize() + gap;
int space;
Dimension d = getSize();
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
space = fm.stringWidth(" ");
for (StringTokenizer t = new StringTokenizer(text) ; t.hasMoreTokens() ; )
{
String word = t.nextToken();
int w = fm.stringWidth(word);
if (x + w > d.width)
{
x = gap;
y += (font.getSize() + gap);
}
if (blinkThread != null)
{
// Randomly decide whether to draw the text in the forground or background color
if (Math.random() < 0.5)
{
g.setColor(getForeground());
}
else
{
g.setColor(getBackground());
}
}
else // when the thread is not running, draw in the foreground color
g.setColor(getForeground());
g.drawString(word, x, y);
x += (w + space);
}
}
//--------------------------------------------------------------------------
// It is up to you to make sure that your bean paints correctly at runtime.
// For example, if a property changes at runtime and that change affects the
// size and position of your bean, you must invalidate and validate the bean
// so that layout managers will re-layout themselves. The following method is
// the recommended way of doing this. It should be called in the set method
// of any property whose change affects the size and position of the bean.
//--------------------------------------------------------------------------
private void validateAll()
{
if (isValid())
{
Component self = this;
self.invalidate();
Component myParent = self.getParent();
if (myParent != null)
{
myParent.invalidate();
self = myParent;
}
self.validate();
}
}
//--------------------------------------------------------------------------
// Method for running the thread
//--------------------------------------------------------------------------
public void run()
{
while (blinkThread != null)
{
repaint();
try
{
if (speed != 0)
Thread.sleep(1000 / speed);
else
Thread.sleep(400);
}
catch (InterruptedException e)
{
// do nothing
}
}
}
//--------------------------------------------------------------------------
// Methods for starting, and stopping the thread.
// These two methods can be used as Actions in the Bean Wizard.
//--------------------------------------------------------------------------
public void start()
{
if (blinkThread == null)
{
blinkThread = new Thread(this);
blinkThread.setPriority(Thread.MIN_PRIORITY);
blinkThread.start();
}
}
public void stop()
{
if (blinkThread != null)
{
blinkThread.stop();
blinkThread = null;
}
}
}