• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Section E.1. Scene APIs

E.1. Scene APIs

The following example shows a complete MPEGlet[1] using the Scene API to invert the color of a shape node when the user clicks on it. In this example, SceneExampleMpeglet implements the MPEGlet interface, SceneListener interface, and the EventOutListener interface. Initially, the SceneManager object is obtained from the MpegjTerminal and the MPEGlet adds itself as the SceneListener. Then the MPEGlet retrieves a reference to the TouchSensorNode (defined with the name TOUCH) and registers itself to be notified when the user clicks on the TouchSensorNode. Finally, the MPEGlet inverts the color of the shape node (defined with the name “MATERIAL”) when the MPEGlet receives the touch event.

[1] MPEG-J applications are called MPEGlets. All MPEG-J applications should implement the MPEGlet Java interface defined in MPEG-J.

import org.iso.mpeg.mpegj.*;
import org.iso.mpeg.mpegj.scene.*;

public class SceneExampleMpeglet implements
     MPEGlet, SceneListener, EventOutListener {

    Scene m_scene = null;
    boolean m_isTouched = false;

    public void run() {
        try {
            // Get the scene manager and register to be
            // notified when the scene is ready.
            MpegjTerminal terminal =
                 new MpegjTerminal(this);
            SceneManager manager =

            // Wait until the scene is ready.
            synchronized (this) {
                while (m_scene == null) {

            // Get the TouchSensor node.
            Node touch = m_scene.getNode("TOUCH");

            // Register to be notified when it's touched.
                EventOut.TouchSensor.touchTime, this);

            // Wait until it's touched.
            synchronized (this) {
                while (!m_isTouched) {
            // Invert the color of a shape as feedback.
            Node material2d = m_scene.getNode("MATERIAL");
            FieldValue val = material2d.getEventOut(
            final float[] color =
                 ((SFColorFieldValue) val).getSFColorValue();
            color[0] = 1 - color[0];
            color[1] = 1 - color[1];
            color[2] = 1 - color[2];
                   new SFColorFieldValue() {
                   public float[] getSFColorValue() {
                      return color;
        } catch (MPEGJException e) {
        } catch (InterruptedException e) {

    // Called by the scene manager when the scene is ready.
    public synchronized void notify(int msg, Scene scene) {
        if (msg == SceneListener.Message.SCENE_READY) {
            m_scene = scene;

    // Called when the touchTime eventOut is triggered.
    public synchronized void notify(int id, FieldValue v) {
        m_isTouched = true;

    public void init() {}
    public void stop() {}
    public void destroy() {}




Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint