[java] Java 2D query

Started by
4 comments, last by MrWugga 16 years, 8 months ago
Here's a quick JAVA 2D question. I'm running Wicket and want to dynamically generate/modify images sent to the browser. Wicket supplies the following method to override.

protected boolean render(Graphics2D graphics)
			        {
                        try {
                            BufferedImage image = ImageIO.read(new File("C:\\Projets\\randoweb\\src\\com\\tentelemed\\randoweb\\ui\\rando.png"));

                            BufferedImage bi= new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
                            Graphics2D g2 = bi.createGraphics();
                            g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                                        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
                            Rectangle2D.Double rect = new Rectangle2D.Double(0,0,32,32);
                            g2.fill(rect);
                            //g2.drawImage(image,0,0,null);


                            graphics.drawImage(bi, 0, 0, null);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
				        return true;
                    }
However this doesn't display my PNG file with the surrounding transparency. All I get is a black rectangle. Help! Many Thanks Anthony
Advertisement
is it maybe because you have the line to draw 'image' onto 'bi' commented out?
Well normally I shouldn't see anything with that code as bi is cleared via g2 however I get a black square... I tried the same thing clearing graphics as well but to no avail.

What happens when you change the code to this?
(haven't pasted the complete method)

                           g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, 1.0f));Rectangle2D.Double rect = new Rectangle2D.Double(0,0,32,32);g2.fill(rect);g2.drawImage(image,0,0,null);



As far as I know AlphaComposite.CLEAR draws nothing on the combined image.
With that code I get an entirely white image without any transparency
Try this:

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));


Tested and got this working for me.

This topic is closed to new replies.

Advertisement