Exemple BroadcastEventXlet

  • Ciclo de vida de Xlet pelo pacote javax.tv.xlet
  • Pintura na tela com Label do pacote com.sun.dtv.lwuit..
  • Uso de com.sun.dtv.broadcast.event

import java.io.IOException;

import javax.tv.locator.InvalidLocatorException;
import javax.tv.locator.Locator;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import com.sun.dtv.broadcast.event.BroadcastEventListener;
import com.sun.dtv.broadcast.event.BroadcastEventManager;
import com.sun.dtv.broadcast.event.BroadcastReceivedEvent;
import com.sun.dtv.locator.URLLocator;
import com.sun.dtv.lwuit.Form;
import com.sun.dtv.lwuit.Image;
import com.sun.dtv.lwuit.Label;
import com.sun.dtv.lwuit.geom.Dimension;
import com.sun.dtv.lwuit.layouts.BorderLayout;

public class BroadcastEventXlet extends Form implements Xlet,
        BroadcastEventListener {

    private Label label;
    BroadcastEventManager manager;

    private XletContext context;

    public void initXlet(XletContext ctx) throws XletStateChangeException {

        System.out.println("**** DEBUG -- initXlet");
        this.setSize(new Dimension(400, 400));

        this.context = ctx;
        setTitle("Broadcast Events");
    }

    public void startXlet() throws XletStateChangeException {

        System.out.println("**** DEBUG -- startXlet3");
        this.setSize(new Dimension(400, 400));
        label = new Label("No Events Received");
        addComponent(BorderLayout.NORTH, label);
        Locator locator;
        try {
            locator = new URLLocator("dtv://1.1.1.1");
            manager = new BroadcastEventManager(locator);
            manager.addListener(this);

        } catch (InvalidLocatorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        show();
    }

    public void pauseXlet() {

        System.out.println("**** DEBUG -- pauseXlet");
    }

    public void destroyXlet(boolean unconditional)
            throws XletStateChangeException {
        context.notifyDestroyed();
    }

    public void received(BroadcastReceivedEvent arg0) {
        String content = new String(arg0.getData());

        System.out.println("\nRecebi um Evento de nome: " + arg0.getName()
                + " e conteudo: " + content + "\n\n");
        label.setText("Event Received Content: " + content);
        try {
            Label imagelabel = new Label();
            imagelabel.setIcon(Image.createImage("goal.jpg"));
            addComponent(BorderLayout.CENTER, imagelabel);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.repaint();
    }

}

broadcastevent.bct (395 Bytes) Álan Livio, 18/10/2012 13:29

goal.jpg (24.2 kB) Álan Livio, 18/10/2012 14:40