SWT 中画布上的可点击对象图像

发布于 2024-10-29 07:30:47 字数 787 浏览 3 评论 0原文

我使用图像上的滤镜创建图像

private void createContents(final Shell shell) { 
shell.setLayout(new FillLayout());
// Create the canvas for drawing
canvas = new Canvas(shell,SWT.NO_BACKGROUND);


canvas.addPaintListener(new PaintListener() {
  public void paintControl(PaintEvent event) {
  Image image = new Image(shell.getDisplay(), canvas.getBounds());
  Image image2 = new Image(shell.getDisplay(), canvas.getBounds());
  //... I add some figure to the images

  ImageData data = image.getImageData();
  ImageData data2 = image2.getImageData();

  for(int j=0;j<rect.width;j++){
   for(int i=0;i<rect.height;i++){
     if(data.getPixel(j, i)<1){
       data.setPixel(j, i , data2.getPixel(j, i));
      }                 
    }
   }

如何向图中添加一些可点击的对象?

I create an image using a filter on an image

private void createContents(final Shell shell) { 
shell.setLayout(new FillLayout());
// Create the canvas for drawing
canvas = new Canvas(shell,SWT.NO_BACKGROUND);


canvas.addPaintListener(new PaintListener() {
  public void paintControl(PaintEvent event) {
  Image image = new Image(shell.getDisplay(), canvas.getBounds());
  Image image2 = new Image(shell.getDisplay(), canvas.getBounds());
  //... I add some figure to the images

  ImageData data = image.getImageData();
  ImageData data2 = image2.getImageData();

  for(int j=0;j<rect.width;j++){
   for(int i=0;i<rect.height;i++){
     if(data.getPixel(j, i)<1){
       data.setPixel(j, i , data2.getPixel(j, i));
      }                 
    }
   }

How can I add some clickable objects to the figure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-11-05 07:30:47

那么最好的方法是在画布上添加一个鼠标控制监听器?

// Create the canvas for drawing
 canvas = new Canvas(shell,SWT.NO_BACKGROUND);
 canvas.addMouseListener(new MouseListener(){
    @Override
    public void mouseDoubleClick(MouseEvent e) {
       // TODO Auto-generated method stub   
    }
    @Override
    public void mouseDown(MouseEvent e) {
       // TODO Auto-generated method stub
       System.out.println("Click"); 
    }
    @Override
    public void mouseUp(MouseEvent e) {
       // TODO Auto-generated method stub   
    }       
});

然后控制点击的位置,也许使用 Rectangle.containt(int x, int y);

So the best way is to add a Mouse control listener to the canvas?

// Create the canvas for drawing
 canvas = new Canvas(shell,SWT.NO_BACKGROUND);
 canvas.addMouseListener(new MouseListener(){
    @Override
    public void mouseDoubleClick(MouseEvent e) {
       // TODO Auto-generated method stub   
    }
    @Override
    public void mouseDown(MouseEvent e) {
       // TODO Auto-generated method stub
       System.out.println("Click"); 
    }
    @Override
    public void mouseUp(MouseEvent e) {
       // TODO Auto-generated method stub   
    }       
});

And then control where the click is, maybe using the Rectangle.containt(int x, int y);

何其悲哀 2024-11-05 07:30:47

如果您将对象直接绘制到画布上,即不将它们作为组件/小部件添加到画布上,那么您将需要确定自己单击了哪些对象。出现这种情况是因为画布不知道您在其上绘制的内容。您可以将鼠标侦听器添加到接收单击事件的画布,然后确定这些单击是否位于您绘制的对象的边界内。

您也可以子类 Control 与您的可点击对象类。然后,将 mouseListener 添加到自定义控件并将其添加到画布中。

If you are painting the objects directly to the canvas, i.e. not adding them as components/widgets to the canvas, then you will need to determine what objects were clicked on your own. This is the case since the canvas doens't know anything about what you draw to it. You can add a mouse listener to the canvas that receives click events and then determine if any of those clicks are inside of the bounds of the objects that you drew.

You could alternatively subclass Control with your clickable object class. Then, add a mouseListener to your custom Control and add it to the canvas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文