简体   繁体   English

SWT:合成内的图像; 获取IllegalArgumentException

[英]SWT: Image inside Composite; Getting IllegalArgumentException

I am currently making a stand alone application (not an eclipse plugin) using SWT and Eclipse's WindowBuilder, and I was testing putting an image in a composite to use it as a way to easily plop images into my composites, when I try to draw the image, it throws an IllegalArgumentException. 我目前正在使用SWT和Eclipse的WindowBuilder制作一个独立的应用程序(不是eclipse插件),并且我正在测试将图像放入复合图像中,以便在尝试绘制图像时将其轻松地放入复合图像中图片,它抛出一个IllegalArgumentException。 I have no clue what's happening, and I'm looking for an explanation/alternative. 我不知道发生了什么,我正在寻找一种解释/替代方法。 What's happening and how would I approach fixing this? 发生了什么事,我将如何解决这个问题?

If I comment out the e.gc.drawImage line, and nothing more, it will not throw the exception. 如果我注释掉e.gc.drawImage行,仅此而已,它将不会引发异常。

Here's the code that's giving me the error: 这是给我错误的代码:

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() {}
}

Any help would be appreciated. 任何帮助,将不胜感激。

You get the error because your image is disposed when paintControl tries to draw it. 之所以会收到此错误,是因为当paintControl尝试绘制图像时,图像会被丢弃。 You dispose it yourself at the end of the ImageTest constructor before the paint listener gets the chance to even be called. 您可以在绘画侦听器有机会被调用之前,将其自己放置在ImageTest构造函数的末尾。

You can avoid that by making img a member variable of your class and then override the dispose methos to do the clean up: 您可以通过使img成为类的成员变量,然后重写dispose方法进行清理来避免这种情况:

@Override
public void dispose() {
    this.img.dispose();
    super.dispose();
}

Don't forget to remove the line 别忘了删除行

img.dispose();

from your constructor. 从您的构造函数。

GC.drawImage API document says that in the following cases IllegalArgumentException is being thrown. GC.drawImage API文档说,在以下情况下会引发IllegalArgumentException。 The possibility is that image object is null. 可能是图像对象为空。

IllegalArgumentException -
ERROR_NULL_ARGUMENT - if the image is null
ERROR_INVALID_ARGUMENT - if the image has been disposed
ERROR_INVALID_ARGUMENT - if the given coordinates are outside the bounds of the image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM