|
swing copy images to clipboard |
Posted by Amar on Nov-16-2006 22:51 |
|
Hi,
I am using Java Swing to display the charts I have created. All the charts have hotspots on them. Clicking on the hotspot opens a popup with an other chart.
I have a panel with multiple different charts in them (i.e more than one chart viewer) and all charts are shown.
My requirement:
1. Right click on an individual chart. Show the end user the 'copy' functionality. User selects to 'copy' and the image is copied to clipboard. End user can then paste it in a word document.
2. Provide a 'copy' button to the Panel. On clicking the button, all images are copied to the clipboard. End user pastes the images in word. All images are copied with the layout UNCHANGED.
I would appreciate it if you can guide me on how to achieve this. Sample working code is highly appreciated. Thank you for your time.
Regards,
Amar.. |
Re: swing copy images to clipboard |
Posted by Peter Kwan on Nov-17-2006 03:37 |
|
Hi Amar,
I think you can copy a chart image to clipboard just like copying any other Java image to clipboard. It should be the same. (Are you familiar with how to copy a standard Java java.awt.Image to clipboard? There should be plenty of sample code you may find in google.)
For example:
java.awt.Image image = myChartViewer.getImage();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new ImageSelection(image), null);
The ImageSelection is what you need to write to represent the image selection. For a single image, you may try:
class ImageSelection implements Transferable
{
private final static DataFlavor [] supportedFlavors = {DataFlavor.imageFlavor};
private Image picture;
public ImageSelection (Image parPicture) {
picture = parPicture;
}
public synchronized DataFlavor [] getTransferDataFlavors () {
return supportedFlavors;
}
public boolean isDataFlavorSupported (DataFlavor parFlavor) {
return parFlavor.equals(DataFlavor.imageFlavor);
}
public synchronized Object getTransferData (DataFlavor parFlavor) {
return parFlavor.equals(DataFlavor.imageFlavor) ? picture : null;
}
}
I am not sure if you can easily copy multiple images with layout to the clipboard. In fact, I am even sure how to copy multiple pieces of text (such as two JLabel controls) to the clipboard (with exact layout). However, if you have in depth knowledge of system clipboard and know how to copy images to clipboard, then you should be able to copy charts easily, as charts are just images to the clipboard. (May be you can try to take a "screen shot" and copy the result to clipboard.)
Hope this can help.
Regards
Peter Kwan |
Re: swing copy images to clipboard |
Posted by Lofi on Jan-03-2012 18:09 |
|
Hello Peter,
I tried this, but all I get is the chart as image and everything around it is entirely black (is.png). I don't see the legend, etc like it is in the windows screenshot (shouldbe.png).
Any ideas how to fix this?
Thank you very much!
|
Re: swing copy images to clipboard |
Posted by Peter Kwan on Jan-04-2012 01:13 |
|
Hi Lofi,
I believe Java does not support transparent images in clipboard. It will change the transparent pixels into black. In your case, it seems you are in fact using transparent images, so that transparent part becomes black.
To solve the problem, you may try the following methods:
(a) Do not use a transparent background. Instead, just set the background color to the same color as your window.
(b) If you must use a transparent image in the window, you would need to remove the transparent part before you copy it to the clipboard. To do this, you may create a new BufferedImage, then use Graphics.drawImage to copy the original image to the BufferedImage with Color.WHITE (or any color you like) as the background color to replace the transparent color. It is like:
BufferedImage image2 = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
image2.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
... use image2 in the clipboard instead of image ...
Hope this can help.
Regards
Peter Kwan |
|