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
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);
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...
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