You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Luiz Gustavo <lu...@gmail.com> on 2010/12/09 01:21:18 UTC

Image Path

Hi,


I had writen an application using Pivot 1.5.2 that used a gif image for a
button, and in the Java source I used the path without a "/" in the begining
of the path, like this:

private String defaultImage = "img/default.gif";

Now, rewriting the application in Pivot 2.0 I had to put the "/" in the
begining of the path (otherwise I receive a NullPointerException that tells
me the IconURL is null):

private String defaultImage = "/img/default.gif";

Are the images referenced by an absolute path now, and not relative to the
bxml script?

-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Image Path

Posted by Luiz Gustavo <lu...@gmail.com>.
"FYI, this isn't specific to Pivot - getResource() is a standard JDK
feature"

Yes! =)


Well, I'll let the customized ButtonData receive two strings as before. That
was working fine. But I'll try to discover the reason of the NPE anyway.


Thanks!

2010/12/9 Greg Brown <gk...@verizon.net>

> OK. It should work, as long as the image is on the class path. Not sure
> what the problem might be (FYI, this isn't specific to Pivot - getResource()
> is a standard JDK feature).
>
>
> On Dec 8, 2010, at 9:43 PM, Luiz Gustavo wrote:
>
> I got an error:
>
>
>
> java.lang.NullPointerException
>     at org.apache.pivot.wtk.media.Image$LoadTask.execute(Image.java:97)
>     at org.apache.pivot.wtk.media.Image.load(Image.java:144)
>     at org.apache.pivot.demos.memorygame.Main.startup(Main.java:46)
>     at
> org.apache.pivot.wtk.DesktopApplicationContext$2.run(DesktopApplicationContext.java:594)
>     at
> org.apache.pivot.wtk.ApplicationContext$QueuedCallback.run(ApplicationContext.java:1474)
>     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
>     at
> java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
>     at
> java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
>     at
> java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
>     at
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
>     at
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
>
>
>
> 2010/12/9 Greg Brown <gk...@verizon.net>
>
>> 2010/12/8 Greg Brown <gk...@verizon.net>
>>
>>> Ah, I see. You are calling ButtonData#setIcon(String) with that value.
>>> That method obtains the location of the image as relative to the classpath,
>>> so the leading "/" is required.
>>>
>>
>> When using Pivot 1.5.2 I didn't need the leading "/". Well, I'll use it
>> from now =)
>>
>>
>> Yes, that is something that changed in 2.0.
>>
>> I would probably use the setIcon(Image) version, and define a default
>>> Image instance by calling Image.load(). But either way will work.
>>>
>>
>> I tried use the version you suggested, but I got an error
>> (MalformedURLException).
>> Where am I missing something? See:
>>
>>
>> ...
>> private String defaultImage = "/img/default.gif";
>> private Image defaultImg;
>> ...
>>
>> defaultImg = Image.load(new URL(defaultImage));
>>
>>
>> Try this:
>>
>> defaultImg = Image.load(getClass().getResource("img/default.gif"));
>>
>> (no leading slash)
>>
>>
>>
>
>
> --
> Luiz Gustavo S. de Souza
>
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso
>
>
>


-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Image Path

Posted by Greg Brown <gk...@verizon.net>.
OK. It should work, as long as the image is on the class path. Not sure what the problem might be (FYI, this isn't specific to Pivot - getResource() is a standard JDK feature).


On Dec 8, 2010, at 9:43 PM, Luiz Gustavo wrote:

> I got an error:
> 
> 
> 
> java.lang.NullPointerException
>     at org.apache.pivot.wtk.media.Image$LoadTask.execute(Image.java:97)
>     at org.apache.pivot.wtk.media.Image.load(Image.java:144)
>     at org.apache.pivot.demos.memorygame.Main.startup(Main.java:46)
>     at org.apache.pivot.wtk.DesktopApplicationContext$2.run(DesktopApplicationContext.java:594)
>     at org.apache.pivot.wtk.ApplicationContext$QueuedCallback.run(ApplicationContext.java:1474)
>     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
>     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
>     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
>     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
> 
> 
> 
> 2010/12/9 Greg Brown <gk...@verizon.net>
>> 2010/12/8 Greg Brown <gk...@verizon.net>
>> Ah, I see. You are calling ButtonData#setIcon(String) with that value. That method obtains the location of the image as relative to the classpath, so the leading "/" is required.
>> 
>> When using Pivot 1.5.2 I didn't need the leading "/". Well, I'll use it from now =)
> 
> Yes, that is something that changed in 2.0.
> 
>> I would probably use the setIcon(Image) version, and define a default Image instance by calling Image.load(). But either way will work.
>> 
>> I tried use the version you suggested, but I got an error (MalformedURLException).
>> Where am I missing something? See:
>> 
>> 
>> ...
>> private String defaultImage = "/img/default.gif";
>> private Image defaultImg;
>> ...
>> 
>> defaultImg = Image.load(new URL(defaultImage));
> 
> Try this:
> 
> defaultImg = Image.load(getClass().getResource("img/default.gif"));
> 
> (no leading slash)
> 
> 
> 
> 
> 
> -- 
> Luiz Gustavo S. de Souza
> 
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso


Re: Image Path

Posted by Luiz Gustavo <lu...@gmail.com>.
I got an error:



java.lang.NullPointerException
    at org.apache.pivot.wtk.media.Image$LoadTask.execute(Image.java:97)
    at org.apache.pivot.wtk.media.Image.load(Image.java:144)
    at org.apache.pivot.demos.memorygame.Main.startup(Main.java:46)
    at
org.apache.pivot.wtk.DesktopApplicationContext$2.run(DesktopApplicationContext.java:594)
    at
org.apache.pivot.wtk.ApplicationContext$QueuedCallback.run(ApplicationContext.java:1474)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



2010/12/9 Greg Brown <gk...@verizon.net>

> 2010/12/8 Greg Brown <gk...@verizon.net>
>
>> Ah, I see. You are calling ButtonData#setIcon(String) with that value.
>> That method obtains the location of the image as relative to the classpath,
>> so the leading "/" is required.
>>
>
> When using Pivot 1.5.2 I didn't need the leading "/". Well, I'll use it
> from now =)
>
>
> Yes, that is something that changed in 2.0.
>
> I would probably use the setIcon(Image) version, and define a default Image
>> instance by calling Image.load(). But either way will work.
>>
>
> I tried use the version you suggested, but I got an error
> (MalformedURLException).
> Where am I missing something? See:
>
>
> ...
> private String defaultImage = "/img/default.gif";
> private Image defaultImg;
> ...
>
> defaultImg = Image.load(new URL(defaultImage));
>
>
> Try this:
>
> defaultImg = Image.load(getClass().getResource("img/default.gif"));
>
> (no leading slash)
>
>
>


-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Image Path

Posted by Greg Brown <gk...@verizon.net>.
> 2010/12/8 Greg Brown <gk...@verizon.net>
> Ah, I see. You are calling ButtonData#setIcon(String) with that value. That method obtains the location of the image as relative to the classpath, so the leading "/" is required.
> 
> When using Pivot 1.5.2 I didn't need the leading "/". Well, I'll use it from now =)

Yes, that is something that changed in 2.0.

> I would probably use the setIcon(Image) version, and define a default Image instance by calling Image.load(). But either way will work.
> 
> I tried use the version you suggested, but I got an error (MalformedURLException).
> Where am I missing something? See:
> 
> 
> ...
> private String defaultImage = "/img/default.gif";
> private Image defaultImg;
> ...
> 
> defaultImg = Image.load(new URL(defaultImage));

Try this:

defaultImg = Image.load(getClass().getResource("img/default.gif"));

(no leading slash)



Re: Image Path

Posted by Luiz Gustavo <lu...@gmail.com>.
2010/12/8 Greg Brown <gk...@verizon.net>

> Ah, I see. You are calling ButtonData#setIcon(String) with that value. That
> method obtains the location of the image as relative to the classpath, so
> the leading "/" is required.
>

When using Pivot 1.5.2 I didn't need the leading "/". Well, I'll use it from
now =)



> I would probably use the setIcon(Image) version, and define a default Image
> instance by calling Image.load(). But either way will work.
>

I tried use the version you suggested, but I got an error
(MalformedURLException).
Where am I missing something? See:


...
private String defaultImage = "/img/default.gif";
private Image defaultImg;
...

defaultImg = Image.load(new URL(defaultImage));

...

buttons[aux].setButtonData(new MemGameButtonData(defaultImg,
images36[aux]));

...

public class MemGameButtonData extends ButtonData{

    private Image defaultImage;
    private Image buttonImage;

    public MemGameButtonData(Image defaultImage, Image buttonImage){

        super();
        this.buttonImage = buttonImage;
        this.defaultImage = defaultImage;
        setDefaultURL();
    }

    public void setDefaultURL(){
        this.setIcon(defaultImage);
    }

    public void setButtonURL(){
        this.setIcon(buttonImage);
    }

    public Image getButtonURL(){
        return this.getIcon();
    }
}

Luiz Gustavo



>
> On Dec 8, 2010, at 8:41 PM, Luiz Gustavo wrote:
>
> Hi Greg,
>
> I use the image in the memory game that I've sended to you.
> I created a specialized ButtonData that receives two Strings in its
> constructor. One of then is de default image, that apears when the button is
> "closed" and the another is the image that apears when the user clicks the
> button.
> I have to do it programmatically 'cause I have to randomize the image
> locations.
>
> I knew about the "@" symbol in the bxml file... but I just had the error
> mentioned... in the source using Pivot 1.5.2 I didn't have to use the
> leading "/" in the path:
>
>
> ...
> private String defaultImage = "/img/default.gif";
> ...
>
> for(int aux = 0; aux < 36; aux++){
>              // *images36[aux]* contains the randomized images
>              buttons[aux].setButtonData(new MemGameButtonData(*
> defaultImage*, *images36[aux]*));
>              buttons[aux].setEnabled(true);
>          }
>
> ...
>
>
>
>
> public class MemGameButtonData extends ButtonData{
>
>     private String defaultURL;
>     private String buttonURL;
>
>     public MemGameButtonData(String URLPadrao, String URLBotao){
>
>         super();
>         this.buttonURL = URLBotao;
>         this.defaultURL = URLPadrao;
>         setDefaultURL();
>     }
>
>     public void setDefaultURL(){
>         this.setIcon(defaultURL);
>     }
>
>     public void setButtonURL(){
>         this.setIcon(buttonURL);
>     }
>
>     public Image getButtonURL(){
>         return this.getIcon();
>     }
> }
>
>
> Cheers,
> Luiz Gustavo
>
>
>
>
>
>
> 2010/12/8 Greg Brown <gk...@verizon.net>
>
>> The leading slash is now required in BXML. A leading "@" symbol means
>> "relative to the current file", whereas a leading "/" means relative to the
>> class path (i.e. absolute).
>>
>> However, you appear to be loading your images programmatically rather than
>> in markup - what exactly are you doing with the defaultImage value? There
>> may be an easier way to accomplish what you need.
>>
>> G
>>
>>
>> On Dec 8, 2010, at 7:21 PM, Luiz Gustavo wrote:
>>
>> Hi,
>>
>>
>> I had writen an application using Pivot 1.5.2 that used a gif image for a
>> button, and in the Java source I used the path without a "/" in the begining
>> of the path, like this:
>>
>> private String defaultImage = "img/default.gif";
>>
>> Now, rewriting the application in Pivot 2.0 I had to put the "/" in the
>> begining of the path (otherwise I receive a NullPointerException that tells
>> me the IconURL is null):
>>
>> private String defaultImage = "/img/default.gif";
>>
>> Are the images referenced by an absolute path now, and not relative to the
>> bxml script?
>>
>> --
>> Luiz Gustavo S. de Souza
>>
>> http://luizgustavoss.wordpress.com
>> http://luizgustavoss.blogspot.com
>> http://twitter.com/lugustso
>>
>>
>>
>
>
> --
> Luiz Gustavo S. de Souza
>
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso
>
>
>

Re: Image Path

Posted by Greg Brown <gk...@verizon.net>.
Ah, I see. You are calling ButtonData#setIcon(String) with that value. That method obtains the location of the image as relative to the classpath, so the leading "/" is required. I would probably use the setIcon(Image) version, and define a default Image instance by calling Image.load(). But either way will work.

On Dec 8, 2010, at 8:41 PM, Luiz Gustavo wrote:

> Hi Greg,
> 
> I use the image in the memory game that I've sended to you.
> I created a specialized ButtonData that receives two Strings in its constructor. One of then is de default image, that apears when the button is "closed" and the another is the image that apears when the user clicks the button.
> I have to do it programmatically 'cause I have to randomize the image locations.
> 
> I knew about the "@" symbol in the bxml file... but I just had the error mentioned... in the source using Pivot 1.5.2 I didn't have to use the leading "/" in the path:
> 
> 
> ...
> private String defaultImage = "/img/default.gif";
> ...
> 
> for(int aux = 0; aux < 36; aux++){
>              // images36[aux] contains the randomized images
>              buttons[aux].setButtonData(new MemGameButtonData(defaultImage, images36[aux])); 
>              buttons[aux].setEnabled(true);             
>          }
> 
> ...
> 
> 
> 
> 
> public class MemGameButtonData extends ButtonData{
> 
>     private String defaultURL;
>     private String buttonURL;    
>     
>     public MemGameButtonData(String URLPadrao, String URLBotao){
>         
>         super();
>         this.buttonURL = URLBotao;
>         this.defaultURL = URLPadrao;        
>         setDefaultURL();        
>     }
>     
>     public void setDefaultURL(){        
>         this.setIcon(defaultURL);
>     }
>     
>     public void setButtonURL(){        
>         this.setIcon(buttonURL);
>     }    
>     
>     public Image getButtonURL(){        
>         return this.getIcon();
>     }
> }
> 
> 
> Cheers,
> Luiz Gustavo
> 
> 
> 
> 
> 
> 
> 2010/12/8 Greg Brown <gk...@verizon.net>
> The leading slash is now required in BXML. A leading "@" symbol means "relative to the current file", whereas a leading "/" means relative to the class path (i.e. absolute).
> 
> However, you appear to be loading your images programmatically rather than in markup - what exactly are you doing with the defaultImage value? There may be an easier way to accomplish what you need.
> 
> G
> 
> 
> On Dec 8, 2010, at 7:21 PM, Luiz Gustavo wrote:
> 
>> Hi,
>> 
>> 
>> I had writen an application using Pivot 1.5.2 that used a gif image for a button, and in the Java source I used the path without a "/" in the begining of the path, like this:
>> 
>> private String defaultImage = "img/default.gif";
>> 
>> Now, rewriting the application in Pivot 2.0 I had to put the "/" in the begining of the path (otherwise I receive a NullPointerException that tells me the IconURL is null):
>> 
>> private String defaultImage = "/img/default.gif";
>> 
>> Are the images referenced by an absolute path now, and not relative to the bxml script?
>> 
>> -- 
>> Luiz Gustavo S. de Souza
>> 
>> http://luizgustavoss.wordpress.com
>> http://luizgustavoss.blogspot.com
>> http://twitter.com/lugustso
> 
> 
> 
> 
> -- 
> Luiz Gustavo S. de Souza
> 
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso


Re: Image Path

Posted by Luiz Gustavo <lu...@gmail.com>.
Hi Greg,

I use the image in the memory game that I've sended to you.
I created a specialized ButtonData that receives two Strings in its
constructor. One of then is de default image, that apears when the button is
"closed" and the another is the image that apears when the user clicks the
button.
I have to do it programmatically 'cause I have to randomize the image
locations.

I knew about the "@" symbol in the bxml file... but I just had the error
mentioned... in the source using Pivot 1.5.2 I didn't have to use the
leading "/" in the path:


...
private String defaultImage = "/img/default.gif";
...

for(int aux = 0; aux < 36; aux++){
             // *images36[aux]* contains the randomized images
             buttons[aux].setButtonData(new MemGameButtonData(*defaultImage*,
*images36[aux]*));
             buttons[aux].setEnabled(true);
         }

...




public class MemGameButtonData extends ButtonData{

    private String defaultURL;
    private String buttonURL;

    public MemGameButtonData(String URLPadrao, String URLBotao){

        super();
        this.buttonURL = URLBotao;
        this.defaultURL = URLPadrao;
        setDefaultURL();
    }

    public void setDefaultURL(){
        this.setIcon(defaultURL);
    }

    public void setButtonURL(){
        this.setIcon(buttonURL);
    }

    public Image getButtonURL(){
        return this.getIcon();
    }
}


Cheers,
Luiz Gustavo






2010/12/8 Greg Brown <gk...@verizon.net>

> The leading slash is now required in BXML. A leading "@" symbol means
> "relative to the current file", whereas a leading "/" means relative to the
> class path (i.e. absolute).
>
> However, you appear to be loading your images programmatically rather than
> in markup - what exactly are you doing with the defaultImage value? There
> may be an easier way to accomplish what you need.
>
> G
>
>
> On Dec 8, 2010, at 7:21 PM, Luiz Gustavo wrote:
>
> Hi,
>
>
> I had writen an application using Pivot 1.5.2 that used a gif image for a
> button, and in the Java source I used the path without a "/" in the begining
> of the path, like this:
>
> private String defaultImage = "img/default.gif";
>
> Now, rewriting the application in Pivot 2.0 I had to put the "/" in the
> begining of the path (otherwise I receive a NullPointerException that tells
> me the IconURL is null):
>
> private String defaultImage = "/img/default.gif";
>
> Are the images referenced by an absolute path now, and not relative to the
> bxml script?
>
> --
> Luiz Gustavo S. de Souza
>
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso
>
>
>


-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Image Path

Posted by Greg Brown <gk...@verizon.net>.
The leading slash is now required in BXML. A leading "@" symbol means "relative to the current file", whereas a leading "/" means relative to the class path (i.e. absolute).

However, you appear to be loading your images programmatically rather than in markup - what exactly are you doing with the defaultImage value? There may be an easier way to accomplish what you need.

G

On Dec 8, 2010, at 7:21 PM, Luiz Gustavo wrote:

> Hi,
> 
> 
> I had writen an application using Pivot 1.5.2 that used a gif image for a button, and in the Java source I used the path without a "/" in the begining of the path, like this:
> 
> private String defaultImage = "img/default.gif";
> 
> Now, rewriting the application in Pivot 2.0 I had to put the "/" in the begining of the path (otherwise I receive a NullPointerException that tells me the IconURL is null):
> 
> private String defaultImage = "/img/default.gif";
> 
> Are the images referenced by an absolute path now, and not relative to the bxml script?
> 
> -- 
> Luiz Gustavo S. de Souza
> 
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso