/** * Returns the receiver's image data. This is the icon that is associated with the receiver in the * operating system. * * @return the image data for the program, may be null */ public ImageData getImageData() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath; if (this.fullPath != null) { fullPath = NSString.stringWith(this.fullPath); } else { fullPath = workspace.fullPathForApplication(NSString.stringWith(name)); } if (fullPath != null) { NSImage nsImage = workspace.iconForFile(fullPath); if (nsImage != null) { NSSize size = new NSSize(); size.width = size.height = 16; nsImage.setSize(size); nsImage.retain(); Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage); ImageData imageData = image.getImageData(); image.dispose(); return imageData; } } return null; } finally { pool.release(); } }
/** * This implementation of <code>nativeToJava</code> converts a platform specific representation of * an image to java <code>ImageData</code>. * * @param transferData the platform specific representation of the data to be converted * @return a java <code>ImageData</code> of the image if the conversion was successful; otherwise * null * @see Transfer#javaToNative */ public Object nativeToJava(TransferData transferData) { ImageData imgData = null; if (transferData.length > 0) { long /*int*/ loader = OS.gdk_pixbuf_loader_new(); try { OS.gdk_pixbuf_loader_write(loader, transferData.pValue, transferData.length, null); OS.gdk_pixbuf_loader_close(loader, null); long /*int*/ pixbuf = OS.gdk_pixbuf_loader_get_pixbuf(loader); if (pixbuf != 0) { Image img = Image.gtk_new_from_pixbuf(Display.getCurrent(), SWT.BITMAP, pixbuf); imgData = img.getImageData(); img.dispose(); } } finally { OS.g_object_unref(loader); } } return imgData; }
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ int startX, startY; @Override public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { Point p = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); startX = p.x - loc.x; startY = p.y - loc.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }
public static void main(String[] args) { final Display display = new Display(); final Clipboard clipboard = new Clipboard(display); final Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setLayout(new GridLayout()); shell.setText("Clipboard ImageTransfer"); final Button imageButton = new Button(shell, SWT.NONE); GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); gd.minimumHeight = 400; gd.minimumWidth = 600; imageButton.setLayoutData(gd); final Text imageText = new Text(shell, SWT.BORDER); imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); Composite buttons = new Composite(shell, SWT.NONE); buttons.setLayout(new GridLayout(4, true)); Button button = new Button(buttons, SWT.PUSH); button.setText("Open"); button.addListener( SWT.Selection, event -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, string); imageButton.setImage(image); imageText.setText(string); } }); button = new Button(buttons, SWT.PUSH); button.setText("Copy"); button.addListener( SWT.Selection, event -> { Image image = imageButton.getImage(); if (image != null) { ImageTransfer imageTransfer = ImageTransfer.getInstance(); TextTransfer textTransfer = TextTransfer.getInstance(); clipboard.setContents( new Object[] {image.getImageData(), imageText.getText()}, new Transfer[] {imageTransfer, textTransfer}); } }); button = new Button(buttons, SWT.PUSH); button.setText("Paste"); button.addListener( SWT.Selection, event -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, imageData); imageButton.setImage(image); } else { imageButton.setText("No image"); imageButton.setImage(null); } String text = (String) clipboard.getContents(TextTransfer.getInstance()); if (text != null) { imageText.setText(text); } else { imageText.setText(""); } }); button = new Button(buttons, SWT.PUSH); button.setText("Clear"); button.addListener( SWT.Selection, event -> { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); imageButton.setImage(null); imageText.setText(""); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }