当前位置: 首页 > 知识库问答 >
问题:

SWT画布当重新绘制时不显示图像,除非它的5x5像素

韩烈
2023-03-14

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

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

canvas.setsize(img.getbounds().width,img.getbounds().width);

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

这是怎么回事..?我应该切换到GridLayout吗?我基本上只想显示两个组,每个组将包含一个字段/画布的垂直列表。

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

共有1个答案

班展
2023-03-14

demo.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();
            }
        };
    }
}
 类似资料:
  • 我在CSV文件中有一些数据,想打印出来,但图形没有显示出来 这是CSV文件http://www.mediafire.com/file/2gtbxm5adom7m4j/pop.csv

  • 我有以下代码应该在图像中绘制线条。我的代码是: 如果我编写,它实际上可以工作。但是我的数组很长并且来自一个输入。

  • 因此,我正在创建一个cordova应用程序,在该应用程序中,我从iphone库中拍摄一张照片,将其绘制到画布上,并向其添加另一张图像,以便将其保存为一张照片。到目前为止,我从iphone照片库中绘制的照片可以毫无问题地绘制到画布上,但是第二张图片没有。 当我加载第二张图像时,它首先被添加到具有绝对定位的div中,以便将其移动到我想要的任何位置。之后,我得到了实际的图像,它的来源和位置,并尝试将其绘

  • 我在FrameLayout中的SurfaceView画布上画线。我从相机预览中接收图像,对其进行处理,获取矩形的坐标并在画布上绘制线条。绘制时,我得到这些线在y轴上的位移,线越低,位移越大(见下面的照片): 用红线标记(使用Paint程序,而不是实际的应用程序)下线坐标坐标在位图上的大致位置,矩形的下部绿线(由实际的应用程序放置,在画布上绘制)以及红线显示,坐标移位了多少。顶部的坐标不能穿过屏幕,

  • 我编写了这段代码,可以在JavaFX画布上绘制。它可以很好地工作,但我不知道如何重新绘制画布(比如在Swing中),以便在新画布上重新开始绘制。这是我的代码,非常感谢你的帮助!马里奥

  • 问题内容: 如何在“画布”中打开图像?被编码 我正在使用 输出是已编码的base 64图像。如何在画布上绘制此图像? 我要使用 并创建图片吗?可能吗? 如果不是,那么将图像加载到画布上的解决方案可能是什么? 问题答案: 给定一个数据URL,您可以通过将图像的设置为数据URL来创建图像(在页面上或仅在JS中)。例如: HTML5 Canvas Context 的方法使您可以将图像(或画布或视频)的全