【发布时间】:2012-09-24 07:16:45
【问题描述】:
我正在开发一个包含 JFreeChart 的 Eclipse RCP 应用程序。它的功能之一是将图形复制到剪贴板以便将它们粘贴到其他应用程序中,但它不适用于 Linux,
有一个SWT sample,您可以在其中找到在 Linux 中不起作用的 sn-p。
另一方面,JFreeChart 在 AWT 上实现了这一点:
Clipboard systemClipboard
= Toolkit.getDefaultToolkit().getSystemClipboard();
Insets insets = getInsets();
int w = getWidth() - insets.left - insets.right;
int h = getHeight() - insets.top - insets.bottom;
ChartTransferable selection = new ChartTransferable(this.chart, w, h,
getMinimumDrawWidth(), getMinimumDrawHeight(),
getMaximumDrawWidth(), getMaximumDrawHeight(), true);
systemClipboard.setContents(selection, null);
但是,这两个示例在 Linux 64 位上都失败了。有没有办法实现?
提前致谢!
编辑:
将 JFreeChart 图形复制到文件但不复制到剪贴板的代码
final org.eclipse.swt.dnd.Clipboard clipboard = new org.eclipse.swt.dnd.Clipboard(menu.getDisplay());
Insets insets = source.getInsets();
int w = source.getWidth() - insets.left - insets.right;
int h = source.getHeight() - insets.top - insets.bottom;
ChartTransferable selection = new ChartTransferable(source
.getChart(), w, h, source.getMinimumDrawWidth(), source.getMinimumDrawHeight(), source
.getMaximumDrawWidth(), source.getMaximumDrawHeight(), true);
Image image = new Image(menu.getDisplay(),ImageUtils.convertToSWT(selection.getBufferedImage()));
if (image != null) {
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save("/tmp/graph.jpg", SWT.IMAGE_JPEG); // fails
ImageTransfer imageTransfer = ImageTransfer.getInstance();
clipboard.setContents(new Object[]{image.getImageData()},
new Transfer[]{imageTransfer}, DND.CLIPBOARD | DND.SELECTION_CLIPBOARD);
}
【问题讨论】:
-
我使用的是 Linux,您提供的链接中的代码 sn-p 有效。开始 -> 打开图像 -> 复制 -> 打开 gimp -> 粘贴 -> 成功。这对你不起作用?
-
使用此方法不正确:Clipboard类型中的setContents(Transferable, ClipboardOwner)方法不适用于参数(ChartTransferable, ChartTransferable)
-
所以代码 sn-p 本身可以工作,但您不能将其用于您的目的?
-
我使用的是 Ubuntu 11.04 64 位版本,这两个示例都不适合我。在 AWT 示例中,我得到一个 javax.imageio.IIOException: Invalid argument to native writeImage。顺便说一句,我正在使用 J2SE6
-
我看了可能是64位的问题,你用的是哪个发行版?
标签: java linux swt awt jfreechart