SWT GC : Color not correctly drawn on Windows
Still looking for a solution
I
have the following problem: I use SWT GC
to plot the graphs contained in GraphNodes to the Zest Graph.
As far as Linux and MacOS are concerned, everything works. But when I run my jar on Windows, the node looks strange. The colors are not drawn correctly and have no transparency (achieved by setAlpha()
of GC
).
Here are two screenshots to illustrate my problem:
Linux:
Windows :
Edit:
I just created this working “mini” example for testing. I would appreciate it if anyone knew why the rectangle is black on Windows. This is back.png image: <img alt=”This is the <code>back
.png</code>” src=”/image/YpewL.png”/>
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.core.widgets.IContainer;
public class MiniExample
{
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
Graph graph = new Graph(shell, SWT. NONE);
graph.setSize(100, 100);
CustomFigure fig = new CustomFigure(new Label());
fig.setSize(-1, -1);
CustomNode node = new CustomNode(graph, SWT. NONE, fig);
node.setLocation(5, 5);
shell.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
/* Minimal helper class for the figure */
static class CustomFigure extends Figure
{
private Image background = new Image(Display.getDefault(), "back.png");
private GC gcBack = new GC(background);
private Label all = new Label(background);
private Color blau = new Color(Display.getDefault(), 19, 59, 94);
public CustomFigure(Label label)
{
ToolbarLayout layout = new ToolbarLayout();
setLayoutManager(layout);
setMiddle(3);
add(all);
}
/* The problem has to occur somewhere here... */
public void setMiddle(int value)
{
gcBack.setBackground(blau);
gcBack.setForeground(blau);
gcBack.setAlpha(255);
/* color background blue and draw the nr of connections */
gcBack.drawRoundRectangle(6, 6, 15, 15, 3, 3);
gcBack.fillRoundRectangle(6, 6, 15, 15, 3, 3);
gcBack.setForeground(ColorConstants.white);
gcBack.drawText(value+"", 9, 6, true);
gcBack.setAlpha(255);
all.repaint();
}
}
/* Minimal helper class for the node */
static class CustomNode extends GraphNode
{
public CustomNode(IContainer graphModel, int style, CustomFigure figure)
{
super(graphModel, style, figure);
}
@Override
protected IFigure createFigureForModel()
{
return (IFigure) this.getData();
}
}
}
Solution
The
gray image used in your program is significantly different from the white image posted by Tobias Willig:
Gray:
Image Width: 28 Image Length: 28
Bitdepth (Bits/Sample): 1
Channels (Samples/Pixel): 1
Pixel depth (Pixel Depth): 1
Colour Type (Photometric Interpretation): PALETTED COLOUR (1 colours, 0 transparent)
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 2835, 2835 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0
White:
Image Width: 28 Image Length: 28
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 3
Pixel depth (Pixel Depth): 24
Colour Type (Photometric Interpretation): RGB
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 2835, 2835 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0
As you can see, the color type
of gray images contains only one color, while one color of white images is RGB.
So surely the image is causing your problem.