ImportJava. awt. Graphics;
ImportJava. awt. Image;
ImportJava. awt. event. InputEvent;
ImportJava. awt. event. MouseEvent;
ImportJava. awt. geom. Point2D;
ImportJava. awt. geom. Rectangle2D;
ImportJava. awt. image. BufferedImage;
ImportJava. awt. image. DirectColorModel;
ImportJava. awt. image. IndexColorModel;
ImportJava. awt. image. WritableRaster;
ImportJavax. swing. JPanel;
ImportOrg. eclipse. swt. SWT;
ImportOrg. eclipse. swt. graphics. Color;
ImportOrg. eclipse. swt. graphics. Device;
ImportOrg. eclipse. swt. graphics. Font;
ImportOrg. eclipse. swt. graphics. FontData;
ImportOrg. eclipse. swt. graphics. GC;
ImportOrg. eclipse. swt. graphics. ImageData;
ImportOrg. eclipse. swt. graphics. PaletteData;
ImportOrg. eclipse. swt. graphics. Point;
ImportOrg. eclipse. swt. graphics. RGB;
ImportOrg. eclipse. swt. graphics. Rectangle;
/**
* Utility class gathering some useful and general method.
* Mainly convert forth and back graphical stuff
* Awt and swt.
*/
Public classSWTUtils {
Private final staticString Az = "ABCpqr ";
/** A dummy JPanel used to provide font metrics .*/
Protected static finalJPanel DUMMY_PANEL =NewJPanel ();
/**
* Creates an AWT <code> MouseEvent </code> from a swt event.
* This method helps passing SWT mouse event to awt components.
* @ Param event The swt event.
* @ Return a awt mouse event based on the given SWT event.
*/
Public staticMouseEvent toAwtMouseEvent (org. eclipse. swt. events. MouseEvent event ){
IntButton = MouseEvent. NOBUTTON;
Switch(Event. button ){
Case1: button = MouseEvent. BUTTON1;Break;
Case2: button = MouseEvent. BUTTON2;Break;
Case3: button = MouseEvent. BUTTON3;Break;
}
IntModifiers = 0;
If(Event. stateMask & SWT. CTRL )! = 0 ){
Modifiers | = InputEvent. CTRL_DOWN_MASK;
}
If(Event. stateMask & SWT. SHIFT )! = 0 ){
Modifiers | = InputEvent. SHIFT_DOWN_MASK;
}
If(Event. stateMask & SWT. ALT )! = 0 ){
Modifiers | = InputEvent. ALT_DOWN_MASK;
}
MouseEvent awtMouseEvent =NewMouseEvent (DUMMY_PANEL, event. hashCode (),
Event. time, modifiers, event. x, event. y, 1, false, button );
ReturnAwtMouseEvent;
}
/**
* Converts an AWT image to SWT.
*
* @ Param image the image (<code> null </code> not permitted ).
*
* @ Return Image data.
*/
Public staticImageData convertAWTImageToSWT (Image image ){
If(Image =Null){
Throw newIllegalArgumentException ("Null 'image' argument .");
}
IntW = image. getWidth (Null);
IntH = image. getHeight (Null);
If(W =-1 | h =-1 ){
Return null;
}
BufferedImage bi =NewBufferedImage (w, h, BufferedImage. TYPE_INT_RGB );
Graphics g = bi. getGraphics ();
G. drawImage (image, 0, 0,Null);
G. dispose ();
ReturnConvertToSWT (bi );
}
/**
* Converts a buffered image to SWT <code> ImageData </code>.
*
* @ Param bufferedImage the buffered image (<code> null </code> not
* Permitted ).
*
* @ Return The image data.
*/
Public staticImageData convertToSWT (BufferedImage bufferedImage ){
If(BufferedImage. getColorModel ()InstanceofDirectColorModel ){
DirectColorModel colorModel
= (DirectColorModel) bufferedImage. getColorModel ();
PaletteData palette =NewPaletteData (colorModel. getRedMask (),
ColorModel. getGreenMask (), colorModel. getBlueMask ());
ImageData data =NewImageData (bufferedImage. getWidth (),
BufferedImage. getHeight (), colorModel. getPixelSize (),
Palette );
WritableRaster raster = bufferedImage. getRaster ();
Int[] PixelArray =NewInt[3];
For(IntY = 0; y <data. height; y ++ ){
For(IntX = 0; x <data. width; x ++ ){
Raster. getPixel (x, y, pixelArray );
IntPixel = palette. getPixel (NewRGB (pixelArray [0],
PixelArray [1], pixelArray [2]);
Data. setPixel (x, y, pixel );
}
}
ReturnData;
}
Else if(BufferedImage. getColorModel ()InstanceofIndexColorModel ){
IndexColorModel colorModel = (IndexColorModel)
BufferedImage. getColorModel ();
IntSize = colorModel. getMapSize ();
Byte[] Reds =NewByte[Size];
Byte[] Greens =NewByte[Size];
Byte[] Blues =NewByte[Size];
ColorModel. getReds (reds );
ColorModel. getGreens (greens );
ColorModel. getBlues (blues );
RGB [] rgbs =NewRGB [size];
For(IntI = 0; I <rgbs. length; I ++ ){
Rgbs [I] =NewRGB (reds [I] & 0xFF, greens [I] & 0xFF,
Blues [I] & 0xFF );
}
PaletteData palette =NewPaletteData (rgbs );
ImageData data =NewImageData (bufferedImage. getWidth (),
BufferedImage. getHeight (), colorModel. getPixelSize (),
Palette );
Data. transparentPixel = colorModel. getTransparentPixel ();
WritableRaster raster = bufferedImage. getRaster ();
Int[] PixelArray =NewInt[1];
For(IntY = 0; y <data. height; y ++ ){
For(IntX = 0; x <data. width; x ++ ){
Raster. getPixel (x, y, pixelArray );
Data. setPixel (x, y, pixelArray [0]);
}
}
ReturnData;
}
Return null;
}
}
Reprinted from:
Http://www.java2s.com/Tutorial/Java/0280__SWT/ConvertsanAWTimagetoSWT.htm
Mutual conversion between SWT and AWT Images
Http://dearseven.blog.163.com/blog/static/10053792220111073394537/