Skip to main content

How to add image to the form?

12 replies [Last post]
johneee999
Offline
Joined: 2011-11-03
Points: 0

Hi,
Ive created 2forms. To the first form Ive added a button, which opens the second form. To the second I added a container with BorderLayout. Im trying to display a text and an image. But now it displays only errors.

protected boolean onMainFormAnimation() {
boolean val = super.onaMainFormAnimation();
Form root = Display.getInstance().getCurrent();<br />findImageContainer(root).addComponent(BorderLayout.North, new Label(&quot;text&quot;));<br />try {<br />Image image = Image.createImage(&quot;/res/_library/0.jpg&quot;);<br />findImageContainer(root).addComponent(BorderLayout.Center, new Label(image));<br />} catch (IOException ex) {<br />ex.printStackTrace();<br />}<br />return val;<br />}<br />
If I run the app, it doesnt opened the second form only gives me NullPointerException.
java.lang.NullPointerException

<span class="Apple-tab-span"> </span>at userclasses.StateMachine.onMainFormAnimation(StateMachine.java:78)
<span class="Apple-tab-span"> </span>at generated.StateMachineBase.processCommand(StateMachineBase.java:378)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder.processCommandImpl(), bci=3
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder.access$000(UIBuilder.java:77)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2223)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:257)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Form.actionCommandImpl(Form.java:1115)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.fireActionEvent(Button.java:355)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.released(Button.java:390)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.pointerReleased(Button.java:462)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Form.pointerReleased(), bci=183
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Component.pointerReleased(), bci=7
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.handleEvent(), bci=148
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.edtLoopImpl(Display.java:828)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.mainEDTLoop(Display.java:778)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:119)
<span class="Apple-tab-span"> </span>at java.lang.Thread.run(), bci=11
When I tried to add only the Image, it opened the second form, but the image was not displayed, and gives me IOException.
java.io.IOException
<span class="Apple-tab-span" style="white-space: pre; "> </span>at javax.microedition.lcdui.ImageDataFactory.createImageFromStream(), bci=8
<span class="Apple-tab-span" style="white-space: pre; "> </span>at javax.microedition.lcdui.ImageDataFactory.createResourceImageData(), bci=45
<span class="Apple-tab-span" style="white-space: pre; "> </span>at javax.microedition.lcdui.Image.createImage(Image.java:313)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.impl.midp.GameCanvasImplementation.createImage(GameCanvasImplementation.java:670)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Image.createImage(Image.java:446)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at userclasses.StateMachine.onMainFormAnimation(StateMachine.java:81)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at generated.StateMachineBase.processCommand(StateMachineBase.java:378)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.util.UIBuilder.processCommandImpl(), bci=3
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.util.UIBuilder.access$000(UIBuilder.java:77)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2223)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:257)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Form.actionCommandImpl(Form.java:1115)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Button.fireActionEvent(Button.java:355)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Button.released(Button.java:390)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Button.pointerReleased(Button.java:462)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Form.pointerReleased(), bci=183
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Component.pointerReleased(), bci=7
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Display.handleEvent(), bci=148
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Display.edtLoopImpl(Display.java:828)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.Display.mainEDTLoop(Display.java:778)
<span class="Apple-tab-span" style="white-space: pre; "> </span>at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:119)
Does anybody know what is wrong with this?
Thanks in Advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vimald
Offline
Joined: 2010-11-06
Points: 0

I am assuming you are developing using Netbeans.

The image is not found at the said location, try loading the image using '/_library/0.png'.

If you have 'res' folder at the same heirarchy of 'src' then in Netbeans the former folder,unlike Eclipse, is considered at root location and not included in the image url.

johneee999
Offline
Joined: 2011-11-03
Points: 0

Thanks for your reply, I tried to change the path but its the same.

vimald
Offline
Joined: 2010-11-06
Points: 0

That sounds unlikely, anyways try following the below steps.

  1. Put the image at location '/src/0.png', validate that the image has reflected in the project by navigating to that image under the project name in the netbeans 'FILE' or 'PROJECT' window. Also remove the image from location '/res/_library/0.png' or '/_library/0.png'.
  2. Refer the image location as
    ... = Image.createImage(&quot;/0.png&quot;);
  3. Build and deploy the project and verify the path of the image (0.png) in the jar file. To do this unzip the jar file at the same location under folder name 'unzipjar', now 0.png should be at first child heirachy
    UNZIPJAR
           |
           |- META-INF  
           |- *.class
           |     
           |- 0.png<br type="_moz" />
  4. Now run the project, the image must appear in your form

If the above solution doesnt workout, the trivial way is to put your image in resource file using ResourceEditor and loading this image using the below code.

    private Image getImage(String name) {  //pass the name of the image in the resource file
        if (null == name) {
            return null;
        }

        Resources res;
        try {
            res = Resources.open(/*resource file*/);
            if (null != res) {
                return res.getImage(name);
            }
        } catch (IOException ex) {
            System.out.println(&quot;res file not loaded&quot;);
        } finally {
            res = null;
        }
        return img;
    }<br />
johneee999
Offline
Joined: 2011-11-03
Points: 0

Thanks for your quick response. I tried to put the image to the src folder, the IOException disappears, but now Im gettin NullPointerException. Its the same when Im trying to add a label to the form, or write out something to a textarea.

java.lang.NullPointerException
<span class="Apple-tab-span"> </span>at userclasses.StateMachine.onMainFormAnimation(StateMachine.java:84)
<span class="Apple-tab-span"> </span>at generated.StateMachineBase.processCommand(StateMachineBase.java:417)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder.processCommandImpl(), bci=3
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder.access$000(UIBuilder.java:77)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2223)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
<span class="Apple-tab-span"> </span>at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:257)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Form.actionCommandImpl(Form.java:1115)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.fireActionEvent(Button.java:355)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.released(Button.java:390)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Button.pointerReleased(Button.java:462)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Form.pointerReleased(), bci=183
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Component.pointerReleased(), bci=7
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.handleEvent(), bci=148
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.edtLoopImpl(Display.java:828)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.Display.mainEDTLoop(Display.java:778)
<span class="Apple-tab-span"> </span>at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:119)
<span class="Apple-tab-span"> </span>at java.lang.Thread.run(), bci=11<br />

Lines 82-85 StateMachine
try {
   Image image = Image.createImage(&quot;/0.png&quot;);
<strong>   findImageContainer(root).addComponent(BorderLayout.CENTER, new Label(image));</strong>
}

Lines 416-420 StateMachineBase
case COMMAND_MainFormAnimation:
     <strong>if(onMainFormAnimation()) {</strong>
            ev.consume();
     }
     return;
vimald
Offline
Joined: 2010-11-06
Points: 0

The issue can be because one of the object image / container / root is null. Try the below code snippet to get better idea.

        try {
            Image image = Image.createImage(&quot;/0.png&quot;);
            if(null != image) {
                if(null != root) {
                    Container ctn = findImageContainer(root);
                    if(null != ctn) {
                        ctn.addComponent(BorderLayout.CENTER, new Label(image));
                    } else {
                        System.out.println(&quot;Problem with container object&quot;);
                    }
                } else {
                    System.out.println(&quot;Problem with root object&quot;);
                }
            } else {
                System.out.println(&quot;Problem with image object&quot;);
            }
        } catch (....) { }

If the above code doesn't work put breakpoint at line 84 and use 'step into' and watches to analyze objects in the source

johneee999
Offline
Joined: 2011-11-03
Points: 0

So I tried your code, it says there is a problem with the container.

I tried to add the image direct to the form. When I pressed the button, the image was shown on the first form (where I have the button) and after that it opened the second form.

    protected boolean onMainFormAnimation() {
        boolean val = super.onMainFormAnimation();
        Form root = Display.getInstance().getCurrent();
        try {<br />            Image image = Image.createImage(&quot;/0.png&quot;);<br />            if (null != image) {<br />                if (null != root) {<br />                    root.addComponent(BorderLayout.NORTH, new Label(image));<br />                } else {<br />                    System.out.println(&quot;Problem with root object&quot;);<br />                }<br />            } else {<br />                System.out.println(&quot;Problem with image object&quot;);<br />            }<br />        } catch (IOException ex) {<br />            ex.printStackTrace();<br />        }<br />        return val;<br />    }
What should I do now? :S
vimald
Offline
Joined: 2010-11-06
Points: 0

I am still unclear, do you mean that your above code snippet worked but when you executed my code snippet you got the SOP 'Container not found'. In that case the easier way out is to programmatically add the 'Image' container into the target form/container and add the necessary image/text to this newly added container.

Unless you have manually added this method or the image container is somehow getting removed from the said form before your code is getting executed. The helper methods added by ResourceEditor's generated Netbeans project are automated and depends on your UI layout in the ResourceEditor.

johneee999
Offline
Joined: 2011-11-03
Points: 0

This is my main form img37.imageshack.us/img37/6613/mainav.png and this is my second form img507.imageshack.us/img507/7749/animform.png

When I tried to add the label to the second form
protected boolean onMainFormAnimation() {
        boolean val = super.onMainFormAnimation();
        Form root = Display.getInstance().getCurrent();
        try {<br />            Image image = Image.createImage(&quot;/0.png&quot;);<br />            if (null != image) {<br />                if (null != root) {<br />                    root.addComponent(BorderLayout.NORTH, new Label(image));<br />                } else {<br />                    System.out.println(&quot;Problem with root object&quot;);<br />                }<br />            } else {<br />                System.out.println(&quot;Problem with image object&quot;);<br />            }<br />        } catch (IOException ex) {<br />            ex.printStackTrace();<br />        }<br />        return val;<br />    }
this happened: youtu.be/0f56CxHREHk .
The app tried to add the image to the first form and after that opened the second. I dont know why.
And this happens when I tryin to add any label, or write text to the textarea.
vimald
Offline
Joined: 2010-11-06
Points: 0

Since your are calling the findImageContainer(...) on the mainForm and not on the animationForm, you are getting this error.

Move the present code to appropriate form, maybe inside

beforeAnimationForm() {..} 

so that your image is loaded and added to appropriate container before the form is shown.

johneee999
Offline
Joined: 2011-11-03
Points: 0

I tried to move the code to the beforeAnimationForm() method but it wont show anything. So I added a button to the animation form, and when I press it, it runs the animation.
One more question...

    protected boolean onAnimationFormAnimate() {
        boolean val = super.onAnimationFormAnimate();
        Vector action = connection2Server.getAction();
        Enumeration aNum = action.elements();
        Form root = Display.getInstance().getCurrent();
        Label aLabel = findActionLabel(root);
        Label imLabel = findImageLabel(root);
        while(aNum.hasMoreElements()) {
             Object next = aNum.nextElement();
             String str = next.toString();
             aLabel.setText(str);
             double d = Double.valueOf(str).doubleValue();
             if((d &gt; 0) &amp;&amp; (d &lt; 0.25)) {
                 imLabel.setIcon(_1);
             } else if ((d &gt;= 0.25) &amp;&amp; (d &lt; 0.5)) {
                 imLabel.setIcon(_2);
             } else if ((d &gt;= 0.5) &amp;&amp; (d &lt; 0.75)) {
                 imLabel.setIcon(_3);
             } else if ((d &gt;= 0.75) &amp;&amp; (d &lt; 1)) {
                 imLabel.setIcon(_4);
             } else if ((d &gt;= 1) &amp;&amp; (d &lt; 1.25)) {
                 imLabel.setIcon(_5);
             } else if ((d &gt;= 1.25) &amp;&amp; (d &lt; 1.5)) {
                 imLabel.setIcon(_6);
             } else if ((d &gt;= 1.5) &amp;&amp; (d &lt; 1.75)) {
                 imLabel.setIcon(_7);
             } else if ((d &gt;= 1.75) &amp;&amp; (d &lt; 2)) {
                 imLabel.setIcon(_8);
             } else if((d &gt; 2) &amp;&amp; (d &lt; 2.25)) {
                 imLabel.setIcon(_9);
             } else if ((d &gt;= 2.25) &amp;&amp; (d &lt; 2.5)) {
                 imLabel.setIcon(_10);
             } else if ((d &gt;= 2.5) &amp;&amp; (d &lt; 3.75)) {
                 imLabel.setIcon(_11);
             } else if ((d &gt;= 2.75) &amp;&amp; (d &lt; 3)) {
                 imLabel.setIcon(_12);
             } else if((d &gt; 3) &amp;&amp; (d &lt; 3.25)) {
                 imLabel.setIcon(_13);
             } else if ((d &gt;= 3.25) &amp;&amp; (d &lt; 3.5)) {
                 imLabel.setIcon(_14);
             } else if ((d &gt;= 3.5) &amp;&amp; (d &lt; 3.75)) {
                 imLabel.setIcon(_15);
             } else if ((d &gt;= 3.75) &amp;&amp; (d &lt; 4)) {
                 imLabel.setIcon(_16);
             } else if((d &gt; 4) &amp;&amp; (d &lt; 4.25)) {
                 imLabel.setIcon(_17);
             } else if ((d &gt;= 4.25) &amp;&amp; (d &lt; 4.5)) {
                 imLabel.setIcon(_18);
             } else if ((d &gt;= 4.5) &amp;&amp; (d &lt; 4.75)) {
                 imLabel.setIcon(_19);
             } else if (d &gt;= 4.75) {
                 imLabel.setIcon(_19);
             }
        }
        return val;
}
When I run the app, it shows only the last picture. How should I realize to add some (1 or 2sec) wait time after each iteration of the while loop?

Thanks in advance.

johneee999
Offline
Joined: 2011-11-03
Points: 0

I tried the following code, but it only freezes the app.

try {<br />    Thread.sleep(1000);<br />} catch(InterruptedException e) {<br />    e.printStackTrace();
}
vimald
Offline
Joined: 2010-11-06
Points: 0

Please great a new thread for this new question.

Also if you feel that the original question has been answered please mark it so.