noob swt canvas question [message #457045] |
Wed, 15 June 2005 21:18 |
Antoine 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 #457355 is a reply to message #457072] |
Thu, 23 June 2005 19:53 |
Antoine 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 |
Grant Gayed 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();
>
> }
>
> }
|
|
|
|
Powered by
FUDForum. Page generated in 0.01872 seconds