java - SWT Canvas 在重绘时不显示图像,除非其像素为 5x5

标签 java swt

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

  • 壳牌
    • TabFolder(填充布局)
    • 复合(RowLayout(水平))
      • 组(行布局、垂直)
        • 标签
        • 标签
        • 文本(宽度:450)
        • 按钮
      • 组(行布局、垂直)
        • 标签
        • 标签
        • Canvas (这就是问题所在)

所有 RowLayout 都具有以下属性:

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

我遇到的问题是,如果我尝试设置任何 Imagecanvas (在调用 paint 事件时使用 event.gc.drawImage(0,0) 进行绘制),即使调用了绘制事件,它通常也根本不显示(我看到 System.out.println() 调用)。如果我设置一个5像素的图像并最大化窗口,那么图像就会被绘制,但它也会疯狂地调用绘画事件,并且图像不断闪烁。

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

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

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

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

我的 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");
    }

最佳答案

演示.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();
            }
        };
    }
}

关于java - SWT Canvas 在重绘时不显示图像,除非其像素为 5x5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871660/

相关文章:

java - java swt表中的动态组合框列表

java - 使用 Jackson Parser 从 JSON 代码获取属性

java - 从 JDateChooser 获取值作为文件名

Java class.getResource().getPath() 在 URL 的开头添加了一个奇怪的 '/'

swt - 禁用和灰显 SWT 复合

java - 如何使用 Java 中的 GUI(仅限 SWT)标记矩形的一侧?

java - SWT<->AWT 图像转换之间的透明度

java - Ajax 不工作 JSF 2.2

java - 如何让组合框说一件事但放入另一件事?

java - Antlr4 在简单语法上没有找到任何可行的输入字符串替代方案