Canvas won't draw on composite (SWT)

Canvas won't draw on composite (SWT)

I can't get a canvas to draw on a composite with SWT. I've made the composite on a shell and given it a layout but the canvas won't draw the image. Please help me and bare with me because I'm new to java. The relevant code is below.

public static void main(String[] args) {

    final Display display = new Display();
    final Image img = new Image(display,"C:\\Users\\Public\\Pictures\\Sample 
                                  Pictures\\Lighthouse.jpg");

    final Shell shell = new Shell(display);
    shell.setText("Canvas in Composite");

    Composite comp = new Composite(shell, SWT.BORDER);
    comp.setBounds(0, 0, 500, 500);
    comp.setLayout(new FillLayout());

    final Canvas can = new Canvas(comp, SWT.NONE);
    can.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            e.gc.drawImage(img, 0, 0);
        }
    });

    shell.open();

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

    img.dispose();
    display.dispose();
}
View Answers









Related Tutorials/Questions & Answers:
Canvas won't draw on composite (SWT)
Canvas won't draw on composite (SWT)  I can't get a canvas to draw on a composite with SWT. I've made the composite on a shell and given it a layout but the canvas won't draw the image. Please help me and bare with me because I'm
Draw Calendar in SWT
Draw Calendar in SWT       This section illustrates you how to draw  a calendar in SWT, showing current date. SWT allows to create Calendar by using the java.util.Calendar class
Advertisements
Draw Font Using Canvas Example
Draw Font Using Canvas Example       This example is used to draw the different types of font using Canvas class. The following line of code is used to show the different style
Draw String Using Canvas
Draw String Using Canvas       This example is used to draw string on different location which is shown in figure. The given code is used to show, how to draw string at different
Draw Clip Area Using Canvas
Draw Clip Area Using Canvas       This Example is going to draw a clip with SOLID line. In this picture only solid line show the clipping area. To draw a solid line, we have
Canvas placing problem
Canvas placing problem  how to place a canvas in swt under a toolbar
J2ME Draw Triangle
J2ME Draw Triangle       As you already aware of the canvas class and it's use in J2ME application, we are using canvas class to draw the triangle on the screen. In this example
Line Canvas MIDlet Example
;CanvasCrossLine" class created by us extends the Canvas class to draw both... Line Canvas MIDlet Example       In this example, we are going to draw to different lines which cross
SWT
in the layout.    Draw Calendar in SWT SWT allows... In SWT, we have used the package org.eclipse.draw2D.Graphics to draw the figure... SWT      
Draw arc in J2ME
Draw arc in J2ME       The given example is going to draw an arc using canvas class of J2ME. You can also set a color for it, as we did in our example. Different methods
Version of swt>swt-macosx dependency
List of Version of swt>swt-macosx dependency
Version of swt>swt-mozilla dependency
List of Version of swt>swt-mozilla dependency
ModuleNotFoundError: No module named 'composite'
ModuleNotFoundError: No module named 'composite'  Hi, My Python... 'composite' How to remove the ModuleNotFoundError: No module named 'composite' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'composite'
ModuleNotFoundError: No module named 'composite'  Hi, My Python... 'composite' How to remove the ModuleNotFoundError: No module named 'composite' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'canvas'
ModuleNotFoundError: No module named 'canvas'  Hi, My Python... 'canvas' How to remove the ModuleNotFoundError: No module named 'canvas'... to install padas library. You can install canvas python with following command
Draw Line
Draw Line  sir i want to draw a moving line in j2me.That line should also show arrow in moving direction. How can we do so
Canvas
Adding customitem in canvas in J2ME?
Adding customitem in canvas in J2ME?  In J2ME how should we add... for customitem.The key event handling is allowed in canvas not in form.So i try to use canvas instead of form
Program compiles but won't execute - Java Beginners
Program compiles but won't execute  I have to write a program with a class Employee and then write a driver class to run it. This is what I have so far for the Employee class(it compiles but does not execute); public class
Java draw triangle draw method?
Java draw triangle draw method?  hi how would i construct the draw method for an triangle using the 'public void draw (graphics g ) method? im... a rectangle and this works for a rectangle: public void draw(Graphics g
Artifacts of swt
List of Artifacts of swt maven depenency
Version of nanocontainer-swt>nanocontainer-swt dependency
List of Version of nanocontainer-swt>nanocontainer-swt dependency
Version of swt>swt-gtk-mozilla dependency
List of Version of swt>swt-gtk-mozilla dependency
Version of swt>swt-linux-gtk dependency
List of Version of swt>swt-linux-gtk dependency
Is it possible in SWT ?
Is it possible in SWT ?  I want drop down like google search (ie, when we type one letter then the word start with that are displayed). when the drop... do this in SWT ? Thanks
Simple Line Canvas Example
and another is Graphics.SOLID. To draw this canvas we are uses the following methods... Simple Line Canvas Example       This is a simple example of drawing lines using canvas class
Composite Pattern
Composite Pattern       Individual objects as well as the composite objects can be represented with the Composite Design Pattern. Composite pattern represents these objects
Version of com.oneandone>composite-jks dependency
List of Version of com.oneandone>composite-jks dependency
ModuleNotFoundError: No module named 'python-composite'
ModuleNotFoundError: No module named 'python-composite'  Hi, My... named 'python-composite' How to remove the ModuleNotFoundError: No module named 'python-composite' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'ckanext-composite'
ModuleNotFoundError: No module named 'ckanext-composite'  Hi, My... named 'ckanext-composite' How to remove the ModuleNotFoundError: No module named 'ckanext-composite' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-composite'
ModuleNotFoundError: No module named 'python-composite'  Hi, My... named 'python-composite' How to remove the ModuleNotFoundError: No module named 'python-composite' error? Thanks   Hi, In your
HIBERNATE COMPOSITE ID - Hibernate
HIBERNATE COMPOSITE ID  Hi, I have a database table structure as CREATE TABLE `attendance` ( `sno` int(11) NOT NULL auto_increment... ever time. SO i want to make Emploee ref & date as composite. Is there any body
Version of swt>swt-linux-gtk-pi dependency
List of Version of swt>swt-linux-gtk-pi dependency
ModuleNotFoundError: No module named 'ascii-canvas'
ModuleNotFoundError: No module named 'ascii-canvas'  Hi, My Python... 'ascii-canvas' How to remove the ModuleNotFoundError: No module named 'ascii-canvas' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'base-canvas'
ModuleNotFoundError: No module named 'base-canvas'  Hi, My Python... 'base-canvas' How to remove the ModuleNotFoundError: No module named 'base-canvas' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'canvas-api'
ModuleNotFoundError: No module named 'canvas-api'  Hi, My Python... 'canvas-api' How to remove the ModuleNotFoundError: No module named 'canvas-api' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'dash-canvas'
ModuleNotFoundError: No module named 'dash-canvas'  Hi, My Python... 'dash-canvas' How to remove the ModuleNotFoundError: No module named 'dash-canvas' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'canvas-science'
ModuleNotFoundError: No module named 'canvas-science'  Hi, My... named 'canvas-science' How to remove the ModuleNotFoundError: No module named 'canvas-science' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'flask-canvas'
ModuleNotFoundError: No module named 'flask-canvas'  Hi, My Python... 'flask-canvas' How to remove the ModuleNotFoundError: No module named 'flask-canvas' error? Thanks   Hi, In your python
draw chart in web application
draw chart in web application  how to draw bar chat from the record store in database? i.e. draw the bar chart according to selected record
Composite Components
Composite Components This section contains a brief description and complete example over Composite Components in JSF2.0 . About Composite Components The composite component is a template which behaves as reusable component
Image Icon Using Canvas Example
Image Icon Using Canvas Example     ... location using Canvas class. In this example to create the image we are using following code: And for draw the image we are using following code of lines:ADS
SWT in Eclipse - Java Beginners
SWT in Eclipse  hi.. how to call a function in SWT when the shell is about to close... ?? thanks in advance
SWT_AWT bridge ,jtextfield edit problem - Swing AWT
bridge in my code to embed swing components in to SWT Composite. I am using SWT...); java.awt.Frame frame = SWT_AWT.new_Frame(composite); JApplet applet==new JApplet...(new RowLayout( )); java.awt.Frame frame = SWT_AWT.new_Frame(composite
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'draw' How to remove the ModuleNotFoundError: No module named 'draw' error
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'draw' How to remove the ModuleNotFoundError: No module named 'draw' error
Define candidate key, alternate key, composite key.
Define candidate key, alternate key, composite key.  Define candidate key, alternate key, composite key
Version of swt>forms dependency
Version of swt>osgi dependency
Version of swt>swt-win32 dependency

Ads