0

我的窗口(SWT)中有以下 UI 布局:

    • 选项卡文件夹(填充布局)
    • 复合(RowLayout(水平)
      • 组(RowLayout,垂直)
        • 标签
        • 标签
        • 文字(宽度:450)
        • 按钮
      • 组(RowLayout,垂直)
        • 标签
        • 标签
        • 画布(这是问题)

所有的 RowLayouts 都具有以下属性:

wrap = true, justify = true, pack = true, marginLeft = 0

我遇到的问题是,如果我尝试将 any 设置Image为(在调用事件canvas时使用 绘制),即使调用了绘制事件(我看到了调用),它通常也根本不显示。如果我设置一个 5 像素的图像并最大化窗口,则图像被绘制,但它也会疯狂地调用绘制事件,并且图像不断闪烁。paintevent.gc.drawImage(0,0)System.out.println()

我在设置图像后调整画布大小,使用:

canvas.setSize(img.getBounds().width, img.getBounds().width);

如果我删除它,那么闪烁和重复的paint调用就消失了,但是我仍然无法显示大于 5x5 的图像,它们根本不显示。

这里发生了什么..?我应该切换到 GridLayout 吗?我基本上只想展示两个组,每个组都包含一个垂直的字段/画布列表。

我的 ImgCanvas 类的代码,用于处理图像的显示:

public class ImgCanvas
{
    private Canvas canvas;
    private Image img; 
    private int lastImgHash = 0;

    public ImgCanvas(Composite parent)
    {
        canvas = new Canvas(parent, SWT.NONE);
        initCanvas();
    }

    public ImgCanvas(Composite parent, Image img)
    {
        canvas = new Canvas(parent, SWT.NONE);
        setImage(img);
        initCanvas();
    }

    public void setCanvas(Canvas canvas)
    {
        this.canvas = canvas;
        this.initCanvas();
    }

    public void setImage(Image img)
    {
        if (this.img != null)
            this.img.dispose();
        this.img = img;

        System.out.println("Set image: " + img.getBounds() + ", " + img.toString());
        redraw();
    }

    public void redraw()
    {
        canvas.redraw();
    }

    protected void initCanvas()
    {
        System.out.println("Canvas started");
        canvas.addPaintListener( getPaintListener() );
        canvas.addDisposeListener( getDisposeListener() );
    }

    protected PaintListener getPaintListener()
    {
        return new PaintListener()
        {
            public void paintControl(PaintEvent e)
            {
                System.out.println("Painting");
                if (img != null )
                {
                    System.out.println("Img:" + img.getBounds() );
                    e.gc.drawImage(img, 0, 0);
                    //canvas.setSize(img.getBounds().width, img.getBounds().width);
                    //canvas.pack();
                }
                else
                    System.out.println("Img is null: " + img);
            }
        };
    }

    protected DisposeListener getDisposeListener()
    {
        return new DisposeListener()
        {
            @Override
            public void widgetDisposed(DisposeEvent e)
            {
                System.out.println("Disposing");
                if (img != null)
                    img.dispose();
            }
        };
    }
}

这是通过以下方式设置的:

imgCanvas = new ImgCanvas(group2); //2nd group in the layout given above.

然后稍后,在组 1(selectionHandler)中按钮的单击处理程序中,完成以下操作:

    public void widgetSelected(SelectionEvent e)
    {
        //get a screenshot of a particular screen region using Java.Awt.Robot.captureScreenRegion,
        //convert the image into a SWT image, and try to show it:
        Image screenshot = ImgUtility.getScreenShot(0,0,10,10); 
        imgCanvas.setImage(screenshot);
        System.out.println("redrawn");
    }
4

2 回答 2

1

演示.java

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import java.awt.Robot.*;

public class Demo {

    static Spinner s;

    public static void main(final String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    FillLayout layout= new FillLayout();
    shell.setLayout(layout);

    Composite comp=new Composite(shell, SWT.BORDER_DOT);
    RowLayout r1=new RowLayout(SWT.HORIZONTAL);
    r1.wrap=true;
    r1.justify=true;
    r1.pack=true;
    r1.marginLeft=0;

    RowLayout r2=new RowLayout(SWT.VERTICAL);
    r2.wrap=true;
    r2.justify=true;
    r2.pack=true;
    r2.marginLeft=0;

    comp.setLayout(r1);
    Group grp1;


    grp1= new Group(comp,SWT.BORDER_DASH);
    grp1.setLayout(r2);
    Label l11,l22;
    Text txt;
    Button btn;

    l11=new Label(grp1, SWT.NONE);
    l11.setText("Label1");
    l22=new Label(grp1, SWT.NONE);
    l22.setText("Label2");
    txt= new Text(grp1, SWT.BORDER);
    btn= new Button(grp1, SWT.PUSH);

    Group grp2;


    Label l1,l2;
    grp2= new Group(comp,SWT.BORDER);
    grp2.setLayout(r2);

    //grp2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    l1= new Label(grp2, SWT.NONE);
    l1.setText("lable1");

    l2= new Label(grp2, SWT.NONE);
    l2.setText("lable1");

    final ImgCanvas imgCanvas = new ImgCanvas(grp2);


    //shell.redraw();
//  shell.setSize(600, 600);
    shell.open();
    shell.layout();

    btn.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e)
        {
            //get a screenshot of a particular screen region using Java.Awt.Robot.captureScreenRegion,
            //convert the image into a SWT image, and try to show it:
            Image screenshot = new Image(display, "c:\\temp\\imgmsg.png"); 
            imgCanvas.setImage(screenshot);
            System.out.println("redrawn");

        }

        @Override
        public void widgetDefaultSelected(SelectionEvent arg0) {
            // TODO Auto-generated method stub

        }
    });
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }  }}

以下是 ImgCanvas.Java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class ImgCanvas
{
    private Canvas canvas;
    private Image img; 
    private int lastImgHash = 0;

    public ImgCanvas(Composite parent)
    {
        canvas = new Canvas(parent, SWT.BORDER);
        initCanvas();
    }

    public ImgCanvas(Composite parent, Image img)
    {
        canvas = new Canvas(parent, SWT.NONE);
       // canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
     //   canvas.layout();
        setImage(img);
        initCanvas();
    }

    public void setCanvas(Canvas canvas)
    {
        this.canvas = canvas;
        this.initCanvas();
    }

    public void setImage(Image img)
    {
        if (this.img != null)
            this.img.dispose();
        this.img = img;

       // canvas.pack();
      // canvas.getParent().getParent().layout();
      //  canvas.getParent().getParent().getParent().layout();
        canvas.getParent().setSize(img.getBounds().width,canvas.getParent().getSize().y);
        canvas.setSize(img.getBounds().width, img.getBounds().height);



        System.out.println("Set image: " + img.getBounds() + ", " + img.toString());
        redraw();
    }

    public void redraw()
    {
        canvas.redraw();
    }

    protected void initCanvas()
    {
        System.out.println("Canvas started");
        canvas.addPaintListener( getPaintListener() );
        canvas.addDisposeListener( getDisposeListener() );
    }

    protected PaintListener getPaintListener()
    {
        return new PaintListener()
        {
            public void paintControl(PaintEvent e)
            {
                System.out.println("Painting");
                if (img != null )
                {
                    System.out.println("Img:" + img.getBounds() );
                    e.gc.drawImage(img, 0, 0);
                 //   canvas.setSize(img.getBounds().width, img.getBounds().width);
                 //   canvas.pack();
                }
                else
                    System.out.println("Img is null: " + img);
            }
        };
    }

    protected DisposeListener getDisposeListener()
    {
        return new DisposeListener()
        {
            @Override
            public void widgetDisposed(DisposeEvent e)
            {
                System.out.println("Disposing");
                if (img != null)
                    img.dispose();
            }
        };
    }
}
于 2013-09-18T14:14:11.313 回答
0

将 的布局数据设置Canvas为一个RowData实例,其中widthheight字段设置为图像的大小。

于 2013-09-18T13:33:54.223 回答