Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] java.awt.Image

I believe the only possible way of doing what you want is by
getting the data out of the AWT image and create a SWT image
using that data. 

The code below shows how to do this. Keep in that the following 
code does not handle all images properly. For e


import java.awt.Frame;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

public class AWTToSWTImage {
 
public static void main(String[] args) throws Exception {
        Frame frame = new Frame();
 
        java.awt.Image awtImage = Toolkit.getDefaultToolkit().getImage("c:\\temp\\upgray.gif");
        MediaTracker tracker = new MediaTracker(frame);
        tracker.addImage(awtImage, 0);
        tracker.waitForAll();
 
        int width = awtImage.getWidth(null);
        int height = awtImage.getHeight(null);
        PixelGrabber grabber = new PixelGrabber(awtImage, 0, 0, width, height, true);
 
        if (!grabber.grabPixels()) {
                System.out.println("Error");
                return;
        }
 
        DirectColorModel cm = 
(DirectColorModel)ColorModel.getRGBdefault();
        int[] pixels = (int[])grabber.getPixels();
 
        PaletteData palette = new PaletteData(cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
        ImageData data = new ImageData(width, height, cm.getPixelSize(), palette);
        data.setPixels(0, 0, width * height, pixels, 0);
 
        Display display = new Display();
        Shell shell = new Shell(); 
        final Image swtImage = new Image(display, data); 
        shell.addListener(SWT.Paint, new Listener() {
                public void handleEvent(Event e) {
                        GC gc = e.gc;
                        gc.drawImage(swtImage, 10, 10);
                }
        });
        shell.setBounds(0, 0, 300, 300);
        shell.open();
 
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) display.sleep();
        }
}

}






"Jess Terr" <jst@xxxxxxxxxxxxx>
Sent by: platform-swt-dev-admin@xxxxxxxxxxx
11/26/01 11:03 AM
Please respond to platform-swt-dev

 
        To:     platform-swt-dev@xxxxxxxxxxx
        cc: 
        Subject:        [platform-swt-dev] java.awt.Image

Hello.
I have tried to port my old AWT application to SWT and everything seemed 
fine until I realized that AWT Image is not SWT Image.

That also would not be problem, if I was in control of creation of that 
image, but I am not. I just have plain reference to java.awt.Image and I 
need a way to display it in SWT.

I am not expert in AWT nor SWT, but I expect a lot of such people 
subscribed to this mailing list. Can somebody help me, please?

Thank you in advance.
jst

-- 

_______________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup


1 cent a minute calls anywhere in the U.S.!

http://www.getpennytalk.com/cgi-bin/adforward.cgi?p_key=RG9853KJ&url=http://www.getpennytalk.com

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





Back to the top