Skip to main content

creating an image with transparent background

1 reply [Last post]
towi
Offline
Joined: 2009-09-03

Hello,
I want to create a custom component which shows a colorwheel, so i created myComponent which extends from component and override the paint() method. For a better performance i want to draw the colorwheel only the first time i use myComponent in an image and show it each time the repaint() method is called. I want to set the backgroud from the image transparent so that only the colorwheel is visible. I do it the following way:

public void paint(Graphics g){
Image cwImage = Image.createImage(getWidth(), getHeight());
Graphics cwg = cwImage.getGraphics();

//draw black background of colorwheel
cwg.setColor(0);
Rectangle cwBorderBound = getRectangleForCircle(center, cwRadius+padding);
cwg.fillArc(cwBorderBound.getX(), cwBorderBound.getY(), cwBorderBound.getSize().getWidth(), cwBorderBound.getSize().getHeight(), 0 ,360);

//creates a mask for alpha channel
Image mask = Image.createImage(getWidth(), getHeight());
Graphics mg = mask.getGraphics();
mg.setColor(0x0000ff); //only blue values set as mentioned by javadoc
mg.fillArc(cwBorderBound.getX(), cwBorderBound.getY(), cwBorderBound.getSize().getWidth(), cwBorderBound.getSize().getHeight(), 0 ,360);

.... do some drawing on cwg (colorWheelGraphic)

cwImage.applyMask(mask.createMask(), 0, 0);
g.drawImage(cwImage, 0, 0);

}

I always get the image of the colorwheel with a white surrounding rectangle instead of only the colorwheel.
When i do the drawing operations directly in the Graphics object g then everything is correct, but the performance is poor because the colorwheel has very expensive drawing operations.

Can anyone tell me how i can solve this problem?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

Your code seems about right only you need to get the response value from applyMask which is the end result... You simply discard it, the input image is unmodified since it might be an immutable image (e.g. jpeg loaded from the net etc.).