Black spaces in a newly created BufferedImage

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();
}
}
 
I

Icarus

This can't be for real... I have tried this out for hours, the right
after posting this question changed the type of the BufferedImage from
TYPE_INT_RGB to TYPE_INT_ARGB and now it works!

Sorry for bothering.
 
M

Mark Space

Tom said:
Sometimes, the act of asking a question is what crystallises an answer
in your own mind. Sages speak of the 'cardboard programmer':

http://c2.com/cgi/wiki?CardboardProgrammer

I think the corollary to this is don't' spend hours on a problem. Get
help right away! If nothing else, the act of phrasing your question
will lead directly to a solution.

This was actually part of a job interview question, lo these many years
ago when I interviewed with a large engineering company as a new grad.
"How long to you like work on a problem before asking for help?" My
answer (essentially correct) "About three minutes."
 
I

Icarus

Wow! This wasn't the kind of reply I was expecting for basically
getting you all excited about nothing. Thank you. I will give this
method some thought. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top