org.opensourcephysics.media.core
Class VideoAdapter

java.lang.Object
  extended by org.opensourcephysics.media.core.VideoAdapter
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.util.EventListener, Drawable, Interactive, Measurable, DrawableImage, InteractiveImage, Playable, Trackable, Video
Direct Known Subclasses:
GifVideo, ImageVideo

public class VideoAdapter
extends java.lang.Object
implements Video, java.beans.PropertyChangeListener

This provides basic implementations of all Video methods. Subclasses should provide a raw image for display--see ImageVideo for an example.

Version:
1.0
Author:
Douglas Brown

Field Summary
protected  DoubleArray aspects
           
protected  java.awt.image.BufferedImage bufferedImage
           
protected  java.awt.image.Raster clearRaster
           
protected  ImageCoordSystem coords
           
protected  int endFrameNumber
           
protected  java.awt.image.BufferedImage filteredImage
           
protected  FilterStack filterStack
           
protected  int frameCount
           
protected  int frameNumber
           
protected  boolean isMeasured
           
protected  boolean isValidFilteredImage
           
protected  boolean isValidImage
           
protected  boolean isValidMeasure
           
protected  boolean looping
           
protected  double maxX
           
protected  double maxY
           
protected  double minX
           
protected  double minY
           
protected  boolean mouseEnabled
           
protected  boolean playing
           
protected  java.util.HashMap<java.lang.String,java.lang.Object> properties
           
protected  double rate
           
protected  java.awt.Image rawImage
           
protected  java.awt.Dimension size
           
protected  int startFrameNumber
           
protected  java.beans.PropertyChangeSupport support
           
protected  boolean visible
           
protected  boolean widthDominates
           
 
Constructor Summary
protected VideoAdapter()
          Protected constructor creates an empty VideoAdapter
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a PropertyChangeListener to this video.
 void addPropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
          Adds a PropertyChangeListener to this video.
 void back()
          Steps the video back one frame.
 void dispose()
          Disposes of this video.
 void draw(DrawingPanel panel, java.awt.Graphics g)
          Draws the video image on the panel.
protected  void finalize()
          Called by the garbage collector when this video is no longer in use.
 Interactive findInteractive(DrawingPanel panel, int xpix, int ypix)
          Returns this video if enabled.
protected  void findMinMaxValues()
          Finds the min and max values of x and y.
protected  void firePropertyChange(java.lang.String property, java.lang.Object oldVal, java.lang.Object newVal)
          Sends a PropertyChangeEvent to registered listeners.
 double getAngle()
          Gets the angle in radians of the curent video frame measured ccw from the world x-axis.
 ImageCoordSystem getCoords()
          Gets the image coordinate system.
 double getDuration()
          Gets the duration of the video.
 int getEndFrameNumber()
          Gets the end frame number.
 double getEndTime()
          Gets the end time in milliseconds.
 FilterStack getFilterStack()
          Gets the filter stack.
 int getFrameCount()
          Gets the total number of video frames.
 double getFrameDuration(int n)
          Gets the duration of the specified frame in milliseconds.
 int getFrameNumber()
          Gets the current video frame number.
 double getFrameTime(int n)
          Gets the start time of the specified frame in milliseconds.
 double getHeight()
          Gets the current height of the video frame.
 java.awt.image.BufferedImage getImage()
          Gets the current video image after applying enabled filters.
 java.lang.Object getProperty(java.lang.String name)
          Gets a user property of the video.
 java.util.Collection<java.lang.String> getPropertyNames()
          Gets a collection of user property names for the video.
 double getRate()
          Gets the relative play rate.
 double getRelativeAspect()
          Gets the relative aspect of the current video frame.
 int getStartFrameNumber()
          Gets the start frame number.
 double getStartTime()
          Gets the start time in milliseconds.
 double getTime()
          Gets the current video time in milliseconds.
 double getWidth()
          Gets the current width of the video frame.
 double getX()
          Gets x position of upper left corner of the current video frame in world units.
 double getXMax()
          Gets the maximum x needed to draw this object.
 double getXMin()
          Gets the minimum x needed to draw this object.
 double getY()
          Gets y position of upper left corner of the current video frame in world units.
 double getYMax()
          Gets the maximum y needed to draw this object.
 double getYMin()
          Gets the minimum y needed to draw this object.
 void goToEnd()
          Sets the frame number to the end frame.
 void goToStart()
          Sets the frame number to the start frame.
protected  void initialize()
          Initialize this video.
 boolean isEnabled()
          Gets whether this responds to mouse hits.
 boolean isLooping()
          Gets the looping state of the video.
 boolean isMeasured()
          Reports whether information is available to set min/max values.
 boolean isPlaying()
          Gets the playing state of this video.
 boolean isVisible()
          Gets the visibility of the video.
 void play()
          Plays the video at the current rate.
 void propertyChange(java.beans.PropertyChangeEvent e)
          Responds to property change events.
protected  void refreshBufferedImage()
          Refreshes the BufferedImage based on current size.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a PropertyChangeListener from this video.
 void removePropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
          Removes a PropertyChangeListener for a specified property.
 void reset()
          Stops the video and resets it to the start time.
 void setAngle(double theta)
          Sets the angle in radians of all video frames measured ccw from the world x-axis.
 void setCoords(ImageCoordSystem coords)
          Sets the image coordinate system used to convert from imagespace to worldspace.
 void setEnabled(boolean enabled)
          Sets whether this responds to mouse hits.
 void setEndFrameNumber(int n)
          Sets the end frame number.
 void setEndTime(double millis)
          Sets the end time in milliseconds.
 void setFilterStack(FilterStack stack)
          Sets the filter stack.
 void setFrameAngle(int n, double theta)
          Sets the angle in radians of the specified video frame measured ccw from the world x-axis.
 void setFrameHeight(int n, double height)
          Sets the height of the specified video frame in world units.
 void setFrameNumber(int n)
          Sets the video frame number.
 void setFrameRelativeAspect(int n, double relativeAspect)
          Sets the relative aspect of the specified video frame.
 void setFrameWidth(int n, double width)
          Sets the width of the specified video frame in world units.
 void setFrameX(int n, double x)
          Sets x position of upper left corner of the specified video frame in world units.
 void setFrameXY(int n, double x, double y)
          Sets the x and y position of the UL corner of the specified video frame in world units.
 void setFrameY(int n, double y)
          Sets y position of upper left corner of the specified video frame in world units.
 void setHeight(double height)
          Sets the height of all video frames in world units.
 void setLooping(boolean loops)
          Sets the looping state of this video.
 void setPlaying(boolean playing)
          Starts and stops the video.
 void setProperty(java.lang.String name, java.lang.Object value)
          Sets a user property of the video.
 void setRate(double rate)
          Sets the relative play rate.
 void setRelativeAspect(double relativeAspect)
          Sets the relative aspect of all video frames.
 void setStartFrameNumber(int n)
          Sets the start frame number.
 void setStartTime(double millis)
          Sets the start time in milliseconds.
 void setTime(double millis)
          Sets the video time in milliseconds.
 void setVisible(boolean visible)
          Shows or hides the video.
 void setWidth(double width)
          Sets the width of all video frames in world units.
 void setX(double x)
          Sets x position of upper left corner of all video frames in world units.
 void setXY(double x, double y)
          Sets the x and y position of the UL corner of all video frames in world units.
 void setY(double y)
          Sets y position of upper left corner of all video frames in world units.
 void step()
          Steps the video forward one frame.
 void stop()
          Stops the video.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rawImage

protected java.awt.Image rawImage

size

protected java.awt.Dimension size

bufferedImage

protected java.awt.image.BufferedImage bufferedImage

filteredImage

protected java.awt.image.BufferedImage filteredImage

frameCount

protected int frameCount

frameNumber

protected int frameNumber

startFrameNumber

protected int startFrameNumber

endFrameNumber

protected int endFrameNumber

rate

protected double rate

playing

protected boolean playing

looping

protected boolean looping

minX

protected double minX

maxX

protected double maxX

minY

protected double minY

maxY

protected double maxY

mouseEnabled

protected boolean mouseEnabled

visible

protected boolean visible

isMeasured

protected boolean isMeasured

isValidMeasure

protected boolean isValidMeasure

widthDominates

protected boolean widthDominates

isValidImage

protected boolean isValidImage

isValidFilteredImage

protected boolean isValidFilteredImage

coords

protected ImageCoordSystem coords

aspects

protected DoubleArray aspects

support

protected java.beans.PropertyChangeSupport support

properties

protected java.util.HashMap<java.lang.String,java.lang.Object> properties

filterStack

protected FilterStack filterStack

clearRaster

protected java.awt.image.Raster clearRaster
Constructor Detail

VideoAdapter

protected VideoAdapter()
Protected constructor creates an empty VideoAdapter

Method Detail

draw

public void draw(DrawingPanel panel,
                 java.awt.Graphics g)
Draws the video image on the panel.

Specified by:
draw in interface Drawable
Parameters:
panel - the drawing panel requesting the drawing
g - the graphics context on which to draw

setVisible

public void setVisible(boolean visible)
Shows or hides the video.

Specified by:
setVisible in interface DrawableImage
Parameters:
visible - true to show the video

isVisible

public boolean isVisible()
Gets the visibility of the video.

Specified by:
isVisible in interface DrawableImage
Returns:
true if the video is visible

getXMin

public double getXMin()
Gets the minimum x needed to draw this object.

Specified by:
getXMin in interface Measurable
Returns:
minimum x

getXMax

public double getXMax()
Gets the maximum x needed to draw this object.

Specified by:
getXMax in interface Measurable
Returns:
maximum x

getYMin

public double getYMin()
Gets the minimum y needed to draw this object.

Specified by:
getYMin in interface Measurable
Returns:
minimum y

getYMax

public double getYMax()
Gets the maximum y needed to draw this object.

Specified by:
getYMax in interface Measurable
Returns:
maximum y

isMeasured

public boolean isMeasured()
Reports whether information is available to set min/max values.

Specified by:
isMeasured in interface Measurable
Returns:
true if min/max values are valid

getImage

public java.awt.image.BufferedImage getImage()
Gets the current video image after applying enabled filters.

Specified by:
getImage in interface DrawableImage
Returns:
the current video image with filters applied

findInteractive

public Interactive findInteractive(DrawingPanel panel,
                                   int xpix,
                                   int ypix)
Returns this video if enabled.

Specified by:
findInteractive in interface Interactive
Parameters:
panel - the drawing panel
xpix - the x coordinate in pixels
ypix - the y coordinate in pixels
Returns:
this if enabled, otherwise null

setEnabled

public void setEnabled(boolean enabled)
Sets whether this responds to mouse hits.

Specified by:
setEnabled in interface Interactive
Parameters:
enabled - true if this responds to mouse hits.

isEnabled

public boolean isEnabled()
Gets whether this responds to mouse hits.

Specified by:
isEnabled in interface Interactive
Returns:
true if this responds to mouse hits.

setFrameX

public void setFrameX(int n,
                      double x)
Sets x position of upper left corner of the specified video frame in world units.

Specified by:
setFrameX in interface Video
Parameters:
n - the video frame number
x - the world x position

setX

public void setX(double x)
Sets x position of upper left corner of all video frames in world units.

Specified by:
setX in interface Interactive
Parameters:
x - the world x position

setFrameY

public void setFrameY(int n,
                      double y)
Sets y position of upper left corner of the specified video frame in world units.

Specified by:
setFrameY in interface Video
Parameters:
n - the video frame number
y - the world y position

setY

public void setY(double y)
Sets y position of upper left corner of all video frames in world units.

Specified by:
setY in interface Interactive
Parameters:
y - the world y position

getX

public double getX()
Gets x position of upper left corner of the current video frame in world units.

Specified by:
getX in interface Interactive
Returns:
the world x position

getY

public double getY()
Gets y position of upper left corner of the current video frame in world units.

Specified by:
getY in interface Interactive
Returns:
the world y position

setFrameXY

public void setFrameXY(int n,
                       double x,
                       double y)
Sets the x and y position of the UL corner of the specified video frame in world units.

Specified by:
setFrameXY in interface Video
Parameters:
n - the video frame number
x - the world x position
y - the world y position

setXY

public void setXY(double x,
                  double y)
Sets the x and y position of the UL corner of all video frames in world units.

Specified by:
setXY in interface Interactive
Parameters:
x - the world x position
y - the world y position

setFrameRelativeAspect

public void setFrameRelativeAspect(int n,
                                   double relativeAspect)
Sets the relative aspect of the specified video frame. Relative aspect is the ratio of the world aspect to the pixel aspect of the image. The pixel aspect is the ratio of image width to height in pixels, and world aspect is the ratio of world width to height in world units. For example, a 320 x 240 pixel movie has a pixel aspect of 1.33. If relative aspect is 2, then the world aspect will be 2.67. So if the video's width is 16 wu, its height will be 6 wu. Or if its height is 10 wu, its width will be 26.67 wu.

Specified by:
setFrameRelativeAspect in interface Video
Parameters:
n - the video frame number
relativeAspect - the desired relative aspect

setRelativeAspect

public void setRelativeAspect(double relativeAspect)
Sets the relative aspect of all video frames. Relative aspect is the ratio of the world aspect to the pixel aspect of the image. The pixel aspect is the ratio of image width to height in pixels, and world aspect is the ratio of world width to height in world units. For example, a 320 x 240 pixel movie has a pixel aspect of 1.33. If relative aspect is 2, then the world aspect will be 2.67. So if the video's width is 16 wu, its height will be 6 wu. Or if its height is 10 wu, its width will be 26.67 wu.

Specified by:
setRelativeAspect in interface InteractiveImage
Parameters:
relativeAspect - the desired relative aspect

getRelativeAspect

public double getRelativeAspect()
Gets the relative aspect of the current video frame.

Specified by:
getRelativeAspect in interface InteractiveImage
Returns:
the relative aspect of the current image.
See Also:
InteractiveImage.setRelativeAspect(double)

setFrameWidth

public void setFrameWidth(int n,
                          double width)
Sets the width of the specified video frame in world units. Also sets the height using the relative aspect.

Specified by:
setFrameWidth in interface Video
Parameters:
n - the video frame number
width - the width in world units
See Also:
setRelativeAspect(double)

setWidth

public void setWidth(double width)
Sets the width of all video frames in world units. Also sets the heights using the relative aspect.

Specified by:
setWidth in interface InteractiveImage
Parameters:
width - the width in world units
See Also:
setRelativeAspect(double)

getWidth

public double getWidth()
Gets the current width of the video frame.

Specified by:
getWidth in interface InteractiveImage
Returns:
the width of the video image

setFrameHeight

public void setFrameHeight(int n,
                           double height)
Sets the height of the specified video frame in world units. Also sets the width using the relative aspect.

Specified by:
setFrameHeight in interface Video
Parameters:
n - the video frame number
height - the height in world units
See Also:
setRelativeAspect(double)

setHeight

public void setHeight(double height)
Sets the height of all video frames in world units. Also sets the widths using the relative aspect.

Specified by:
setHeight in interface InteractiveImage
Parameters:
height - the height in world units
See Also:
setRelativeAspect(double)

getHeight

public double getHeight()
Gets the current height of the video frame.

Specified by:
getHeight in interface InteractiveImage
Returns:
the height of the video image

setFrameAngle

public void setFrameAngle(int n,
                          double theta)
Sets the angle in radians of the specified video frame measured ccw from the world x-axis. This results in a rotation only.

Specified by:
setFrameAngle in interface Video
Parameters:
n - the video frame number
theta - the angle in radians

setAngle

public void setAngle(double theta)
Sets the angle in radians of all video frames measured ccw from the world x-axis. This results in a rotation only.

Specified by:
setAngle in interface InteractiveImage
Parameters:
theta - the angle in radians

getAngle

public double getAngle()
Gets the angle in radians of the curent video frame measured ccw from the world x-axis.

Specified by:
getAngle in interface InteractiveImage
Returns:
the angle in radians

step

public void step()
Steps the video forward one frame.

Specified by:
step in interface Video

back

public void back()
Steps the video back one frame.

Specified by:
back in interface Video

getFrameCount

public int getFrameCount()
Gets the total number of video frames.

Specified by:
getFrameCount in interface Video
Returns:
the number of video frames

getFrameNumber

public int getFrameNumber()
Gets the current video frame number.

Specified by:
getFrameNumber in interface Video
Returns:
the current frame number

setFrameNumber

public void setFrameNumber(int n)
Sets the video frame number.

Specified by:
setFrameNumber in interface Video
Parameters:
n - the desired frame number
See Also:
Video.getStartFrameNumber(), Video.getEndFrameNumber()

getStartFrameNumber

public int getStartFrameNumber()
Gets the start frame number.

Specified by:
getStartFrameNumber in interface Video
Returns:
the start frame number
See Also:
getEndFrameNumber()

setStartFrameNumber

public void setStartFrameNumber(int n)
Sets the start frame number.

Specified by:
setStartFrameNumber in interface Video
Parameters:
n - the desired start frame number
See Also:
setEndFrameNumber(int)

getEndFrameNumber

public int getEndFrameNumber()
Gets the end frame number.

Specified by:
getEndFrameNumber in interface Video
Returns:
the end frame number
See Also:
getStartFrameNumber()

setEndFrameNumber

public void setEndFrameNumber(int n)
Sets the end frame number.

Specified by:
setEndFrameNumber in interface Video
Parameters:
n - the desired end frame number,
See Also:
setStartFrameNumber(int)

getFrameTime

public double getFrameTime(int n)
Gets the start time of the specified frame in milliseconds.

Specified by:
getFrameTime in interface Video
Parameters:
n - the frame number
Returns:
the start time of the frame in milliseconds, or -1 if not known

getFrameDuration

public double getFrameDuration(int n)
Gets the duration of the specified frame in milliseconds.

Specified by:
getFrameDuration in interface Video
Parameters:
n - the frame number
Returns:
the duration of the frame in milliseconds

play

public void play()
Plays the video at the current rate.

Specified by:
play in interface Playable

stop

public void stop()
Stops the video.

Specified by:
stop in interface Playable

reset

public void reset()
Stops the video and resets it to the start time.

Specified by:
reset in interface Playable

getTime

public double getTime()
Gets the current video time in milliseconds.

Specified by:
getTime in interface Playable
Returns:
the current time in milliseconds, or -1 if not known

setTime

public void setTime(double millis)
Sets the video time in milliseconds.

Specified by:
setTime in interface Playable
Parameters:
millis - the desired time in milliseconds

getStartTime

public double getStartTime()
Gets the start time in milliseconds.

Specified by:
getStartTime in interface Playable
Returns:
the start time in milliseconds, or -1 if not known

setStartTime

public void setStartTime(double millis)
Sets the start time in milliseconds. NOTE: the actual start time is normally set to the beginning of a frame.

Specified by:
setStartTime in interface Playable
Parameters:
millis - the desired start time in milliseconds

getEndTime

public double getEndTime()
Gets the end time in milliseconds.

Specified by:
getEndTime in interface Playable
Returns:
the end time in milliseconds, or -1 if not known

setEndTime

public void setEndTime(double millis)
Sets the end time in milliseconds. NOTE: the actual end time is set to the end of a frame.

Specified by:
setEndTime in interface Playable
Parameters:
millis - the desired end time in milliseconds

getDuration

public double getDuration()
Gets the duration of the video.

Specified by:
getDuration in interface Playable
Returns:
the duration of the video in milliseconds, or -1 if not known

goToStart

public void goToStart()
Sets the frame number to the start frame.

Specified by:
goToStart in interface Playable

goToEnd

public void goToEnd()
Sets the frame number to the end frame.

Specified by:
goToEnd in interface Playable

setPlaying

public void setPlaying(boolean playing)
Starts and stops the video.

Specified by:
setPlaying in interface Playable
Parameters:
playing - true starts the video, and false stops it

isPlaying

public boolean isPlaying()
Gets the playing state of this video.

Specified by:
isPlaying in interface Playable
Returns:
true if the video is playing

setLooping

public void setLooping(boolean loops)
Sets the looping state of this video. If true, the video restarts when reaching the end.

Specified by:
setLooping in interface Playable
Parameters:
loops - true if the video loops

isLooping

public boolean isLooping()
Gets the looping state of the video. If true, the video restarts when reaching the end.

Specified by:
isLooping in interface Playable
Returns:
true if the video loops

setRate

public void setRate(double rate)
Sets the relative play rate. Relative play rate is the ratio of a video's play rate to its preferred ("normal") play rate.

Specified by:
setRate in interface Playable
Parameters:
rate - the relative play rate.

getRate

public double getRate()
Gets the relative play rate. Relative play rate is the ratio of a video's play rate to its preferred ("normal") play rate.

Specified by:
getRate in interface Playable
Returns:
the relative play rate.

setCoords

public void setCoords(ImageCoordSystem coords)
Sets the image coordinate system used to convert from imagespace to worldspace.

Specified by:
setCoords in interface InteractiveImage
Parameters:
coords - the image coordinate system

getCoords

public ImageCoordSystem getCoords()
Gets the image coordinate system.

Specified by:
getCoords in interface InteractiveImage
Returns:
the image coordinate system

setFilterStack

public void setFilterStack(FilterStack stack)
Sets the filter stack.

Specified by:
setFilterStack in interface DrawableImage
Parameters:
stack - the new filter stack

getFilterStack

public FilterStack getFilterStack()
Gets the filter stack.

Specified by:
getFilterStack in interface DrawableImage
Returns:
the filter stack

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
Sets a user property of the video.

Specified by:
setProperty in interface DrawableImage
Parameters:
name - the name of the property
value - the value of the property

getProperty

public java.lang.Object getProperty(java.lang.String name)
Gets a user property of the video. May return null.

Specified by:
getProperty in interface DrawableImage
Parameters:
name - the name of the property
Returns:
the value of the property

getPropertyNames

public java.util.Collection<java.lang.String> getPropertyNames()
Gets a collection of user property names for the video.

Specified by:
getPropertyNames in interface DrawableImage
Returns:
a collection of property names

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to this video.

Specified by:
addPropertyChangeListener in interface Playable
Parameters:
listener - the object requesting property change notification

addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String property,
                                      java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to this video.

Specified by:
addPropertyChangeListener in interface Playable
Parameters:
property - the name of the property of interest to the listener
listener - the object requesting property change notification

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener from this video.

Specified by:
removePropertyChangeListener in interface Playable
Parameters:
listener - the listener requesting removal

removePropertyChangeListener

public void removePropertyChangeListener(java.lang.String property,
                                         java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener for a specified property.

Specified by:
removePropertyChangeListener in interface Playable
Parameters:
property - the name of the property
listener - the listener to remove

dispose

public void dispose()
Disposes of this video.

Specified by:
dispose in interface Video

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent e)
Responds to property change events. VideoAdapter listens for the following events: "transform" from ImageCoordSystem and "image" from FilterStack.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Parameters:
e - the property change event

firePropertyChange

protected void firePropertyChange(java.lang.String property,
                                  java.lang.Object oldVal,
                                  java.lang.Object newVal)
Sends a PropertyChangeEvent to registered listeners. No event is sent if oldVal and newVal are equal, unless they are both null.

Parameters:
property - the name of the property that has changed
oldVal - the value of the property before the change (may be null)
newVal - the value of the property after the change (may be null)

finalize

protected void finalize()
Called by the garbage collector when this video is no longer in use.

Overrides:
finalize in class java.lang.Object

initialize

protected void initialize()
Initialize this video.


refreshBufferedImage

protected void refreshBufferedImage()
Refreshes the BufferedImage based on current size. Creates a new image if needed.


findMinMaxValues

protected void findMinMaxValues()
Finds the min and max values of x and y.