You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Michael O'Cleirigh <mi...@rivulet.ca> on 2009/10/28 16:37:51 UTC
Re: How to have a RenderedDynamicImageResource with transparent
background?
Hi Reinhard,
You need to seperate your image generating logic from the wicket Image
Resource.
I'd create a test program or unit test to build verify that the image
you want is being built correctly and then bind it into wicket.
For example you can just use a BufferedDynamicImageResource with the
image you generate.
Here is some code I have for writing a string into an image. If you
pass backgroundColor = null it will emit a transparent background with
foregroundColor text on top.
public static BufferedImage writeString(String text, int height, int
width, Font font, Color backgroundColor, Color foregroundColor) {
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = bi.createGraphics();
graphics2D.setFont(font);
FontMetrics fontMetrics = graphics2D.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(text);
int stringHeight = fontMetrics.getAscent();
if (backgroundColor != null) {
graphics2D.setBackground(backgroundColor);
graphics2D.clearRect(0, 0, width, height);
}
graphics2D.setPaint(foregroundColor);
graphics2D.drawString(text, (width - stringWidth) / 2, height
/ 2 + stringHeight / 4);
return bi;
}
These Wiki references that should also help you:
http://cwiki.apache.org/WICKET/how-to-create-dynamic-image-overlays.html
http://cwiki.apache.org/WICKET/how-to-stamp-an-image-template-with-context-specific-details.html
Regards,
Mike
> I like to have a RenderedDynamicImageResource with a transparent background.
> I tried serveral things google came up with. But none worked so far.
> Let me show you what I do:
>
> public RenderedDynamicImageResource getCity(final Long cityId){
> RenderedDynamicImageResource image = new
> RenderedDynamicImageResource(500, 570) {
> private static final long serialVersionUID = 1L;
>
> @Override
> protected boolean render(Graphics2D mapGraphic) {
> return renderCity(mapGraphic, cityId);
> }
> };
>
> return image;
> }
>
>
> protected boolean renderCity(Graphics2D mapGraphic, final Long cityId) {
>
> ....
>
> // Prepare g2 for transparency
> //one of the approches I tried... without luck
>
>
> mapGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC,
> 0.9f));
> Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, 500,
> 570);
> mapGraphic.fill(rect);
>
> .....
>
> // Here I draw some shapes into the mapGraphic
>
> ....
>
> //in the end I'd like to have a png with a transparent background
> and some shapes on it
> }
>
>
>
> If anyone can provide me with help or even a working example, that would be
> greatly apreciated.
>
> cheers
> reinhard
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org