Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Image Transparency and drawing into images
Image Transparency and drawing into images [message #440462] Sun, 01 August 2004 05:12 Go to next message
Eclipse UserFriend
Originally posted by: jameshoward.mac.com

Hi all,

I'm trying to draw text at arbitrary angles for labels on a map. This
is not possible in swt, but I wrote an AffineTransform class and an
image transform method so I can draw the text into an Image and then
transform the Image and then draw it. This all works great except for
one thing, I can't seem to draw the text into an Image with nothing but
a transparent background, I always end up with a white background (the
white background corresponds to the original bounds of the text, so for
instance if you try the affine transform on a rectangle, you won't see
the problem). I've included a code snippet that illustrates the
problem (snippet #1). Note that the problem isn't with the GC or the
text drawing or anything like that, the problem is that as soon as I
create an image it has an all white background.

Here's a screenshot (I don't quite have the label positioning worked
out yet, so ignore that):
http://homepage.mac.com/jameshoward/.Pictures/whitebgs.png

and here's what it should look like (shot made with transforms off):
http://homepage.mac.com/jameshoward/.Pictures/transparentbgs .png

So then I try to create an ImageData of the size I need to hold the
text and I walk the pixels there and set them all to fully transparent,
and then I create my Image that I draw into off that. But when the GC
goes through to draw the text it doesn't set the pixels it draws to
opaque again, so I end up with a blank nothing.

Then, I think I'll just do a workaround and specify a mask color, and
then walk the post-draw (but pre-transform) ImageData and everytime I
see the mask color I set the pixel there to fully transparent. But I
haven't had any luck doing this. In code snippet #2 I show what I'm
doing to try to compare the color values in the image to my mask color.

Solving any one of these problems will be sufficient to make the whole
setup work. I'd appreciate any suggestions anyone has.

Thanks,
James

Snippet #1:
package org.placelab.util.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TransparencyTest {

public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 200);
Canvas canvas = new Canvas(shell, SWT.NO_MERGE_PAINTS);
canvas.setSize(200, 200);
canvas.addPaintListener(new PaintListener() {
// draws a white rectangle on a black background. in my opinion it
// ought to draw simply a black background
public void paintControl(PaintEvent pe) {
GC gc = pe.gc;
Color black = display.getSystemColor(SWT.COLOR_BLACK);
gc.setForeground(black);
gc.setBackground(black);
gc.fillRectangle(0, 0, 200, 200);
Image image = new Image(display, 20, 50);
gc.drawImage(image, 10, 10);
image.dispose();
}
});

shell.open();

while(!shell.isDisposed()) {
if(display.readAndDispatch()) {
display.sleep();
}
}

}
} // end snippet #1


Snippet #2:
// assume the following things are declared and set properly
Color maskColor;
ImageData textInImage;

// now I try to walk the textInImage to find maskColor
RGB maskRGB = maskColor.getRGB();
for(int x = 0; x < textInImage.width; x++) {
for(int y = 0; y < textInImage.height; y++) {
int px = textInImage.getPixel(x, y);
RGB rbg = textInImage.palette.getRGB(px);
if(rgb.equals(maskRGB)) textInImage.setAlpha(x, y, 0);
}
}
Re: Image Transparency and drawing into images [message #440467 is a reply to message #440462] Mon, 02 August 2004 01:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jameshoward.mac.com

Nevermind. I solved it using masking, but I have no idea why snippet
#2 didn't work, I did a variation on the theme in snippet #2 and got it
working. For the curious, here is my solution (with surrounding
context code):

Snippet #3:
// assume buffer contains the pre-transform image

ImageData temp = buffer.getImageData();

if(this.getMaskColor() != null) {
int maskRed = this.getMaskColor().getRed();
int maskBlue = this.getMaskColor().getBlue();
int maskGreen = this.getMaskColor().getGreen();

int[] lineData = new int[temp.width];
for (int y = 0; y < temp.height; y++) {
temp.getPixels(0,y,temp.width,lineData,0);
// Analyze each pixel value in the line
for (int x=0; x<lineData.length; x++){
// Extract the red, green and blue component
int pixelValue = lineData[x];
int r = temp.palette.getRGB(pixelValue).red;
int g = temp.palette.getRGB(pixelValue).green;
int b = temp.palette.getRGB(pixelValue).blue;
if (r == maskRed && g == maskGreen && b == maskBlue) {
temp.setAlpha(x,y,0);
} else {
// this is important
temp.setAlpha(x, y, 255);
}
}
}
}
Re: Image Transparency and drawing into images [message #440468 is a reply to message #440462] Mon, 02 August 2004 01:48 Go to previous message
Eclipse UserFriend
Originally posted by: jameshoward.mac.com

Nevermind. I solved it using masking, but I have no idea why snippet
#2 didn't work, I did a variation on the theme in snippet #2 and got it
working. For the curious, here is my solution (with surrounding
context code):

Snippet #3:
// assume buffer contains the pre-transform image

ImageData temp = buffer.getImageData();

if(this.getMaskColor() != null) {
int maskRed = this.getMaskColor().getRed();
int maskBlue = this.getMaskColor().getBlue();
int maskGreen = this.getMaskColor().getGreen();

int[] lineData = new int[temp.width];
for (int y = 0; y < temp.height; y++) {
temp.getPixels(0,y,temp.width,lineData,0);
// Analyze each pixel value in the line
for (int x=0; x<lineData.length; x++){
// Extract the red, green and blue component
int pixelValue = lineData[x];
int r = temp.palette.getRGB(pixelValue).red;
int g = temp.palette.getRGB(pixelValue).green;
int b = temp.palette.getRGB(pixelValue).blue;
if (r == maskRed && g == maskGreen && b == maskBlue) {
temp.setAlpha(x,y,0);
} else {
// this is important
temp.setAlpha(x, y, 255);
}
}
}
}
Previous Topic:RCP Error
Next Topic:Contribution Item fails
Goto Forum:
  


Current Time: Thu Apr 25 19:07:34 GMT 2024

Powered by FUDForum. Page generated in 0.03310 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top