Oh, sorry, mean to include that if you add this to a quickstart, you can
test with URL:

http://localhost:8080/foo
http://localhost:8080/foo?text=fff&width=200

-- 
Jeremy Thomerson
http://www.wickettraining.com


On Fri, Dec 26, 2008 at 12:48 AM, Jeremy Thomerson <
jer...@wickettraining.com> wrote:

> I spent a while playing with this, and indeed - it is much more difficult
> than it should be (or I missed something that one of the core devs can point
> out.  I really hope one of them can review this and point me to an easier
> way.  But in the meantime, this should work for you.
>
> First, let's start with the basics.  In your web application class, you'll
> add a shared resource and mount it to whatever path you want:
>
> public static final String IMAGE_KEY = "jrtimage".intern();
>
> @Override
> protected void init() {
>     super.init();
>     getSharedResources().add(IMAGE_KEY, new MyImage());
>     mountSharedResource("foo", new
> ResourceReference(IMAGE_KEY).getSharedResourceKey());
> }
>
> Then, you can include it in your page:
> JAVA: add(new Image("img", new
> ResourceReference(WicketApplication.IMAGE_KEY)));
> HTML: <img wicket:id="img" />
>
> Okay, so what was that "MyImage"?  It is the class that creates your
> dynamic image.  Read the long comments in the class explaining the nuances
> of doing it this way.
> private static class MyImage extends DynamicImageResource {
>     private static final long serialVersionUID = 1L;
>     ThreadLocal<String> mText = new ThreadLocal<String>();
>     ThreadLocal<Integer> mWidth = new ThreadLocal<Integer>();
>     @Override
>     public IResourceStream getResourceStream() {
>      // see note below on why we get parameters in this method
>         int width = 300;
>         String text = "Hello World!";
>         String w = ((String[]) getParameters().get("width"))[0];
>         if (w != null && "".equals(w.trim()) == false) {
>             try {
>                 width = Integer.parseInt(w);
>             } catch(NumberFormatException nfe) {
>                 //no-op
>             }
>         }
>         String t = ((String[]) getParameters().get("text"))[0];
>         if (t != null && "".equals(t.trim()) == false) {
>             text = t;
>         }
>         mText.set(text);
>         mWidth.set(width);
>         return super.getResourceStream();
>     }
>     @Override
>     protected byte[] getImageData() {
>      /*
>       Unfortunately by the time we get here, the request target has already
>       been switched, and is no longer an instance of
> ISharedResourceRequestTarget
>       and the getParameters() method no longer returns any parameters,
> presumably
>       because the request target has been switched to a
> ResourceStreamRequestTarget
>
>       Therefore, we must override getResourceStream above (while params
> still available)
>       so that we can get the parameters there and store them in a
> ThreadLocal
>
>       NOTE: I haven't tested the ThreadLocal here at all.  Presumably it
> will be okay.
>       I don't think you could use regular member fields because multiple
> requests
>       could be operating on this object at the same time - I'm pretty sure,
> but haven't
>       double checked.
>      */
>         BufferedImage img = new BufferedImage(mWidth.get(), 100,
> BufferedImage.TYPE_INT_RGB);
>         Graphics gr = img.createGraphics();
>         gr.setColor(Color.BLACK);
>         gr.fillRect(0, 0, mWidth.get(), 100);
>         gr.setFont(new Font("Arial", Font.PLAIN, 16));
>         gr.setColor(Color.WHITE);
>         gr.drawString(mText.get(), 25, 25);
>         gr.drawString("Width: " + mWidth.get(), 25, 45);
>         mText.set(null);
>         mWidth.set(null);
>         return toImageData(img);
>     }
> }
>  --
> Jeremy Thomerson
> http://www.wickettraining.com
>
>
> On Thu, Dec 25, 2008 at 11:03 PM, smallufo <small...@gmail.com> wrote:
>
>> Well ,
>> What I need is parsing URL and generating a corresponding image from
>> BufferedImage ,
>> not a bookmarkable link to internal file resource...
>>
>> for example :
>> http://localhost/app/myImage/text/Hello/width/500
>>
>> This will generate a 500x500 png , containing a "Hello" String.
>>
>
>
>
>

Reply via email to