Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » noob swt canvas question
noob swt canvas question [message #457045] Wed, 15 June 2005 21:18 Go to next message
Antoine is currently offline AntoineFriend
Messages: 19
Registered: July 2009
Junior Member
Hi,

Could someone tell me why I can not get the Canvas object below to
display an image or have text written to it?
Cheers
Antoine

Image img = new Image(display,
"/home/antt/workspace/uitry/EclipseBannerPic.jpg");
shell.open();
/*Button button = new Button(shell,SWT.PUSH);
button.setImage(img);
button.setBounds(new org.eclipse.swt.graphics.Rectangle(88,88,117,100));
//get a Graphics Context

Label label = new Label(shell,SWT.NONE);
//label.setBackground(couleur);
label.setImage(img);
//label.setText("Saisir la valeur");
label.setBounds(10, 10, 100, 25); */

Canvas canvas = new Canvas(shell, SWT.BORDER);

canvas.setBounds(210, 210, 200, 200);

GC gc = new GC(canvas);

//draw the image at a specific x and y location
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawImage(img, 0, 0);
Re: noob swt canvas question [message #457048 is a reply to message #457045] Wed, 15 June 2005 21:31 Go to previous messageGo to next message
Antoine is currently offline AntoineFriend
Messages: 19
Registered: July 2009
Junior Member
Antoine wrote:
> Hi,
>
> Could someone tell me why I can not get the Canvas object below to
> display an image or have text written to it?
> Cheers
> Antoine
>
....

even more bizarre - I can sometimes see text flash on and then disappear?
!
Cheers
Antoine
Re: noob swt canvas question [message #457072 is a reply to message #457048] Wed, 15 June 2005 22:07 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Painting to a GC should be done in a PaintListener. For an example of this
see
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet168.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup .

Grant

"Antoine" <melser.anton@gmail.com> wrote in message
news:d8q01g$mgi$1@news.eclipse.org...
> Antoine wrote:
> > Hi,
> >
> > Could someone tell me why I can not get the Canvas object below to
> > display an image or have text written to it?
> > Cheers
> > Antoine
> >
> ...
>
> even more bizarre - I can sometimes see text flash on and then disappear?
> !
> Cheers
> Antoine
Re: noob swt canvas question [message #457355 is a reply to message #457072] Thu, 23 June 2005 19:53 Go to previous messageGo to next message
Antoine is currently offline AntoineFriend
Messages: 19
Registered: July 2009
Junior Member
Grant Gayed wrote:
> Painting to a GC should be done in a PaintListener. For an example of this
> see
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet168.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup .

what about this from an IBM sponsored tutorial? It was written for 2.1 -
should this no longer work?
Cheers
Antoine

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

public class ImageDemo {

public static Display myDisplay;
public static boolean internalCall = false;

public static void main(String[] args) {
internalCall = true;
myDisplay = new Display();
ImageDemo id = new ImageDemo();
id.runDemo(myDisplay);
}

public void runDemo(Display display) {
myDisplay = display;
Shell shell = new Shell(display);
shell.setSize(140,100);
shell.setText("Image Demo");

//load an image from a JPEG file
Image img = new Image(display, "EclipseBannerPic.jpg");

// //roughly equivalent code that works for loading an image from a JAR
file
// InputStream is = getClass().getResourceAsStream("EclipseBannerPic.jpg");
// Image img = new Image(display, is);

//open the shell, so we can draw on it
shell.open();

Canvas canvas = new Canvas(shell, SWT.BORDER);

canvas.setSize(150, 150);
canvas.setLocation(20, 20);

//get a Graphics Context
GC gc = new GC(canvas);

//draw the image at a specific x and y location
gc.drawImage(img, 0, 0);

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

//dispose of the image object
img.dispose();

if (internalCall) display.dispose();

}

}
Re: noob swt canvas question [message #457368 is a reply to message #457355] Fri, 24 June 2005 13:20 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
That was never the optimal way to do this, because even if it did work
(maybe it did at the time, or on certain platform(s)), the image would not
repaint damaged areas caused by (for example) dragging another window over
it. Is this tutorial available on-line? If so then I'll try to find
someone to send a note to to have it fixed.

Grant

"Antoine" <melser.anton@gmail.com> wrote in message
news:d9f3vj$ect$1@news.eclipse.org...
> Grant Gayed wrote:
> > Painting to a GC should be done in a PaintListener. For an example of
this
> > see
> >
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet168.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup .
>
> what about this from an IBM sponsored tutorial? It was written for 2.1 -
> should this no longer work?
> Cheers
> Antoine
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.*;
> import org.eclipse.swt.widgets.*;
>
> public class ImageDemo {
>
> public static Display myDisplay;
> public static boolean internalCall = false;
>
> public static void main(String[] args) {
> internalCall = true;
> myDisplay = new Display();
> ImageDemo id = new ImageDemo();
> id.runDemo(myDisplay);
> }
>
> public void runDemo(Display display) {
> myDisplay = display;
> Shell shell = new Shell(display);
> shell.setSize(140,100);
> shell.setText("Image Demo");
>
> //load an image from a JPEG file
> Image img = new Image(display, "EclipseBannerPic.jpg");
>
> // //roughly equivalent code that works for loading an image from a JAR
> file
> // InputStream is =
getClass().getResourceAsStream("EclipseBannerPic.jpg");
> // Image img = new Image(display, is);
>
> //open the shell, so we can draw on it
> shell.open();
>
> Canvas canvas = new Canvas(shell, SWT.BORDER);
>
> canvas.setSize(150, 150);
> canvas.setLocation(20, 20);
>
> //get a Graphics Context
> GC gc = new GC(canvas);
>
> //draw the image at a specific x and y location
> gc.drawImage(img, 0, 0);
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
>
> //dispose of the image object
> img.dispose();
>
> if (internalCall) display.dispose();
>
> }
>
> }
Re: noob swt canvas question [message #457450 is a reply to message #457368] Sat, 25 June 2005 08:28 Go to previous message
Antoine is currently offline AntoineFriend
Messages: 19
Registered: July 2009
Junior Member
Grant Gayed wrote:
> That was never the optimal way to do this, because even if it did work
> (maybe it did at the time, or on certain platform(s)), the image would not
> repaint damaged areas caused by (for example) dragging another window over
> it. Is this tutorial available on-line? If so then I'll try to find
> someone to send a note to to have it fixed.
>
> Grant
>
> "Antoine" <melser.anton@gmail.com> wrote in message
> news:d9f3vj$ect$1@news.eclipse.org...
>
>>Grant Gayed wrote:
>>
>>>Painting to a GC should be done in a PaintListener. For an example of
>
> this
>
>>>see
>>>
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet168.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup .
>
>>what about this from an IBM sponsored tutorial? It was written for 2.1 -
>>should this no longer work?
>>Cheers
>>Antoine

Hi there,
I must make a confession - I come from a VB6 background and am having
trouble making the leap to a proper OO language like Java. In my efforts
to get up to speed I rely heavily on code snippets, though I realise
that these are always a two-edged sword given that eclipse and swt are
evolving rapidly. The code comes from:

http://www.cs.umanitoba.ca/~eclipse/

which is always up there with the top results from Google. I must say I
have seen gcs being painted out of paint listeners in lots of places
though... and trying to do that with 3.0.2 doesn't seem to work for me.
It works if I add a paint listener though so I won't be making that
mistake again!
Thanks
Antoine
ps. The site above provides a very comprehensive set of tutorials and it
would be great, after so graciously having funded the initial dev., if
IBM could fund one of their (your!) gurus to have a quick look at it!
Previous Topic:swt developers : inconsistent tree behaviour
Next Topic:Testing for Unicode support in an SWT font
Goto Forum:
  


Current Time: Thu Apr 18 11:10:33 GMT 2024

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

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

Back to the top