This discussion is archived
10 Replies Latest reply: Mar 2, 2009 9:11 AM by 843851 RSS

HContainer problem, Xlet

843851 Newbie
Currently Being Moderated
Hi all,

I'm new to iTV (and also to this forum), and i'm realizing an application in Java for a project.
In my first basic part of this application, i wanted to create and customize a HContainer object and show it on the screen. The problem i'm facing is that i can only see my text field on the screen, and not the background of my container.
I've tried many things to solve this problem but always no results. Maybe i'm wrong in the way to manage my Xlet, or my HContainer.

Help is welcome ! Thanks


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import javax.tv.graphics.AlphaColor;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import org.havi.ui.HContainer;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HStaticText;


public class ServiceDescription implements Xlet{
     
     
     private HScene scene;     
     private Container servDescrCont;
     private static final Color bgColorContainer = new AlphaColor(100, 100, 230, 120);     

     
     public void initXlet(XletContext xletCtx) throws XletStateChangeException {
          try{
               //     Get a scene and define a size
               scene = HSceneFactory.getInstance().getDefaultHScene();
               scene.setSize(640,480);
               
               //     Launch the initialization of the descriptor
               initDescriptor();
               
               scene.validate();
          }
          catch(Exception e){
               e.printStackTrace();
               throw new javax.tv.xlet.XletStateChangeException(e.getMessage());
          }
     }


     public void startXlet() throws XletStateChangeException {
          try{
               //     Show the scene created
               scene.setVisible(true);
               scene.show();
          }
          catch(Exception e){
               e.printStackTrace();
               throw new javax.tv.xlet.XletStateChangeException(e.getMessage());
          }
     }


     public void pauseXlet() {

     }

     
     public void destroyXlet(boolean arg0) throws XletStateChangeException {

     }
     
     
     private void initDescriptor()
     {
          servDescrCont = new HContainer(50, 50, 540, 380);
          servDescrCont.setLayout(new BorderLayout());
          servDescrCont.setBackground(bgColorContainer);

     // Title
          HStaticText title = new HStaticText("Service Description");
     title.setForeground(new Color(220, 220, 220));
     title.setFont(new Font("SansSerif", 0, 18));

     servDescrCont.add(title, BorderLayout.NORTH);
     servDescrCont.setVisible(true);
     
     // Add the container to the scene
     scene.add(servDescrCont);
     }

}
  • 1. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    The Backgriond does't work in HContainer, you can extends the HContainer class an redefine the paint() procedure.

    public void paint(Graphics g) {  
          g.setColor(this.getBackground());            
          g.fillRect(0, 0, this.getWidth(), this.getHeight());      
          super.paint(g);
     }
  • 2. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Thanks for your reply !
    But i don't see how i can use this method for painting my container (the code you gave me). I've seen that there are paint and repaint methods, but i don't really understand their use for both of them.
    Also, is it a call to do in the startXlet method like scene.repaint()? Or somewhere else?

    Thanks for your help
  • 3. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Just copy and paste paint method, it will be called by default
  • 4. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Even if i just paste the code of this method and describe that my class extends HContainer, it doesn't work.
  • 5. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Have you tried to use the setBackgroundMode(HVisible.BACKGROUND_FILL) ?

    By default many objects don't draw their background, just give them a little push ;)
  • 6. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Hey i posting the working code for you this could help u, let me know if that is helpfull,TC
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.tv.xlet.Xlet;
    import javax.tv.xlet.XletContext;
    import javax.tv.xlet.XletStateChangeException;
    import org.dvb.event.EventManager;
    import org.dvb.event.UserEvent;
    import org.dvb.event.UserEventListener;
    import org.dvb.event.UserEventRepository;
    import org.havi.ui.HContainer;
    import org.havi.ui.HScene;
    import org.havi.ui.HStaticText;
    import org.havi.ui.event.HRcEvent;


    public class ServiceSel extends HContainer implements Xlet,FocusListener, UserEventListener {
    /**
         *
         */
         private static final long serialVersionUID = 1L;
         private HScene scene;
    private XletContext context;
         private EventManager evtManager;
         private UserEventRepository userEvents;
         private HStaticText title;
         public void destroyXlet(boolean unconditional)
                   throws XletStateChangeException {
              
              if(!unconditional)
                   throw new XletStateChangeException("Please don,t let me die............");
              if(scene != null){
                   scene.setVisible(false);
                   scene.removeFocusListener(this);
                   scene.removeAll();
                   org.havi.ui.HSceneFactory.getInstance().dispose(scene);
                   scene = null;
              }
              context.notifyDestroyed();
         }

         public void initXlet(XletContext ctx) throws XletStateChangeException {
              this.context = ctx;
              this.getResources();
              this.addFocusListener(this);
              this.setSize(scene.getSize());
              registerUserEventListener(evtManager, userEvents);
         }
         
    /**
         * method to create the scene
         *
    */
         public void getResources() {
              org.havi.ui.HSceneFactory factory = org.havi.ui.HSceneFactory.getInstance();
              org.havi.ui.HSceneTemplate hst = new org.havi.ui.HSceneTemplate();
              hst.setPreference(org.havi.ui.HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1, 1), org.havi.ui.HSceneTemplate.REQUIRED);
              hst.setPreference(org.havi.ui.HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0, 0), org.havi.ui.HSceneTemplate.REQUIRED);
              scene = factory.getBestScene(hst);
              scene.setBounds(0, 0, 720, 576);
              scene.addFocusListener(this);
              title=new HStaticText("Hi ther guys");
              title.setBounds(50, 50, 100, 200);
              title.setForeground(java.awt.Color.blue);
              //title.setFont(new Font(SansSerif", 0, 18));
              scene.add(title);
              scene.add(this);
    }
         
         public void pauseXlet() {
              scene.setVisible(false);
         }

         public void startXlet() throws XletStateChangeException {
              this.validate();
              this.scene.setVisible(true);
         }
         public void userEventReceived(UserEvent e) {
              if(e.getType() == HRcEvent.KEY_PRESSED)
              {
                   switch(e.getCode()){
                        case HRcEvent.VK_ESCAPE:
                             try {
                                  this.destroyXlet(true);
                             } catch (XletStateChangeException e1) {
                                  e1.printStackTrace();
                             }
                             break;
                   }
         }
         }
         
         public void focusGained(FocusEvent e) {
         }

         public void focusLost(FocusEvent e) {
         }
         public void registerUserEventListener(EventManager eventManager, UserEventRepository userEvents) {
              userEvents = new UserEventRepository("Application Events");
              userEvents.addAllArrowKeys();
              userEvents.addAllColourKeys();
              userEvents.addKey(HRcEvent.VK_ENTER);
              userEvents.addKey(HRcEvent.VK_ESCAPE);
              eventManager = EventManager.getInstance();
              eventManager.addUserEventListener(this, userEvents);
         }
         //paint the container
         public void paint(Graphics g) {
         g.setColor(java.awt.Color.cyan);
         g.fillRect(0, 0, this.getWidth(), this.getHeight());
         super.paint(g);
         }

    }
  • 7. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    @Zuarko : Yes i've tried with setBackgroundMode but it doesn't change anything. And this method is only available for Hscene and not for HContainer.
    @ Shri_katti : Thanks for your code. I've seen that it works but the thing is you don't use HContainer object, and i want to use one to contain HStaticTexts, HTextButtons, HSingleLineEntrys, and possible other HAVI items. I think that it is the proper way to manage my scene, but maybe it is not.

    Do you think it is more interesting to add directly all the elements on the scene or to have a container for all the elements?
  • 8. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Yes, I didn't look at the javadocs... what a shame... hehe.

    It depends on what will the elements act as. I mean, if you're trying to emulate somehow a single page or similar, it's better to use a HContainer, it will help you to move/add/delete them from screen. If it's a single screen, with no page behavior you can add them to the HScreen.

    Anyway, other thing you can do is to extend the HContainer and implement a new constructor which could draw the background rectangle.
  • 9. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Thanks for your reply Zuarko.

    I want to do a diagnostic screen which can be activated/unactivated by a user key event. And this interface will possibly be shown over the video running on the other layout (like a guide tv). I think it is necessary for me to use HContainer. I will try to get books about HAVi, because i've also got problems with HSinglelineEntry display.
    All of these objects are quite complicated to use ! Or maybe it is because i'm not used to use them...
  • 10. Re: HContainer problem, Xlet
    843851 Newbie
    Currently Being Moderated
    Just to tell it's now working well, by changing things here and there. Joys of programming !