Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Draw


Hi Maxine,
In future, please post user questions to the eclipse newsgroup.
This mailing list is for discussion on the implementation of SWT.
You can subscribe to the eclipse newsgroup by reading this page:
http://www.eclipse.org/newsgroups/index.html
and subscribing to this newsgroup:
news://www.eclipse.org/eclipse.tools

Having said that, here's a couple of code examples that should help.
Code example #1 will save to a file, but you may have trouble with your depth.
Currently, you can't save a 32-bit color depth BMP file using SWT.
And GIF can only be 8-bit color depth.
If you use an Image constructor, you get the default Display depth, which you quite likely have set to 32-bit (True color) in your Windows Control Panel.
So look quickly at code example #1 below, but you will probably want to have control over your image's depth and colors, so I recommend following code example #2. (Example #2 also shows how you would create a transparent gif, if that's what you wanted to do).

Code example #1:

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

public class ImageDrawTest {
       
public static void main (String [] args) {
        Display display = new Display();

        // create Image with default depth and palette
        Image image = new Image(display, 20, 20);
       
        // create a GC on your image to draw on it
        GC gc = new GC(image);
        gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
        gc.fillOval(2, 2, 16, 16);
        gc.dispose();

        // get ImageData from image
        ImageData imageData = image.getImageData();
        image.dispose();
       
        // save imageData as a BMP in current directory
        ImageLoader loader = new ImageLoader();
        loader.data = "">new ImageData[] { imageData };
        loader.save("red_oval.bmp", SWT.IMAGE_BMP);

        display.dispose();
}
}

Code example #2:

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

public class ImageDrawTest2 {
       
public static void main (String [] args) {
        Display display = new Display();

        Color black = display.getSystemColor(SWT.COLOR_BLACK);
        Color white = display.getSystemColor(SWT.COLOR_WHITE);
        Color red = display.getSystemColor(SWT.COLOR_RED);
        Color transparent = display.getSystemColor(SWT.COLOR_BLUE);
        PaletteData palette = new PaletteData(new RGB[] {
                black.getRGB(),                        // 0
                white.getRGB(),                        // 1
                red.getRGB(),                        // 2
                transparent.getRGB()                // 3
        });
        ImageData imageData = new ImageData(20, 20, 4, palette); // depth 4, palette with 4 colors
        imageData.transparentPixel = 3; // if you want transparency, you would set that here
       
        // create your Image using your ImageData
        Image image = new Image(display, imageData);

        // create a GC on your image to draw on it
        GC gc = new GC(image);
        gc.setBackground(transparent);
        gc.fillRectangle(0, 0, 20, 20);
        gc.setBackground(red);
        gc.fillOval(2, 2, 16, 16);
        gc.setForeground(black);
        gc.drawLine(2, 2, 16, 16);
        gc.drawLine(2, 16, 16, 2);
        gc.dispose();

        // get modified ImageData from image
        imageData = image.getImageData();
        image.dispose();
       
        // save imageData as a GIF in current directory
        ImageLoader loader = new ImageLoader();
        loader.data = "">new ImageData[] { imageData };
        loader.save("red_oval.gif", SWT.IMAGE_GIF);

        display.dispose();
}
}

Carolyn



Jesse_Hill@xxxxxxx
Sent by: platform-swt-dev-admin@xxxxxxxxxxx

05/13/02 09:00 PM
Please respond to platform-swt-dev

       
        To:        platform-swt-dev@xxxxxxxxxxx
        cc:        
        Subject:        Re: [platform-swt-dev] Draw



To draw on an image, you can do the following:

     Display display = new Display();
     Image image = new Image(display, 100, 100);
     GC gc = new GC(image);
     gc.drawLine(0, 0, 100, 100);
     gc.dispose();
     image.dispose();

You might be out of luck when it comes to writing the image to file. There
is API to do this:

     FileFormat.unloadIntoStream(ImageData data, LEDataOutputStream
stream);

but the file format classes are internal. Probably shouldn't be used.

You could get the data from the Image - Image.getImageData() - and write
that to a file yourself.

-Jesse



                                                                                                                               
                     "Maxine Meiring"                                                                                          
                     <maxine__m@xxxxxxxxxxx>         To:      platform-swt-dev@xxxxxxxxxxx                                    
                     Sent by:                        cc:                                                                      
                     platform-swt-dev-admin@         Subject: [platform-swt-dev] Draw                                          
                     eclipse.org                                                                                              
                                                                                                                               
                                                                                                                               
                     05/13/2002 05:01 PM                                                                                      
                     Please respond to                                                                                        
                     platform-swt-dev                                                                                          
                                                                                                                               
                                                                                                                               



Hi There,

I need some documentation or a simple example of how I would go about
drawing a line on a image in swt and then saving that image to a file.

What should I draw on - canvas?

Regards,
Maxine.


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev




_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev



Back to the top