Adding two BufferedImages together?

Is there anyway to "for lack of a better wordadd two bufferedImages together?

For example if I have a JLabel with an ImageIcon thats a red square 32 x 32 pixels and another JLabel with an ImageIocn thats a green square also 32 x 32 pixels. Can I create a new ImageIcon thats 64 x 32 pixels that is the red square and green square together?

Something likenew BufferedImage(image1, image2, concanate_xaxis);

Thanks,

Cathal.

cathal87

[509 byte] By [cathal87a] at [2007-10-3 8:42:45]
# 1
I haven't tried it but maybe using these methods:Create an image of the appropriate size andcall getData() on each image and put into the new image:setData(Raster r)Again, I don't know if this is the right way just an idea?
zadoka at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 2
a) Create a BufferedImage of the correct sizeb) get the Graphics object of the image and draw the first image at (0, 0)c) then draw the second image at the appropriate offsets
camickra at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 3

Ive tryed to impliment your above suggestion camickr, but all I can seem to get as output is a black image.

Heres some code that demonstrates my problem.

Once the following code is compiled and run an image is created using all of the JLabels ImageIcons from the JFrame and its saved into a file at "C:/Image.png". Obviously this would have to be changed for testing purposes depending on ones OS and Drive letter preferences.

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.*;

public class Example2

{

static JFrame frame;

static JLayeredPane layeredPane;

public static void main(String[] args)

{

frame = new JFrame("Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setSize(320, 320);

frame.setLocationRelativeTo(null);

frame.getContentPane().setLayout(null);

layeredPane = new JLayeredPane();

layeredPane.setOpaque(true);

layeredPane.setLayout(null);

layeredPane.setBounds(0, 0, 320, 320);

frame.setContentPane(layeredPane);

for(int i = 0; i < 32; i ++)

{

for(int j = 0; j < 32; j ++)

{

JLabel label = new JLabel(new ImageIcon("images/grass.gif"));

layeredPane.add(label, new Integer(0));

label.setBounds(i * 32, j * 32, 32, 32);

}

}

frame.setVisible(true);

createAndSaveImage();

}

public static void createAndSaveImage()

{

BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB);

Component[] components = layeredPane.getComponentsInLayer(0);

int count = 0;

for(int i = 0; i < 32; i ++)

{

for(int j = 0; j < 32; j ++)

{

JLabel label = (JLabel)components[count];

label.getGraphics().drawImage(image, i, j, 32, 32, null);

count ++;

}

}

try

{

ImageIO.write(image, "png", new File("C:/Image.png"));

}

catch(Exception exexe)

{

}

}

}

cathal87a at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 4

> but all I can seem to get as output is a black image.

Thats because you are not drawing on the buffered image.

> a) Create a BufferedImage of the correct size

In your example you have 32 images each of width 32, so why is the buffer only 320 pixels. (it should be 32 * 32).

> b) get the Graphics object of the image and draw the first image at (0, 0)

I guess I should have said - "get the Graphics object of the newly created BufferedImage"....

> label.getGraphics().drawImage(image, i, j, 32, 32, null);

You are trying to draw the empty buffered image onto the existing label. You code should be the other way around:

// BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB);

BufferedImage image = new BufferedImage(1024, 1024, BufferedImage.TYPE_INT_RGB);

Graphics graphics = image.getGraphics();

Component[] components = layeredPane.getComponentsInLayer(0);

int count = 0;

for(int i = 0; i < 32; i ++)

{

for(int j = 0; j < 32; j ++)

{

JLabel label = (JLabel)components[count];

//label.getGraphics().drawImage(image, i, j, 32, 32, null);

ImageIcon icon = (ImageIcon)label.getIcon();

graphics.drawImage(icon.getImage(), i * 32, j * 32, 32, 32, null);

count ++;

}

}

Of course the code you posted isn't even related to your question. You question was "how to I add to buffered images together".

The code you posted is for the question - "how to a create an image of the components on the screen.

That question is easily answered by searching the forum for my "ScreenImage" class which does just that. It creates an image from any component on the screen. Although I must admit I haven't tested it on a LayeredPane yet, but I see no reason why it wouldn't work.

camickra at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 5

>Of course the code you posted isn't even related to your question. You >question was "how to I add to buffered images together".

>The code you posted is for the question - "how to a create an image of the >components on the screen.

>That question is easily answered by searching the forum for my >"ScreenImage" class which does just that. It creates an image from any >component on the screen. Although I must admit I haven't tested it on a >LayeredPane yet, but I see no reason why it wouldn't work.

I actually need to save each layer from the JLayeredPane as a seperate image, this is the reason I did not use the "ScreenImage" class.

Can I ask you one last question on this topic tho? I currently have a piece of code that will save each layer of a JLayeredPane into a seperate image.

For example if the JLayeredPane has 3 layers it save the images layer1.png, layer2.png and layer3.png.

These 3 images when painted in their respective layers create a map for a game im currently working on. I would like to be able to save all 3 of these layers into one file called "map1.map" or something simular. So that when I want to edit a map using my editor I dont have to prompt the user to open layer1.png then layer2.png and then layer3.png.

Is there a Java way to do this?

cathal87a at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 6

> I actually need to save each layer from the JLayeredPane as a

> seperate image, this is the reason I did not use the "ScreenImage" class.

Did you try it? Like I said I've never tried it on a LayeredPane but I see no reason why it shouldn't work.

> Is there a Java way to do this?

If you have one large image then its not a problem. Is this not your original question. How to merger images. Then when you load the image you will need to separate them.

camickra at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 7

>Did you try it? Like I said I've never tried it on a LayeredPane but I see no reason why it shouldn't work.

Ive used ScreenImage before on a simular project and I found that it ocassionally dosent capture all of the specified component, also I found that if another window is dragged ontop of the component during capture like for instance a file save dialog part of it ends up getting saved along with the underneat component. Perhapse I was using it in an unsafe way, Im sure its realitivly realiable. But with my above method of getting all of the images in the seperate components via getIcon im gaurented to get a perfect container capture every time. Which is vital for my current project.

>If you have one large image then its not a problem. Is this not your >original question. How to merger images. Then when you load the >image you will need to separate them.

I should have specified more clearly, its not just 2 images files I wish to store in the one file, I also need to save 2 more files which contain a seperate boolean value for every tile present in the 2 layers. If the character is able to walk on a specific tile a 1 will be present in the respective location, if the character is unable to walk on this tile a 0 is present.

So I need one file called map1.map "for example" that will hold the images for the 2 layers and the boolean data for the 2 layers.

I just want to be able to open this one file and extract the 4 others so I can build the map.

cathal87a at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...
# 8

Is the way to do this create a new File called map1.map.

Use a fileReader on the 4 files I want to save into this new file.

Store the first file into map1.map followed by some termination symbol lets say $ then do this four all for files.

Then when I open map1.map read all data from the file untill the first $ and save that as layer1.png then read everything from that point on untill the next $ and save that as layer2.png untill all four files are saved.

Would this work?

cathal87a at 2007-7-15 3:51:14 > top of Java-index,Desktop,Core GUI APIs...