creating an ImageIcon from others ImageIcon

Hi,

I want to write a method that create an ImageIcon object from two ImageIcon objects passed.

ImageIcon createImageIcon(ImageIcon img1, ImageIcon img2) {

ImageIcon img;

...

...

return img;

}

The returned ImageIcon displays as the two img1 and img2 placed side by side.

Thanks very much,

Charly

[362 byte] By [amaglio.ca] at [2007-10-3 5:14:15]
# 1

Maybe something like this untested code:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = image.createGraphics();

icon1.paintIcon(...);

icon2.paintIcon(...);

g2d.dispose();

ImageIcon imageIcon = new ImageIcon(image);

camickra at 2007-7-14 23:20:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

how about

public static ImageIcon createImageIcon(ImageIcon icon1, ImageIcon icon2) {

Image image = new BufferedImage(icon1.getIconWidth() + icon2.getIconWidth(), icon1.getIconHeight() + icon2.getIconHeight(), BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

g.drawImage(icon1.getImage(), 0, 0, icon1.getIconWidth(), icon1.getIconHeight());

g.drawImage(icon2.getImage(), icon1.getIconWidth(), icon1.getIconHeight(), icon2.getIconWidth(), icon2.getIconHeight());

return image;

}

for merging two ImageIcons vertically...

stevopsa at 2007-7-14 23:20:46 > top of Java-index,Desktop,Core GUI APIs...
# 3
Sorry but i don't think so.Finally i found it! Thanks to camickr for his invaluable information even if it was to a different question. http://forum.java.sun.com/thread.jspa?forumID=57&threadID=715330
virtual_gra at 2007-7-14 23:20:46 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks to camickr and stevops,

this is my working method (mainly the camickr code).

public static ImageIcon createImageIcon(ImageIcon icon1, ImageIcon icon2) {

int width = Math.max(icon1.getIconWidth(), icon2.getIconWidth());

BufferedImage image = new BufferedImage(width, icon1.getIconHeight() + icon2.getIconHeight(), BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

//g.drawImage(icon1.getImage(), 0, 0, icon1.getIconWidth(), icon1.getIconHeight(), null);

//g.drawImage(icon2.getImage(), 0, icon1.getIconHeight(), icon2.getIconWidth(), icon2.getIconHeight(), null);

icon1.paintIcon(null, g, 0, 0);

icon2.paintIcon(null, g, 0, icon1.getIconHeight());

g.dispose();

return new ImageIcon(image);

}

Bye

amaglio.ca at 2007-7-14 23:20:46 > top of Java-index,Desktop,Core GUI APIs...
# 5
Another little problem:my sources icons (icon1 and icon2) come from a GIF file with transparent pixels. How to have the background of the created ImageIcon be transparent?Thanks again,Charly
amaglio.ca at 2007-7-14 23:20:46 > top of Java-index,Desktop,Core GUI APIs...