This discussion is archived
1 Reply Latest reply: Mar 13, 2009 3:20 AM by 843851 RSS

Error draw an image on canvas

843851 Newbie
Currently Being Moderated
Hello! In this code, the drawImage method is returning false. If the same code is run as a desktop application using the main method the error does not happen.
Who knows why?
public class AppJavaTV implements Xlet{
    private XletContext context;
    private Container container;
    private SCanvas canvas;

    public void initXlet(XletContext arg0) throws XletStateChangeException {
        context = arg0;
        container = TVContainer.getRootContainer(arg0);

        canvas = new SCanvas();
        canvas.setLocation(0, 0);
        canvas.setSize(container.getSize());
        canvas.setBackground(Color.white);

        container.add(canvas);
        container.setVisible(true);
    }

    public void startXlet() throws XletStateChangeException {}

    public void pauseXlet() {}

    public void destroyXlet(boolean arg0) throws XletStateChangeException {}

    public static void main(String argv[]) {
        Frame frame = new Frame();
        frame.setLayout(new BorderLayout());
        frame.add("Center", new SCanvas());
        frame.resize(400, 400);
        frame.show();
    }

}




public class SCanvas extends Canvas{
    Image img = null;

    public SCanvas(){
        MediaTracker media = new MediaTracker(this);
        img = Toolkit.getDefaultToolkit().createImage("/Image.jpg");
        media.addImage(img, 0);
        try {
            media.waitForID(0);
        } catch (Exception e) {}
    }

    @Override
    public void paint(Graphics graphics){
        graphics.setColor(Color.blue);
        graphics.drawRect(0, 0, 50, 50);
        graphics.setColor(Color.black);

        if(!graphics.drawImage(img, 200, 200, this))
            graphics.drawString("Error on draw image", 200, 200);
    }
}
  • 1. Re: Error draw an image on canvas
    843851 Newbie
    Currently Being Moderated
    That is because you are using Canvas and Container objects. That objects are not used on the DTV environment. You should use the ones adapted to it, the ones contained in org.havi.ui, like HContainer, HScreen...

    Remember tha java.awt is not used (except for some classes) and the equivalent elements are under org.havi.ui