[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Re: Drawing to GC
|
Thanks Grant, that did the trick. Our code was identical except for one line
I was missing: gc.dispose(); I did a few experiments and it looks like if
you don't dispose the GC you get a completely black label.
Thanks again.
Angel
"Grant Gayed" <grant_gayed@xxxxxxxxxx> wrote in message
news:badeg8$cs5$1@xxxxxxxxxxxxxxxx
> This does it through JFace (direct to an swt Tree shouldn't be any
> different w.r.t. creating the Image):
>
> public static void main (String[] args) {
> final Display display = new Display();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,200,200);
> TreeViewer tree = new TreeViewer(shell,SWT.NONE);
> tree.getTree().setBounds(10,10,140,140);
> tree.setLabelProvider(new ILabelProvider() {
> public Image getImage(Object element) {
> Image image = new Image (display, 16, 16);
> int random = (int)System.currentTimeMillis() % 2;
> Color color;
> if (random == 0) {
> color = display.getSystemColor (SWT.COLOR_RED);
> } else {
> color = display.getSystemColor (SWT.COLOR_BLUE);
> }
> GC gc = new GC (image);
> gc.setBackground (color);
> gc.fillOval (1, 1, 14, 14);
> gc.dispose ();
> return image;
> }
> public String getText(Object element) {
> return "node";
> }
> public void addListener(ILabelProviderListener listener) {}
> public void dispose() {}
> public boolean isLabelProperty(Object element, String property) {
> return false;
> }
> public void removeListener(ILabelProviderListener listener) {}
> });
> tree.setContentProvider(new ITreeContentProvider() {
> public Object[] getChildren(Object parentElement) {return null;}
> public Object getParent(Object element) {return null;}
> public boolean hasChildren(Object element) {return true;}
> public Object[] getElements(Object inputElement) {
> return new String[] {"",""};
> }
> public void dispose() {}
> public void inputChanged(Viewer viewer, Object oldInput, Object
> newInput) {}
> });
> tree.setInput("");
> shell.pack ();
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
> Grant
>
>
>
> Angel Shishkov wrote:
>
> > Hello,
>
> > I would like to dynamically generate the labels for a Tree. The labels
must
> > be circles with varying colors. I don't have the labels as images in a
> > file, so I need to create the Image dynamically in the code. This is
what
> > I've tried:
>
> > Image label = new Image(null, 15, 15);
> > GC gc = new GC (label);
> > gc.setBackground(ColorConstants.blue);
> > gc.fillOval(0, 0, 15, 15);
> > return label;
>
> > However, the labels that get displayed by this are just black squares.
I
> > have also tried:
>
> > Image label = new Image(Display.getCurrent(), 15, 15);
>
> > with the same effect.
>
> > I have tried this with both a JFace TreeViewer by implementing the
> > LabelProvider and an SWT Tree widget by using TreeItem.setImage(img);
Both
> > methods produce a tree with black squares for labels.
>
> > Is this the correct way to draw to a GC? Am I missing something?
>
> > Any help is greatly appreciated. Thanks!
>
> > Angel
>
>
>
>
>
>