向所有组件转发事件JAVA [英] Forward event to all components JAVA

查看:139
本文介绍了向所有组件转发事件JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会如何解决这个问题。我正在开发一个图形编辑器,其中arc(线)绘图是可能的。我手动将弧分量的大小设置为1000x1000,所以当这个弧被拖动时,我不必改变它下降。然后我将这个组件绘制成所需尺寸的弧。每个弧组件都安装了鼠标监听器。问题是,只有最上面的弧形组件才能获取鼠标消息。
另外,我有一个鼠标点击监听器安装在JPane本身,但是最上层组件上的鼠标点击监听器只是获取所有的事件...这很难描述这个问题,所以我会提供给你简单的图片





所以第一个解决方案是以某种方式将收到的消息转发到下面的组件,但是我不知道该怎么做,所以你有什么想法?



第二个解决方案是,不要对弧对象设置常量,但是以某种方式旋转该弧的矩形,我的意思就是这样。



,还有可能是重叠的问题。



那么你有什么想法可以解决这个问题吗?或者如果您有任何其他想法,如何解决这个问题,请高兴,我只需要正确的弧线将对鼠标点击做出反应。



我不认为我的代码将以某种方式有所帮助,但这里是

  arcObject.setSize(1000,1000); //! 

然后,在这个组件中我绘制一个弧

  toX = o2.x  -  24 * Math.cos(theta); 
toY = o2.y - 24 * Math.sin(theta);
g2.draw(new Line2D.Double(toX,toY,o1.x,o1.y));


解决方案

我解决了这样:


  1. 将所有创建的弧保存到列表

  2. ,当鼠标单击时,循环遍历所有弧

  3. 创建新的Line2D对象,其中弧的参数保存在列表中,如下所示:



    Line2D line = new Line2D.Double ...)


  4. 检查这个条件



    if(line.ptLineDist(x,y) < 3){
    ... //我们点击了行,做某事



    }



i dont how to solve this problem. I am developing a graphical editor, where arc(line) drawing is possible. I manually set the size of arc component to 1000x1000, so i dont have to change it when this arc will be dragged & dropped. Then i paint into this component the arc of the desired dimensions. Every arc components have mouse listener installed. The problem is, that only the most upper arc component get the mouse message. Also i have a mouse click listener installed on the JPane itself, but the mouse click listener on the most upper component simply gets all the events...It is kinda hard to describe this problem, so i will provide you with the simple image

So the first solution is to somehow forward the received message to the component that lies under, however i dont know how to do this, so do you have any ideas ?

The second solution is, not to set constant dimenstion to the arc object, but somehow rotate the rectangle in which that arc will be, i mean it like this.

however, there still can be the overlapping problem.

So, do you have any ideas how to solve this problem ? or if you have any other ideas, how to solve this, id be happy, i simply need that the correct arc will react to the mouse click.

I dont think that the my code will somehow helps, but here it is

arcObject.setSize(1000, 1000); // !

and then, in this component i draw an arc

toX = o2.x - 24 * Math.cos(theta);
toY = o2.y - 24 * Math.sin(theta);
g2.draw(new Line2D.Double(toX, toY, o1.x, o1.y));

解决方案

I solved it like this:

  1. save all created arcs to the List
  2. when mouse clicked, loop through all the arcs
  3. create new Line2D object, with the parameters of the arc saved in the list, like this:

    Line2D line = new Line2D.Double(...)

  4. check this condition

    if (line.ptLineDist(x, y) < 3) { ... //we clicked on the line, do something

    }

这篇关于向所有组件转发事件JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆