SWT 中画布上的可点击对象图像
我使用图像上的滤镜创建图像
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么最好的方法是在画布上添加一个鼠标控制监听器?
然后控制点击的位置,也许使用 Rectangle.containt(int x, int y);
So the best way is to add a Mouse control listener to the canvas?
And then control where the click is, maybe using the Rectangle.containt(int x, int y);
如果您将对象直接绘制到画布上,即不将它们作为组件/小部件添加到画布上,那么您将需要确定自己单击了哪些对象。出现这种情况是因为画布不知道您在其上绘制的内容。您可以将鼠标侦听器添加到接收单击事件的画布,然后确定这些单击是否位于您绘制的对象的边界内。
您也可以子类 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.