私は現在、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() {}
}
どんな助けでもいただければ幸いです。