gpt4 book ai didi

java - SWT 中 Canvas 图像上的可点击对象

转载 作者:行者123 更新时间:2023-11-29 06:14:48 25 4
gpt4 key购买 nike

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

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

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

最佳答案

那么最好的方法是在 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
}
});

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

关于java - SWT 中 Canvas 图像上的可点击对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5487953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com