I
Icarus
I have recently tried to create a BufferedImage by combining several
other Images using Graphics2D.drawImage(). But every area in the new
picture not occupied by another picture gets painted black.
As I need the picture for a mouse cursor, among other things, I
require these areas to remain transparent. I already tried to paint a
transparent rectangle over the image first, but it doesn't change the
outcome. I tried to export the created image to my system for
checking, but this doesn't work either.
I have created some sample code replicating the problem. Included (but
commented out) is my attempt with the transparent rectangle.
You will need a picture about 40x40 pixels big for the code to run,
however.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CreateBufferedImage extends JFrame{
public CreateBufferedImage(){
// Create images
BufferedImage picture = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
java.net.URL url = this.getClass().getResource("image.png");
ImageIcon smallerPicture = new ImageIcon(url);
// Retrieve graphics-object
Graphics2D grafik = picture.createGraphics();
// Try to clear the background
//
grafik.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
0.0f));
// Rectangle2D.Double rect =
// new
Rectangle2D.Double(0,0,picture.getWidth(),picture.getHeight());
// grafik.fill(rect);
//
grafik.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1.0f));
// Draw the smaller into the bigger image
grafik.drawImage(smallerPicture.getImage(), 40, 40,
Color.white, null);
// Put the image onto a JLabel for visual presentation
JLabel labelWithPicture = new JLabel(new ImageIcon(picture));
// Put the JLabel on this JFrame and make it visible
this.setSize(new Dimension(150, 150));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(labelWithPicture);
this.setVisible(true);
// Get the picture on the drive for controlling issues
try {
ImageIO.write(picture, "jpg", new File("test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
CreateBufferedImage frame = new CreateBufferedImage();
}
}
other Images using Graphics2D.drawImage(). But every area in the new
picture not occupied by another picture gets painted black.
As I need the picture for a mouse cursor, among other things, I
require these areas to remain transparent. I already tried to paint a
transparent rectangle over the image first, but it doesn't change the
outcome. I tried to export the created image to my system for
checking, but this doesn't work either.
I have created some sample code replicating the problem. Included (but
commented out) is my attempt with the transparent rectangle.
You will need a picture about 40x40 pixels big for the code to run,
however.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CreateBufferedImage extends JFrame{
public CreateBufferedImage(){
// Create images
BufferedImage picture = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
java.net.URL url = this.getClass().getResource("image.png");
ImageIcon smallerPicture = new ImageIcon(url);
// Retrieve graphics-object
Graphics2D grafik = picture.createGraphics();
// Try to clear the background
//
grafik.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
0.0f));
// Rectangle2D.Double rect =
// new
Rectangle2D.Double(0,0,picture.getWidth(),picture.getHeight());
// grafik.fill(rect);
//
grafik.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1.0f));
// Draw the smaller into the bigger image
grafik.drawImage(smallerPicture.getImage(), 40, 40,
Color.white, null);
// Put the image onto a JLabel for visual presentation
JLabel labelWithPicture = new JLabel(new ImageIcon(picture));
// Put the JLabel on this JFrame and make it visible
this.setSize(new Dimension(150, 150));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(labelWithPicture);
this.setVisible(true);
// Get the picture on the drive for controlling issues
try {
ImageIO.write(picture, "jpg", new File("test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
CreateBufferedImage frame = new CreateBufferedImage();
}
}