Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Bug in SWT, drawing on partially transparent image?
Bug in SWT, drawing on partially transparent image? [message #451655] Fri, 04 March 2005 04:31 Go to next message
Eclipse UserFriend
Originally posted by: David.A.Price.nokia.com

I'm new to SWT, and playing around with Canvas and transparent images.
I've found a rather puzzling behaviour, which seems to me to be a bug. I
didn't find anything relevant in the bug database or from Google. I'm
using SWT 3.0.1 and Windows 2000 SP4.

When I create a blank Image using a depth 32 ImageData and a non-indexed
PaletteData, where part of the ImageData has had setAlpha called to make
it fully opaque, later drawLine operations on the image only affect the
opaque part of the image. I would expect that lines drawn on the
transparent part of the image would also appear.

I've confirmed that if I create the image using an indexed palette, lines
drawn on the transparent part appear as I'd expect.

Is this a known bug? Is it somewhere documented as expected behaviour? If
not, should I submit a bug report?

Thanks,
David

PS. I've attached sample code. It produces a black rectangle with a red
'V', on a yellow background. I'd expect the red 'V' to continue downwards
to form an 'X'.

----8<----------------------------------

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

public class TestImage3 {
public static void main(String[] args) {
final Display display = new Display();
PaletteData palette =
new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF);
ImageData data = new ImageData(40, 40, 32, palette);
for (int x = 0; x < data.width; ++x) {
for (int y = 0; y < data.height / 2; ++y) {
data.setAlpha(x, y, 255);
}
}
final Image image = new Image(display, data);
GC tempGc = new GC(image);
tempGc.setForeground(display.getSystemColor(SWT.COLOR_RED));
tempGc.drawLine(0, 0, image.getBounds().width,
image.getBounds().height);
tempGc.drawLine(0, image.getBounds().height,
image.getBounds().width, 0);

final Shell shell = new Shell(display);
shell.setText("Test Image 3.");
shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
GC gc = event.gc;
Rectangle rect = shell.getClientArea();
Rectangle bounds = image.getBounds();
gc.drawImage(image,
rect.x + (rect.width - bounds.width) / 2,
rect.y + (rect.height - bounds.height) / 2);
}
});
shell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW) );
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Re: Bug in SWT, drawing on partially transparent image? [message #451658 is a reply to message #451655] Fri, 04 March 2005 08:37 Go to previous messageGo to next message
Eclipse UserFriend
When you call the ImageData.setAlpha() API, it is assumed that you are going
to set the alpha value for each pixel. The alphaData array gets created and
initially all of its values are 0 until you set a different value. Drawing
on the image does not affect the alpha values for the pixels you are drawing
on. Perhaps you should be setting the transparency rather than the alpha
value to get the effect you want:


public static void main(String[] args) {
final Display display = new Display();
PaletteData palette = new PaletteData(0x00FF0000, 0x0000FF00,
0x000000FF);
ImageData data = new ImageData(40, 40, 32, palette);
// pick some colour to be transparent
// anything drawn with this colour will be transparent
data.transparentPixel =
palette.getPixel(display.getSystemColor(SWT.COLOR_BLUE).getR GB());
final Image image = new Image(display, data);
Rectangle bounds = image.getBounds();
GC tempGc = new GC(image);
tempGc.setBackground(display.getSystemColor(SWT.COLOR_BLACK) );
tempGc.fillRectangle(0, 0, bounds.width, bounds.height/2);
tempGc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)) ;
tempGc.fillRectangle(0, bounds.height/2, bounds.width,
bounds.height);
tempGc.setForeground(display.getSystemColor(SWT.COLOR_RED));
tempGc.drawLine(0, 0, bounds.width, bounds.height);
tempGc.drawLine(0, bounds.height, bounds.width, 0);

final Shell shell = new Shell(display);
shell.setText("Test Image 3.");
shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
GC gc = event.gc;
Rectangle rect = shell.getClientArea();
Rectangle bounds = image.getBounds();
gc.drawImage(image,
rect.x + (rect.width -
bounds.width) / 2,
rect.y + (rect.height -
bounds.height) / 2);
}
});
shell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW) );
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

"David Price" <David.A.Price@nokia.com> wrote in message
news:d099sq$3pc$1@www.eclipse.org...
> I'm new to SWT, and playing around with Canvas and transparent images.
> I've found a rather puzzling behaviour, which seems to me to be a bug. I
> didn't find anything relevant in the bug database or from Google. I'm
> using SWT 3.0.1 and Windows 2000 SP4.
>
> When I create a blank Image using a depth 32 ImageData and a non-indexed
> PaletteData, where part of the ImageData has had setAlpha called to make
> it fully opaque, later drawLine operations on the image only affect the
> opaque part of the image. I would expect that lines drawn on the
> transparent part of the image would also appear.
>
> I've confirmed that if I create the image using an indexed palette, lines
> drawn on the transparent part appear as I'd expect.
>
> Is this a known bug? Is it somewhere documented as expected behaviour? If
> not, should I submit a bug report?
>
> Thanks,
> David
>
> PS. I've attached sample code. It produces a black rectangle with a red
> 'V', on a yellow background. I'd expect the red 'V' to continue downwards
> to form an 'X'.
>
> ----8<----------------------------------
>
> import org.eclipse.swt.*;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.graphics.*;
>
> public class TestImage3 {
> public static void main(String[] args) {
> final Display display = new Display();
> PaletteData palette =
> new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF);
> ImageData data = new ImageData(40, 40, 32, palette);
> for (int x = 0; x < data.width; ++x) {
> for (int y = 0; y < data.height / 2; ++y) {
> data.setAlpha(x, y, 255);
> }
> }
> final Image image = new Image(display, data);
> GC tempGc = new GC(image);
> tempGc.setForeground(display.getSystemColor(SWT.COLOR_RED));
> tempGc.drawLine(0, 0, image.getBounds().width,
> image.getBounds().height);
> tempGc.drawLine(0, image.getBounds().height,
> image.getBounds().width, 0);
>
> final Shell shell = new Shell(display);
> shell.setText("Test Image 3.");
> shell.addListener(SWT.Paint, new Listener() {
> public void handleEvent(Event event) {
> GC gc = event.gc;
> Rectangle rect = shell.getClientArea();
> Rectangle bounds = image.getBounds();
> gc.drawImage(image,
> rect.x + (rect.width - bounds.width) / 2,
> rect.y + (rect.height - bounds.height) / 2);
> }
> });
> shell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW) );
> shell.setSize(200, 100);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
> }
>
>
Re: Bug in SWT, drawing on partially transparent image? [message #451659 is a reply to message #451658] Fri, 04 March 2005 10:15 Go to previous message
Eclipse UserFriend
Originally posted by: David.A.Price.nokia.com

Hi,

Veronika Irvine wrote:
> When you call the ImageData.setAlpha() API, it is assumed that you are going
> to set the alpha value for each pixel. The alphaData array gets created and
> initially all of its values are 0 until you set a different value. Drawing
> on the image does not affect the alpha values for the pixels you are drawing
> on. Perhaps you should be setting the transparency rather than the alpha
> value to get the effect you want:

Actually, my eventual purpose is to be able to create arbitrary
non-rectangular 'sprites' by drawing on initially transparent images. I
was just surprised that drawing on an image does not affect the alpha
values for the pixels you draw on - it seems natural that it would (after
all, drawing on a transparent piece of glass results in opaque lines).
Maybe this is just a reflection of an oddity of the underlying Windows API?

Your approach of defining a transparent colour should solve my problem -
with a 24-bit palette it's not hard to choose a colour that I won't need
for my sprite. I must say I'm surprised to learn that you can have a
transparent colour without an indexed palette - you learn a new thing
every day :-)

Thanks for your help,
David
Previous Topic:How I can change the size of a treeviewer to show all content?
Next Topic:OleAutmation help
Goto Forum:
  


Current Time: Mon Jul 07 21:16:41 EDT 2025

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

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

Back to the top