将事件转发到所有组件 JAVA

Forward event to all components JAVA(将事件转发到所有组件 JAVA)

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

问题描述

我不知道如何解决这个问题.我正在开发一个图形编辑器,可以在其中绘制弧(线).我手动将弧组件的大小设置为 1000x1000,因此当拖动此弧时我不必更改它&掉了.然后我将所需尺寸的弧线绘制到该组件中.每个弧组件都安装了鼠标监听器.问题是,只有最上面的弧组件才能获得鼠标消息.我还在 JPane 本身上安装了一个鼠标单击侦听器,但是最上面的组件上的鼠标单击侦听器只会获取所有事件...很难描述这个问题,所以我将为您提供简单的图像

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

推荐答案

我是这样解决的:

  1. 将所有创建的弧保存到列表中
  2. 当鼠标点击时,循环遍历所有弧线
  3. 创建新的 Line2D 对象,将圆弧的参数保存在列表中,如下所示:

  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 线 = 新 Line2D.Double(...)

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

检查这个条件

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

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

}

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

本文标题为:将事件转发到所有组件 JAVA

基础教程推荐