home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 7.7 KB | 250 lines |
- package symantec.itools.multimedia;
-
-
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- 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
-
- /**
- * MovingAnimation component. Draws a series of images at sequential horizontal
- * positions within the component area. Each frame is drawn at a given offset
- * from the previous frame.
- *
- * @see Animator
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class MovingAnimation
- extends Animator
- implements Runnable
- {
- /**
- * Index of the image to draw. A loop counter.
- */
- transient protected int loopslot;
- /**
- * The amount each image is shifted horizontally from the previous image.
- * The offset may be positive
- * or negative. If the offset is positive, the first image is drawn on the
- * left side of the component and subsequent images are drawn to the right
- * of the first image by offset pixels. If the offset is negative, the
- * first image is drawn at the right side of the component area.
- */
- protected int shiftOffset;
-
- /**
- * Constructs a default MovingAnimation.
- */
- public MovingAnimation()
- {
- shiftOffset = 10;
-
- loopslot = 0;
- curXOffset = 0;
- forever = true;
-
- vetos = new symantec.itools.beans.VetoableChangeSupport(this);
- changes = new symantec.itools.beans.PropertyChangeSupport(this);
- }
-
- /**
- * Sets the animation shift offset. Each image is shifted horizontally
- * from the previous image by the specified offset. Offset may be positive
- * or negative. If the offset is positive, the first image is drawn on the
- * left side of the component and subsequent images are drawn to the right
- * of the first image by offset pixels. If the offset is negative, the
- * first image is drawn at the right side of the component area.
- * @param newShiftOffset shift offset amount, in pixels
- * @see #getShiftOffset
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setShiftOffset(int newShiftOffset)
- throws PropertyVetoException
- {
- if (shiftOffset != newShiftOffset)
- {
- Integer oldShiftOffsetInt = new Integer(shiftOffset);
- Integer newShiftOffsetInt = new Integer(newShiftOffset);
-
- vetos.fireVetoableChange("ShiftOffset",oldShiftOffsetInt,newShiftOffsetInt);
-
- shiftOffset = newShiftOffset;
-
- changes.firePropertyChange("ShiftOffset",oldShiftOffsetInt,newShiftOffsetInt);
- }
- }
-
- /**
- * Gets the animation shift offset.
- * @return int current shift offset amount, in pixels
- * @see #setShiftOffset
- */
- public int getShiftOffset()
- {
- return shiftOffset;
- }
-
- /**
- * Body of MovingAnimation Thread. This method is called by the Java Virtual Machine
- * in response to a call to the start method of this object.
- */
- public void run()
- {
- Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
-
- Dimension d = size();
-
- int imageCount = images.size();
-
- if (imageCount > 1)
- {
- if (shiftOffset < 0)
- curXOffset = d.width - maxWidth;
-
- int count = 0;
-
- while (true)
- {
- d = size(); //Needed if the component resizes while running.
-
- if (++loopslot >= imageCount)
- {
- if (++count > numLoops && !forever)
- break;
-
- loopslot = 0;
- curXOffset += shiftOffset;
-
- if (curXOffset < 0)
- curXOffset = d.width - maxWidth;
-
- else if (curXOffset + maxWidth > d.width)
- curXOffset = 0;
- }
-
- repaint();
-
- try
- {
- Thread.sleep(delay);
- }
- catch (InterruptedException e)
- {
- break;
- }
- }
- }
- }
-
- /**
- * Incrementally updates an image as image data becomes available.
- * This is a standard Java AWT method which gets called by the AWT to
- * incrementally draw an image as more image data becomes available.
- * @param img the image being drawn
- * @param flags image update flags (see class ImageObserver)
- * @param x horizontal position
- * @param y vertical position
- * @param w width
- * @param h height
- *
- * @return true if image has been completely loaded
- *
- * @see java.awt.image.ImageObserver
- * @see java.awt.image.ImageObserver#imageUpdate
- */
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
- {
- if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0)
- if ((images != null) && (loopslot < images.size()) && (((AnimatorImage)images.elementAt(loopslot)).image == img))
- repaint(100);
-
- return (flags & (ALLBITS|ERROR)) == 0;
- }
-
- /**
- * 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.
- *
- * The horizontal position of the image is shifted according to the current offset.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- if ((images != null) && (loopslot < images.size()))
- {
- Image img = ((AnimatorImage)images.elementAt(loopslot)).image;
- if (img != null)
- g.drawImage(img, curXOffset, 0, this);
- }
- }
-
- /**
- * 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
- transient private int curXOffset;
- private symantec.itools.beans.VetoableChangeSupport vetos;
- private symantec.itools.beans.PropertyChangeSupport changes;
- }
-