Using the dogbane's answer,
You could adapt the code easily to do what you want.
Using Graphics.fillRect
to create a rectangle. Just need to set the color to black but you will need to manage the height and width in code.
g.setColor(Color.BLACK);
//Black rectangle on the left edge
g.fillRect(x, y, rectWidth, rectHeight);
x += rectSize;
for(String image : images){
BufferedImage bi = ImageIO.read(new File(image));
g.drawImage(bi, x, y, null);
x += bi.getWidth();
//Black rectangle on the right
g.fillRect(x, y, rectWidth, rectHeight);
x += rectSize;
}
You will see an rectWidth
, rectHeight
to generate the black square. You have two solutions here,
- set it yourself (a parameter that you give from the batch)
- iterate on the image first to get the biggest height of this batch (carefull on the perfs)
Note :
- to improve this, you might want to center the image if those have some different sizes ... this will requires somt update to add a black square up above and below. But it's a nice exercice ;)
- The default background is black, so you don't need to create the filled rectangle for a black one (default byte are 0 so black #000000)
- Stangely, a Windows XP tell me the PNG is not valid but on a Seven it works fine, might need to investigate this.
UPDATE :
Since we need the result image height and width on the beginning (didn't though about it), we answer some question
- It is easy to update the background color to don't bother with it later (paint the full image at the beginning
- We are force to iterate the image first to do the sum of there width
- We can get the maximum height in the same time