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   
Text File  |  1997-11-05  |  9KB  |  306 lines

  1. //------------------------------------------------------------------------------
  2. // BeanMachine 
  3. // Copyright IBM 1996, 1997
  4. //
  5. // Class: BlinkingText
  6. //
  7. // Description: 
  8. //  A sample class that illustrates how to write a Java class
  9. //  for the BeanMachine Palette.
  10. //
  11. //------------------------------------------------------------------------------
  12.  
  13. import java.awt.*;
  14. import java.util.StringTokenizer;
  15. import ibm.appauthor.*;
  16.  
  17. public class BlinkingText extends Canvas implements Runnable, IBMCustomListener
  18. {
  19.   // Each of these properties will appear in the Bean Wizard.
  20.   // They must be declared as private, and must have get and set methods.
  21.   private boolean     autoStart;
  22.   private String      text;
  23.   private Font        font;
  24.   private int         speed;   // 1 is slow, 10 is fast
  25.  
  26.   // these variables will not be used as properties
  27.   private   Thread      blinkThread = null;
  28.   static final int gap = 2;
  29.  
  30.   //--------------------------------------------------------------------------
  31.   // Constructor
  32.   // This method should not take any arguments, and should set all the
  33.   // default values for the properties.
  34.   //--------------------------------------------------------------------------
  35.   public BlinkingText()
  36.   {
  37.     setAutoStart(true);
  38.     setBackground(Color.white);
  39.     setForeground(Color.blue);
  40.     setSpeed(4);
  41.     setText("Blink");
  42.     setFont(new Font("TimesRoman", Font.PLAIN, 24));
  43.   }
  44.  
  45.   //------------------------------------------------------------------------------
  46.   // Get and Set methods for each of the properties.
  47.   // The Bean Wizard will mark text, font, and speed
  48.   // as properties because it finds these method names, which match the
  49.   // text, font, and speed instance variables.
  50.   //
  51.   // Since this class inherits foreground and background color variables, which
  52.   // also have get and set methods, foreground and background will also be marked
  53.   // as properties.
  54.   //
  55.   // Notice the use of repaint() and validateAll() in set methods to ensure
  56.   // that the bean behaves correctly at runtime when properties change due to
  57.   // connections.
  58.   //
  59.   // Marking the text property as this bean's default property in the Bean
  60.   // Wizard will enable direct editing of the bean in the Composer.
  61.   //------------------------------------------------------------------------------
  62.  
  63.   public boolean getAutoStart()
  64.   {
  65.     return autoStart;
  66.   }
  67.  
  68.   public void setAutoStart (boolean b)
  69.   {
  70.     autoStart = b;
  71.   }
  72.  
  73.   public void setBackground(Color c)
  74.   {
  75.     super.setBackground(c);
  76.     repaint();
  77.   }
  78.  
  79.   public void setForeground(Color c)
  80.   {
  81.     super.setForeground(c);
  82.     repaint();
  83.   }
  84.  
  85.   public Font getFont()
  86.   {
  87.     return font;
  88.   }
  89.  
  90.   public void setFont (Font f)
  91.   {
  92.     font = f;
  93.     validateAll();
  94.     repaint();
  95.   }
  96.  
  97.  
  98.   public String getText()
  99.   {
  100.     return text;
  101.   }
  102.  
  103.   public void setText (String s)
  104.   {
  105.     text = s;
  106.     validateAll();
  107.     repaint();
  108.   }
  109.  
  110.  
  111.   public int getSpeed()
  112.   {
  113.     return speed;
  114.   }
  115.  
  116.   public void setSpeed (int s)
  117.   {
  118.     speed = s;
  119.   }
  120.  
  121.   //------------------------------------------------------------------------------
  122.   // These methods tell BeanMachine what size the bean should be when it is
  123.   // first dropped in the Composer
  124.   //------------------------------------------------------------------------------
  125.  
  126.   public Dimension getPreferredSize()
  127.   {
  128.     FontMetrics fm = getFontMetrics(getFont());
  129.     Dimension d = new Dimension(fm.stringWidth(text) + (gap * 2),
  130.                                 fm.getHeight() + (gap * 2));
  131.     return(d);
  132.   }
  133.  
  134.   public Dimension getMinimumSize()
  135.   {
  136.     return getPreferredSize();
  137.   }
  138.  
  139.   //------------------------------------------------------------------------------
  140.   // These methods handle custom events of the IBMCustomListener interface.
  141.   // BeanMachine sends an autostart event to each bean, which we capture here
  142.   // to know to start the text blinking.
  143.   //------------------------------------------------------------------------------
  144.   public void autoStart(IBMCustomEvent evt)
  145.   {
  146.     if (autoStart)
  147.       start();
  148.   }
  149.   //----------------------------------------------------------------------------
  150.   public void autoStop(IBMCustomEvent evt)
  151.   {
  152.     stop();
  153.   }
  154.   //----------------------------------------------------------------------------
  155.   public void ended(IBMCustomEvent evt)
  156.   {
  157.   }
  158.   //----------------------------------------------------------------------------
  159.   public void errorOccurred(IBMCustomEvent evt)
  160.   {
  161.   }
  162.   //----------------------------------------------------------------------------
  163.   public void itemSelected(IBMCustomEvent evt)
  164.   {
  165.   }
  166.   //----------------------------------------------------------------------------
  167.   public void refreshed(IBMCustomEvent evt)
  168.   {
  169.   }
  170.   //----------------------------------------------------------------------------
  171.   public void startedOpened(IBMCustomEvent evt)
  172.   {
  173.   }
  174.   //----------------------------------------------------------------------------
  175.   public void stoppedClosed(IBMCustomEvent evt)
  176.   {
  177.   }
  178.   //----------------------------------------------------------------------------
  179.   public void timerAwake(IBMCustomEvent evt)
  180.   {
  181.   }
  182.   //----------------------------------------------------------------------------
  183.   public void timerElapsed(IBMCustomEvent evt)
  184.   {
  185.   }
  186.   //----------------------------------------------------------------------------
  187.   public void transitionEnded(IBMCustomEvent evt)
  188.   {
  189.   }
  190.  
  191.   //------------------------------------------------------------------------------
  192.   // This method draws the text. The blinking effect is caused by randomly 
  193.   // changing the color we use to draw the text, switching it between the
  194.   // background color and the value of the color property.
  195.   //------------------------------------------------------------------------------
  196.  
  197.   public void paint (Graphics g)
  198.   {
  199.     int x = gap;
  200.     int y = font.getSize() + gap;
  201.     int space;
  202.     Dimension d = getSize();
  203.  
  204.     g.setFont(font);
  205.     FontMetrics fm = g.getFontMetrics();
  206.     space = fm.stringWidth(" ");
  207.     for (StringTokenizer t = new StringTokenizer(text) ; t.hasMoreTokens() ; )
  208.     {
  209.       String word = t.nextToken();
  210.       int w = fm.stringWidth(word);
  211.       if (x + w > d.width)
  212.       {
  213.         x = gap;
  214.         y += (font.getSize() + gap);
  215.       }
  216.       if (blinkThread != null)
  217.       {
  218.         // Randomly decide whether to draw the text in the forground or background color
  219.         if (Math.random() < 0.5)
  220.         {
  221.           g.setColor(getForeground());
  222.         }
  223.         else
  224.         {
  225.           g.setColor(getBackground());
  226.         }
  227.       }
  228.       else  // when the thread is not running, draw in the foreground color
  229.         g.setColor(getForeground());
  230.  
  231.       g.drawString(word, x, y);
  232.       x += (w + space);
  233.     }
  234.   }
  235.  
  236.   //--------------------------------------------------------------------------
  237.   // It is up to you to make sure that your bean paints correctly at runtime.
  238.   // For example, if a property changes at runtime and that change affects the
  239.   // size and position of your bean, you must invalidate and validate the bean
  240.   // so that layout managers will re-layout themselves. The following method is
  241.   // the recommended way of doing this. It should be called in the set method
  242.   // of any property whose change affects the size and position of the bean.
  243.   //--------------------------------------------------------------------------
  244.   private void validateAll()
  245.   {
  246.     if (isValid())
  247.     {
  248.       Component self = this;
  249.       self.invalidate();
  250.       Component myParent = self.getParent();
  251.       if (myParent != null)
  252.       {
  253.         myParent.invalidate();
  254.         self = myParent;
  255.       }
  256.       self.validate();
  257.     }
  258.   }
  259.  
  260.   //--------------------------------------------------------------------------
  261.   // Method for running the thread
  262.   //--------------------------------------------------------------------------
  263.   public void run()
  264.   {
  265.     while (blinkThread != null)
  266.     {
  267.       repaint();
  268.       try
  269.       {
  270.         if (speed != 0)
  271.           Thread.sleep(1000 / speed);
  272.         else
  273.           Thread.sleep(400);
  274.       }
  275.       catch (InterruptedException e)
  276.       {
  277.         // do nothing
  278.       }
  279.     }
  280.   }
  281.  
  282.   //--------------------------------------------------------------------------
  283.   // Methods for starting, and stopping the thread.
  284.   // These two methods can be used as Actions in the Bean Wizard.
  285.   //--------------------------------------------------------------------------
  286.   public void start() 
  287.   {
  288.     if (blinkThread == null)
  289.     {
  290.       blinkThread = new Thread(this);
  291.       blinkThread.setPriority(Thread.MIN_PRIORITY);
  292.       blinkThread.start();
  293.     }
  294.   }
  295.     
  296.   public void stop()
  297.   {
  298.     if (blinkThread != null)
  299.     {
  300.       blinkThread.stop();
  301.       blinkThread = null;
  302.     }
  303.   }
  304.  
  305. }
  306.