我目前正在使用 SWT 和 Eclipse 的 WindowBuilder 制作一个独立的应用程序(不是 Eclipse 插件),并且我正在测试将图像放入合成中,以便在我尝试绘制时轻松地将图像放入我的合成中图像,它会引发 IllegalArgumentException。我不知道发生了什么,我正在寻找解释/替代方案。发生了什么,我将如何解决这个问题?
如果我注释掉这一e.gc.drawImage
行,仅此而已,它不会抛出异常。
这是给我错误的代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GUI {
public static final Display display = Display.getDefault();;
private final Shell shell;
public GUI() {
shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
}
public static void main(String[] args) {
GUI window = new GUI();
window.open();
}
public void open() {
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
private void createContents() {
shell.setSize(450, 300);
ImageTest img = new ImageTest(shell, SWT.NONE);
}
}
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
public class ImageTest extends Composite {
public ImageTest(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout(SWT.HORIZONTAL));
final Image img = new Image(GUI.display, "img.gif");
// I tried drawing the image to both a canvas and the composite its self. Same outcome.
Canvas canvas = new Canvas(this, SWT.NONE);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(img, 0, 0); // If I comment this out, it runs fine.
}
});
img.dispose();
}
@Override
protected void checkSubclass() {}
}
任何帮助,将不胜感激。