ImageIcon + ImageIcon = ImageIcon - java

I have two ImageIcons and I want to create a third ImageIcon which has nr 2 drawn upon nr 1.
How would I best do that?

The following code takes an Image from two ImageIcons and creates a new ImageIcon.
The image from the second ImageIcon is drawn on top of the image from the first, then the resulting image is used to make a new ImageIcon:
Image img1 = imageIcon1.getImage();
Image img2 = imageIcon2.getImage();
BufferedImage resultImage = new BufferedImage(
img1.getWidth(null), img1.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resultImage.createGraphics();
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon resultImageIcon = new ImageIcon(resultImage);
Edit
(Fixed some errors, added transparency support.)
For allowing transparency, the BufferedImage.TYPE_INT_ARGB can be used for the image type in the constructor, rather than BufferedImage.TYPE_INT_RGB which does not have an alpha channel.

Related

How do I ignore the alpha content when loading an Image off disk into a BufferedImage

Currently, when I load an image off disk,
BufferedImage img = ImageIO.read(new File("myfile.png");
The resulting color space is one of ARGB. What I'd prefer is a plain old RGB, but without having to do the filtering myself.
Is there a way to open an image in a certain color mode?
I'm not aware of a way to open it with a specific color format, but you can create a new BufferedImage of the desired format and draw the old image on to it:
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img2.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
Any transparent parts in the original image will be drawn over a black background in the new image. If you'd prefer a different background, you can insert these lines before the drawImage call:
g.setColor(Color.white); // or whichever
g.fillRect(0, 0, img.getWidth(), img.getHeight());

How resize javax.swing.Icon

I try to resize IUManager icon. But i cant do it correct. My code looks like:
// label
ErrorDetails = new javax.swing.JLabel();
// icon
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage bi = new BufferedImage(
205,
250,
BufferedImage.SCALE_DEFAULT);
Graphics2D g = bi.createGraphics();
g.scale(205,205);
// paint new graphics
icon.paintIcon(null,g,250,250);
g.dispose();
// set resized UIManage icon
ErrorDetails.setIcon(icon);
but icon have still same size
You are attempting to paint the Icon onto the BufferedImage. Therefore you would need to create a new Icon using the BufferedImage>
ImageIcon scaled = new ImageIcon(bi);
ErrorDetails.setIcon(scaled);
Also follow Java naming conventions. Variable names should NOT start with an upper case character. "ErrorDetails" should be "errorDetails".

display an image in awt

I have a frame and a buffer values of the image. I couldn't display the image in the frame. The code that I used is as follows:
byte [] payload = new byte[payload_length];
rtp_packet.getpayload(payload);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(payload, 0, payload_length);
icon = new ImageIcon(image);
iconLabel.setIcon(icon);
I also tried adding it directly to the frame using the code :
f.setIconImage(image);
now how to display the image? and why it is not working?
You want to use the graphics object. Your awt frame should already have one and you call your graphics object via ...
BufferedImage img = javaImage; // You replace this with your image
Graphics g = this.getGraphics(); // this is what you need to call
g.drawImage(img, 0, 0, null); // you then call draw image
In your case you simply do
g.drawImage(image, 0, 0, null); // you can look up the parameters
That should do it for you.

Drawing two overlayed images

I'm trying to draw 2 images, one on top of the other. The 1'st image is an arrow (that should appear like a header in the final image). The 1'st image (arrow) is 32x32 px while the 2'nd is 24x24.
Ideally I would like to draw the 2'nd image on top of the 1'st, starting from the right-bottom corner of the 1'st image.
Currently I'm using such code
// load source images
BufferedImage baseImage = ImageIO.read(new File(baseImg.getFileLocation()));
BufferedImage backgroundImage = ImageIO.read(new File(backgroundImg.getFileLocation()));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(baseImage.getWidth(), backgroundImage.getWidth());
int h = Math.max(baseImage.getHeight(), backgroundImage.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);
int index = baseImg.getFileLocation().lastIndexOf(".png");
String newFileName = baseImg.getFileLocation().substring(0, index);
// Save as new image
ImageIO.write(combined, "PNG", new File(newFileName + "_combined.png"));
but this won't quite work for me because the end result is a 32x32 image with the 2nd image being drawn only.
Any help is appreciated.
Thanks !
It looks like the issue here is you are drawing the 32x32 background image last, meaning it will be printed on top of the other image making it seem as if the 24x24 image was never drawn at all.
If you swap these two lines around, you should see both images. From:
g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);
to:
g.drawImage(backgroundImage, 0, 0, null);
g.drawImage(baseImage, 0, 0, null);
However this will draw the 24x24 image in the top-left corner, and you said you'd like it in the bottom-right. This can be done with some basic subtraction:
g.drawImage(baseImage, w - baseImage.getWidth(), h - baseImage.getHeight(), null);

Keeping array of ImageIcon or BufferedImage?

My Java program loads a bunch of image on startup. For now for each image I call this piece of code. Eventually I get an array of ImageIcon, which would be used in user interface.
InputStream is = getClass().getClassLoader().getResourceAsStream(imagePath);
bimage = ImageIO.read(is);
is.close();
Image img = bimage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
But now I want to implement a method which would get an object from array and add title in specified language. So I have to use Graphics2D.drawString(). But ImageIcon does not have createGraphics() method. Is there a way I can get Graphics from ImageIcon?
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red);
g.setFont(new Font( "SansSerif", Font.BOLD, 25 ));
g.drawString("TEST", 25, 25);
Now is the question: should I keep array of ImageIcon or BufferedImage?

Categories

Resources