the design and implementation of multimedia software

93
Chapter 8 Sampled Dynamic Visual Content The Design and Implementation of Multimedia Software David Bernstein Jones and Bartlett Publishers www.jbpub.com David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 1 / 85

Upload: others

Post on 16-Nov-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Design and Implementation of Multimedia Software

Chapter 8Sampled Dynamic Visual Content

The Design and Implementation ofMultimedia Software

David Bernstein

Jones and Bartlett Publishers

www.jbpub.com

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 1 / 85

Page 2: The Design and Implementation of Multimedia Software

Introduction

About this Chapter

• Dynamic visual content is any visual content that changes overtime.

• Our interest is with visual content that changes over time in a waythat causes the perception of apparent motion.

• The sampling of dynamic visual content involves sampling from allpossible points in time.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 2 / 85

Page 3: The Design and Implementation of Multimedia Software

Introduction

Sampling from a Visual Stream

Visual

“Stream” {Time

“Frames”

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 3 / 85

Page 4: The Design and Implementation of Multimedia Software

Introduction

A Comparison

• In sampled static visual content each pixel is treated as atomic.

• In sampled dynamic visual content each frame is atomic.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 4 / 85

Page 5: The Design and Implementation of Multimedia Software

Introduction

Levels of Abstraction/Resolution

• At a lower level of abstraction, each frame actually consists ofstatic visual content, and that content can be either sampled ordescribed.

• At the current (higher) level of abstraction, the nature of eachframe is of no consequence.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 5 / 85

Page 6: The Design and Implementation of Multimedia Software

A ‘Quick Start’

What’s Next

We need some instant gratification.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 6 / 85

Page 7: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Requirements

Our goal is to create a system that behaves like a flip book forSimpleContent objects. That is, the system must:

F8.1 Render a sequence of SimpleContent objects over time.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 7 / 85

Page 8: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Satisfying this Requirement

• What We Have:

An object that manages the collection of SimpleContent objects.

A process that controls the rendering of the objects in the sequence.

A GUI component that presents the SimpleContent objects.

• What We Need:

An ‘enhanced’ Visualization object to manage the collection offrames (which are SimpleContent objects).

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 8 / 85

Page 9: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 1

• Approach:

Add code to the Visualization class.

• Shortcomings:

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 9 / 85

Page 10: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 1

• Approach:

Add code to the Visualization class.

• Shortcomings:

It makes this class more complicated and more difficult tounderstand for those that have no interest in dynamic content.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 9 / 85

Page 11: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 2

• Approach:

Use the decorator pattern.

• Shortcomings:

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 10 / 85

Page 12: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 2

• Approach:

Use the decorator pattern.

• Shortcomings:

It is hard to imagine a situation in which, at run time, one wouldwant to add these kinds of capabilities to a Visualization object.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 10 / 85

Page 13: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 3

Create a specialization of the Visualization class (called the Screenclass) that includes the attributes and methods needed to manage andpresent dynamic content.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 11 / 85

Page 14: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 3.1

• Approach:

Have the Screen class iterate through the collection ofSimpleContent objects sending each a render() message in turn.

• Shortcomings:

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 12 / 85

Page 15: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 3.1

• Approach:

Have the Screen class iterate through the collection ofSimpleContent objects sending each a render() message in turn.

• Shortcomings:

It does not provide control over the amount of time eachSimpleContent object is visible.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 12 / 85

Page 16: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Alternative 3.2

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 13 / 85

Page 17: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen - Demonstrations

In examples/chapter:java -cp multimedia2.jar:examples.jar ScreenDemo solidclock

java -cp multimedia2.jar:examples.jar ScreenDemo scribble

java -cp multimedia2.jar:examples.jar ScreenDemo movingrectangle

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 14 / 85

Page 18: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – Structure

package visual.dynamic.sampled;

import java.util.*;

import collectionframework.*;

import event.*;

import visual.*;

import visual.statik.SimpleContent;

public class Screen extends Visualization

implements MetronomeListener

{

private boolean repeating;

private int frameNumber;

private Iterator<SimpleContent> frames;

protected Metronome metronome;

protected SimpleContent currentFrame;

public static final int DEFAULT_FRAME_DELAY = 42;

public Screen()

{

this(DEFAULT_FRAME_DELAY);

}

public Screen(int frameRate)

{

this(new Metronome((int)(1000.0 / frameRate)));

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 15 / 85

Page 19: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – Structure (cont.)

}

public Screen(Metronome metronome)

{

super();

this.metronome = metronome;

metronome.addListener(this);

setRepeating(false);

}

public void start()

{

reset();

if (frames != null) metronome.start();

}

public void stop()

{

metronome.stop();

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 16 / 85

Page 20: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – Setters

public void setRepeating(boolean repeating)

{

this.repeating = repeating;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 17 / 85

Page 21: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – getFrameNumber()

public int getFrameNumber()

{

return frameNumber;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 18 / 85

Page 22: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – Initiating Rendering

public void handleTick(int time)

{

if (frames != null)

{

// See if we’re done

if (frameNumber < 0)

{

if (repeating) reset();

else stop();

}

// Start the rendering process (i.e., request that the

// paint() method be called)

repaint();

// Advance the frame

advanceFrame();

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 19 / 85

Page 23: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – Advancing the Frame

private void advanceFrame()

{

if ((frames != null) && (frames.hasNext()))

{

currentFrame = frames.next();

frameNumber++;

}

else

{

currentFrame = null;

frameNumber = -1;

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 20 / 85

Page 24: The Design and Implementation of Multimedia Software

A ‘Quick Start’

An Important Modification

The Screen class modifies the behavior of its parent Visualizationclass in one important way – its iterator() method does not containall of the frames, it only contains the current frame.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 21 / 85

Page 25: The Design and Implementation of Multimedia Software

A ‘Quick Start’

Screen – The Current Frame

protected NullIterator<SimpleContent> currentFrameIterator;

currentFrameIterator = new NullIterator<SimpleContent>();

public Iterator<SimpleContent> iterator()

{

currentFrameIterator.setElement(currentFrame);

if (frameNumber < 0) currentFrameIterator.clear();

return currentFrameIterator;

}

public Iterator<SimpleContent> iterator(boolean all)

{

Iterator<SimpleContent> result;

if (all) result = super.iterator();

else result = iterator();

return result;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 22 / 85

Page 26: The Design and Implementation of Multimedia Software

A ‘Quick Start’

The Process of Rendering Sampled Dynamic Content

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 23 / 85

Page 27: The Design and Implementation of Multimedia Software

Encapsulating Sampled Dynamic Content

What’s Next

We need to consider the encapsulation of sampled dynamic content.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 24 / 85

Page 28: The Design and Implementation of Multimedia Software

Encapsulating Sampled Dynamic Content

The Collection

• Desirable Properties:

What are they?

• A Good Choice:

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 25 / 85

Page 29: The Design and Implementation of Multimedia Software

Encapsulating Sampled Dynamic Content

The Collection

• Desirable Properties:

Can return either an associated Iterator or an associatedListIterator (that supports bi-directional traversal).

Thread safe.

• A Good Choice:

Any thoughts?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 25 / 85

Page 30: The Design and Implementation of Multimedia Software

Encapsulating Sampled Dynamic Content

The Collection

• Desirable Properties:

Can return either an associated Iterator or an associatedListIterator (that supports bi-directional traversal).

Thread safe.

• A Good Choice:

CopyOnWriteArrayList

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 25 / 85

Page 31: The Design and Implementation of Multimedia Software

Rendering Individual Frames

What’s Next

We need to consider the rendering of individual frames.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 26 / 85

Page 32: The Design and Implementation of Multimedia Software

Rendering Individual Frames

A Small Problem

• You Might Think:

Since sampled.Content objects know how to render themselvesthere is no reason to discuss the rendering of individual frames.

• However:

When rendered in quick succession, the frames have a tendency to“flicker”.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 27 / 85

Page 33: The Design and Implementation of Multimedia Software

Rendering Individual Frames

Double Buffering

• The Idea:

The rendering engine writes to a background image or screen buffer.

Then this buffer is transfered to the screen in its entirety (veryquickly).

• Where to Put this Functionality:

The VisualizationView class.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 28 / 85

Page 34: The Design and Implementation of Multimedia Software

Rendering Individual Frames

Attributes for Double Buffering

// Attributes used for double-buffering

protected boolean useDoubleBuffering;

protected Graphics2D bg;

protected Image offscreenImage;

protected int height, width;

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 29 / 85

Page 35: The Design and Implementation of Multimedia Software

Rendering Individual Frames

The Buffer

private Graphics2D createOffscreenBuffer()

{

Dimension d;

d = getSize();

if ((d.height != height) || (d.width != width))

{

height = d.height;

width = d.width;

offscreenImage = createImage(width, height);

bg = (Graphics2D)offscreenImage.getGraphics();

bg.setClip(0,0,width,height);

}

return bg;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 30 / 85

Page 36: The Design and Implementation of Multimedia Software

Rendering Individual Frames

Rendering

public void paint(Graphics g)

{

Graphics2D bg;

if (useDoubleBuffering) bg = createOffscreenBuffer();

else bg = (Graphics2D)g;

if (bg != null)

{

// Perform necessary operations before rendering

preRendering(bg);

// Render the visual content

render(bg);

// Perform necessary operations after rendering

postRendering(bg);

if (useDoubleBuffering)

{

// Put the offscreen image on the screen

g.drawImage(offscreenImage, 0, 0, null);

// Reset the clipping area

bg.setClip(0,0,width,height);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 31 / 85

Page 37: The Design and Implementation of Multimedia Software

Rendering Individual Frames

Rendering (cont.)

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 32 / 85

Page 38: The Design and Implementation of Multimedia Software

Operating on Multiple Frames

What’s Next

We need to consider operations on multiple frames.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 33 / 85

Page 39: The Design and Implementation of Multimedia Software

Operating on Multiple Frames

Motivation

Definition

A straight cut involves rendering frame n+ 1, in its entirety,immediately after frame n.

• As it has been implemented thus far, the Screen class always usesstraight cuts.

• An alternative way to render a sequence of frames is to use atransition.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 34 / 85

Page 40: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Fades

Common Fades

Definition

A fade from black is a progressive brightening and a fade to black is aprogressive darkening.

The fade from black is a specific example of a fade-in (or fade-up) andthe fade to black is a specific example of a fade-out (or fade-to).

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 35 / 85

Page 41: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Fades

Setting the Destination Pixels to Black

protected void setDestinationPixels(Graphics g)

{

Graphics2D g2;

Rectangle r;

g2 = (Graphics2D)g;

r = g2.getClipBounds();

g2.setComposite(AlphaComposite.Src);

g2.setColor(g2.getBackground());

g2.fill(r);

g2.draw(r);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 36 / 85

Page 42: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Fades

Pre-Rendering

g2 = (Graphics2D)g;

originalComposite = g2.getComposite();

alpha = ((float)(frame - first + 1))/(float)duration;

if (direction == FADE_OUT) alpha = 1.0f - alpha;

if (alpha > 1.0f) alpha = 1.0f;

else if (alpha < 0.0f) alpha = 0.0f;

setDestinationPixels(g2);

ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,

alpha);

g2.setComposite(ac);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 37 / 85

Page 43: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Fades

Post-Rendering

g2 = (Graphics2D)g;

if (originalComposite != null) g2.setComposite(originalComposite);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 38 / 85

Page 44: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Dissolves

Understanding Dissolves

• Usual Description:

Frame n fades out while frame n+ 1 fades in.

A fade-in without an intermediate solid background.

• Approach:

Specialize the Fade class and override thesetDestinationPixels() method so that it does nothing.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 39 / 85

Page 45: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Dissolves

Dissolve

package visual.dynamic.sampled;

import java.awt.*;

public class Dissolve extends Fade

{

public Dissolve(int first, int duration)

{

super(FADE_IN, first, duration);

}

protected void setDestinationPixels(Graphics g)

{

// Use the last frame

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 40 / 85

Page 46: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

Basics

Definition

In a wipe, a frame (or frames) is clipped to a series of (one or more)geometric shapes.

• Circle Wipe

• Fly-On Wipe

• Line Wipe

• Quivering wipe (e.g., in “Wayne’s World” in Saturday Night Live)

• Rectangle Wipe

• Star Wipe (e.g., Homer’s video of Flanders in The Simpsons)

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 41 / 85

Page 47: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

RectangleWipe – calculateClip()

protected Rectangle2D calculateClip(float width,

float height, int frame)

{

float h, w, x, y;

Rectangle2D clip;

w = scale*frame*width;

h = scale*frame*height;

x = width/2.0f - w/2.0f;

y = height/2.0f - h/2.0f;

clip = new Rectangle2D.Float(x, y, w, h);

return clip;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 42 / 85

Page 48: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

RectangleWipe – Pre-Rendering

g2 = (Graphics2D)g;

originalClip = g2.getClip();

bounds = g2.getClipBounds();

height = (float)(bounds.getHeight());

width = (float)(bounds.getWidth());

clip = calculateClip(width, height, frame-first+1);

g2.setClip(clip);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 43 / 85

Page 49: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

RectangleWipe – Post-Rendering

g2 = (Graphics2D)g;

if (originalClip != null) g2.setClip(originalClip);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 44 / 85

Page 50: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

LineWipe

protected Rectangle2D calculateClip(float width,

float height,

int frame)

{

float h, w, x, y;

Rectangle2D clip;

w = width;

h = height;

x = 0.0f;

y = 0.0f;

if (direction == RIGHT)

{

w = scale*frame*width;

h = height;

x = 0.0f;

y = 0.0f;

}

else if (direction == LEFT)

{

w = scale*frame*width;

h = height;

x = width - w;

y = 0.0f;

}

else if (direction == UP)

{

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 45 / 85

Page 51: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

LineWipe (cont.)

w = width;

h = scale*frame*height;

x = 0.0f;

y = height - h;

}

else

{

w = width;

h = scale*frame*height;

x = 0.0f;

y = 0.0f;

}

clip = new Rectangle2D.Float(x, y, w, h);

return clip;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 46 / 85

Page 52: The Design and Implementation of Multimedia Software

Operating on Multiple Frames Wipes

Transitions – Demonstration

In examples/chapter:java -cp multimedia2.jar:examples.jar TransitionDemo -Xmx256m

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 47 / 85

Page 53: The Design and Implementation of Multimedia Software

Operating on Individual Frames

What’s Next

We need to consider operations on individual frames.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 48 / 85

Page 54: The Design and Implementation of Multimedia Software

Operating on Individual Frames

Don’t Get Confused

• A variety of operations can be performed on the individual frameswhen they are created/produced.

• One can also perform operations on individual frames while theyare being presented in a dynamic setting.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 49 / 85

Page 55: The Design and Implementation of Multimedia Software

Operating on Individual Frames

Superimpositions

Definition

A superimposition is visual content that is to be added to an existingframe while it is being rendered.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 50 / 85

Page 56: The Design and Implementation of Multimedia Software

Operating on Individual Frames

Examples of Superimpositions

• ‘Text’:

Films superimpose credits

Sporting events superimpose statistics

Television shows superimpose closed-captioning

• ‘Graphics’:

Television shows superimpose network logos

Football games superimpose first-down lines

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 51 / 85

Page 57: The Design and Implementation of Multimedia Software

Operating on Individual Frames

Superimpositions – Demonstrations

In examples/chapter:java -cp multimedia2.jar:examples.jar SuperimpositionDemo

java -cp multimedia2.jar:examples.jar SuperimpositionDemo buzzy

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 52 / 85

Page 58: The Design and Implementation of Multimedia Software

Operating on Individual Frames

AbstractSuperimposition – Construction

public AbstractSuperimposition(int first,

int duration, int position)

{

super(first, duration);

this.position = SwingConstants.SOUTH_EAST;

if ((position == SwingConstants.NORTH) ||

(position == SwingConstants.NORTH_EAST) ||

(position == SwingConstants.EAST) ||

(position == SwingConstants.SOUTH_EAST) ||

(position == SwingConstants.SOUTH) ||

(position == SwingConstants.SOUTH_WEST) ||

(position == SwingConstants.WEST) ||

(position == SwingConstants.NORTH_WEST) ||

(position == SwingConstants.CENTER ) )

{

this.position = position;

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 53 / 85

Page 59: The Design and Implementation of Multimedia Software

Operating on Individual Frames

AbstractSuperimposition – Registration

protected Point2D calculateRegistrationPoint(double frameWidth,

double frameHeight, double siWidth, double siHeight)

{

double left, top;

top = 0.0;

left = 0.0;

if (position == SwingConstants.NORTH)

{

top = siHeight;

left = frameWidth/2.0 - siWidth/2.0;

}

else if (position == SwingConstants.NORTH_EAST)

{

top = siHeight;

left = frameWidth - siWidth - 1;

}

else if (position == SwingConstants.EAST)

{

top = frameHeight/2.0 - siHeight/2.0;

left = frameWidth - siWidth - 1;

}

else if (position == SwingConstants.SOUTH_EAST)

{

top = frameHeight - siHeight - 1;

left = frameWidth - siWidth - 1;

}

else if (position == SwingConstants.SOUTH)

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 54 / 85

Page 60: The Design and Implementation of Multimedia Software

Operating on Individual Frames

AbstractSuperimposition – Registration (cont.)

{

top = frameHeight - siHeight - 1;

left = frameWidth/2.0 - siWidth/2.0;

}

else if (position == SwingConstants.SOUTH_WEST)

{

top = frameHeight - siHeight - 1;

left = 0.0;

}

else if (position == SwingConstants.WEST)

{

top = frameHeight/2.0 - siHeight/2.0;

left = 0.0;

}

else if (position == SwingConstants.NORTH_WEST)

{

top = siHeight;

left = 0.0;

}

else if (position == SwingConstants.CENTER)

{

top = frameHeight/2.0 - siHeight/2.0;

left = frameWidth/2.0 - siWidth/2.0;

}

return new Point2D.Double(left, top);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 55 / 85

Page 61: The Design and Implementation of Multimedia Software

Operating on Individual Frames

TransformableContentSuperimposition

Post-Rendering

g2 = (Graphics2D)g;

// Transform the TransformableContent so that

// it is positioned properly

frameBounds = g2.getClipBounds();

frameWidth = (double)frameBounds.width;

frameHeight = (double)frameBounds.height;

contentBounds = content.getBounds2D(false);

contentWidth = contentBounds.getWidth();

contentHeight = contentBounds.getHeight();

rp = calculateRegistrationPoint(frameWidth,

frameHeight,

contentWidth,

contentHeight);

content.setLocation(rp.getX(), rp.getY());

content.render(g);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 56 / 85

Page 62: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

What’s Next

We need to design a sampled dynamic visual content system.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 57 / 85

Page 63: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Requirements

F8.2 Support transitions (other than straight cuts) betweenframes of sampled dynamic content.

F8.3 Support the superimposition of static visual content onone or more frames of sampled dynamic content.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 58 / 85

Page 64: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 1

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 59 / 85

Page 65: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 1

It leads to an enormous amount of code duplication.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 59 / 85

Page 66: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 2

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 60 / 85

Page 67: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 2

Inflexibility – it does not lend itself to the addition oftransition/superimposition classes that do not extend theAbstractTransition or AbstractSuperimposition classes.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 60 / 85

Page 68: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 3

What are the shortcomings?

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 61 / 85

Page 69: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 3

At the level of the interface, it does not distinguish betweentransitions and superimpositions. Hence, it is not type-safe.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 61 / 85

Page 70: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Alternative 4

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 62 / 85

Page 71: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

FrameOp

package visual.dynamic.sampled;

import java.awt.Graphics;

public interface FrameOp

{

public abstract int getFirstFrame();

public abstract int getLastFrame();

public abstract void postRendering(Graphics g, int frame);

public abstract void preRendering(Graphics g, int frame);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 63 / 85

Page 72: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Superimposition

package visual.dynamic.sampled;

public interface Superimposition extends FrameOp

{

public abstract int getPosition();

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 64 / 85

Page 73: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Transition

package visual.dynamic.sampled;

public interface Transition extends FrameOp

{

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 65 / 85

Page 74: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

AbstractFrameOp – Structure

package visual.dynamic.sampled;

import java.awt.Graphics;

public abstract class AbstractFrameOp

implements FrameOp

{

protected int duration, first;

public AbstractFrameOp(int first, int duration)

{

this.first = first;

this.duration = 0;

if (duration > 0) this.duration = duration;

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 66 / 85

Page 75: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

AbstractFrameOp – Methods

public int getFirstFrame()

{

return first;

}

public int getLastFrame()

{

return first+duration;

}

protected boolean hasFinishedAt(int frame)

{

return (frame >= (first+duration-1));

}

protected boolean shouldApplyAt(int frame)

{

return ((frame >= first) && (frame <= (first+duration-1)));

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 67 / 85

Page 76: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Screen – Transitions and Superimpositions

// Attributes used for transitions

private IntervalIndexedCollection<Transition> transitions;

// Attributes used for superimpositions

private IntervalIndexedCollection<Superimposition> superimpositions;

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 68 / 85

Page 77: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Screen – Managing Transitions

public void addTransition(Transition t)

{

transitions.add(t, t.getFirstFrame(), t.getLastFrame());

}

public Iterator<Transition> getTransitions()

{

Iterator<Transition> result;

result = null;

if (frameNumber >= 0) result=transitions.get(frameNumber);

return result;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 69 / 85

Page 78: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Screen – Managing Superimpositions

public void addSuperimposition(Superimposition si)

{

superimpositions.add(si, si.getFirstFrame(), si.getLastFrame());

}

public Iterator<Superimposition> getSuperimpositions()

{

Iterator<Superimposition> result;

result = null;

if (frameNumber >= 0) result=superimpositions.get(frameNumber);

return result;

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 70 / 85

Page 79: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Enhancing the VisualizationView

• Recall:

VisualizationView objects delegate their rendering responsibilitiesto a VisualizationRenderer.

A VisualizationRenderer object can be decorated to addfunctionality.

• What We Need Now:

A decorator called a ScreenRenderer.

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 71 / 85

Page 80: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

ScreenRenderer – Pre-Rendering

public void preRendering(Graphics g,

Visualization model,

VisualizationView view)

{

Graphics2D g2;

int frameNumber;

Screen smodel;

Iterator<Transition> transitions;

Iterator<Superimposition> superimpositions;

g2 = (Graphics2D)g;

oldComposite = g2.getComposite();

view.setDoubleBuffered(true);

// Get information from the model

smodel = (Screen)model;

transitions = smodel.getTransitions();

superimpositions = smodel.getSuperimpositions();

frameNumber = smodel.getFrameNumber();

// Apply the transitions

if (transitions != null)

{

while (transitions.hasNext())

{

transitions.next().preRendering(g, frameNumber);

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 72 / 85

Page 81: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

ScreenRenderer – Pre-Rendering (cont.)

// Apply the superimpositions

if (superimpositions != null)

{

while (superimpositions.hasNext())

{

superimpositions.next().preRendering(g, frameNumber);

}

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 73 / 85

Page 82: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

ScreenRenderer – Post-Rendering

public void postRendering(Graphics g,

Visualization model,

VisualizationView view)

{

Graphics2D g2;

int frameNumber;

Screen smodel;

Iterator<Transition> transitions;

Iterator<Superimposition> superimpositions;

g2 = (Graphics2D)g;

g2.setComposite(oldComposite);

view.setDoubleBuffered(true);

// Get information from the model

smodel = (Screen)model;

frameNumber = smodel.getFrameNumber();

transitions = smodel.getTransitions();

superimpositions = smodel.getSuperimpositions();

// Apply the transitions

if (transitions != null)

{

while (transitions.hasNext())

{

transitions.next().postRendering(g, frameNumber);

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 74 / 85

Page 83: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

ScreenRenderer – Post-Rendering (cont.)

// Apply the superimpositions

if (superimpositions != null)

{

while (superimpositions.hasNext())

{

superimpositions.next().postRendering(g, frameNumber);

}

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 75 / 85

Page 84: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

ScreenRenderer – Rendering

public void render(Graphics g,

Visualization model,

VisualizationView view)

{

decorated.render(g, model, view);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 76 / 85

Page 85: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System

Screen – createDefaultView()

protected VisualizationView createDefaultView()

{

ScreenRenderer renderer;

renderer = new ScreenRenderer(new PlainVisualizationRenderer());

return new VisualizationView(this, renderer);

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 77 / 85

Page 86: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled Content

import javax.swing.*;

import app.*;

import visual.*;

import io.ResourceFinder;

import visual.dynamic.sampled.*;

import visual.statik.*;

import visual.statik.sampled.*;

public class DualScreenPIPDemo extends JApplication

{

public static void main(String[] args)

{

JApplication demo = new DualScreenPIPDemo(args, 640, 480);

invokeInEventDispatchThread(demo);

}

public DualScreenPIPDemo(String[] args, int width, int height)

{

super(args, width, height);

}

public void init()

{

ResourceFinder finder = ResourceFinder.createInstance(new resources.Marker());

Screen screen1 = new Screen(4);

screen1.setRepeating(true);

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 78 / 85

Page 87: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled Content (cont.)

VisualizationView view1 = screen1.getView();

view1.setRenderer(new ScaledVisualizationRenderer(view1.getRenderer(),

200.0, 200.0));

view1.setBounds(200,10,100,100);

view1.setSize(100,100);

String[] names = finder.loadResourceNames("solidclock.txt");

ContentFactory factory = new ContentFactory(finder);

SimpleContent[] frames1 = factory.createContents(names, 4);

for (int i=0; i<frames1.length; i++)

{

screen1.add(frames1[i]);

}

Screen screen2 = new Screen(24);

screen2.setRepeating(true);

VisualizationView view2 = screen2.getView();

view2.setBounds(0,0,320,240);

names = finder.loadResourceNames("scribble.txt");

SimpleContent[] frames2 = factory.createContents(names, 4);

for (int i=0; i<frames2.length; i++)

{

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 79 / 85

Page 88: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled Content (cont.)

screen2.add(frames2[i]);

}

// The content pane

JPanel contentPane = (JPanel)getContentPane();

contentPane.add(view1);

contentPane.add(view2);

screen2.start();

screen1.start();

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 80 / 85

Page 89: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP – Demonstration (Sampled)

In examples/chapter:java -cp multimedia2.jar:examples.jar DualScreenPIPDemo -Xmx256m

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 81 / 85

Page 90: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled and Described Content

import javax.swing.*;

import app.*;

import visual.*;

import io.ResourceFinder;

import visual.dynamic.sampled.Screen;

import visual.statik.SimpleContent;

import visual.statik.sampled.ContentFactory;

public class ScreenPIPDemo extends JApplication

{

public static void main(String[] args)

{

JApplication demo = new ScreenPIPDemo(args, 640, 480);

invokeInEventDispatchThread(demo);

}

public ScreenPIPDemo(String[] args, int width, int height)

{

super(args, width, height);

}

public void init()

{

ResourceFinder finder;

finder = ResourceFinder.createInstance(new resources.Marker());

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 82 / 85

Page 91: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled and Described Content (cont.)

Screen screen1 = new Screen(20);

screen1.setRepeating(true);

VisualizationView view1 = screen1.getView();

view1.setBounds(0,0,320,240);

String[] names = finder.loadResourceNames("scribble.txt");

ContentFactory factory = new ContentFactory(finder);

SimpleContent[] frames1 = factory.createContents(names, 4);

for (int i=0; i<frames1.length; i++)

{

screen1.add(frames1[i]);

}

Screen screen2 = new Screen();

screen2.setRepeating(true);

VisualizationView view2 = screen2.getView();

view2.setRenderer(new ScaledVisualizationRenderer(view2.getRenderer(),

200.0, 200.0));

view2.setBounds(200,10,75,75);

view2.setSize(75,75);

MovingRectangle mr = new MovingRectangle();

SimpleContent[] frames2 = mr.getFrames();

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 83 / 85

Page 92: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP - Sampled and Described Content (cont.)

for (int i=0; i<frames2.length; i++)

{

screen2.add(frames2[i]);

}

// The content pane

JPanel contentPane = (JPanel)getContentPane();

contentPane.add(view2);

contentPane.add(view1);

screen1.start();

screen2.start();

}

}

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 84 / 85

Page 93: The Design and Implementation of Multimedia Software

Design of a Sampled Dynamic Visual Content System Examples

PIP – Demonstration (Sampled and Described)

In examples/chapter:java -cp multimedia2.jar:examples.jar ScreenPIPDemo -Xmx256m

David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 85 / 85