問題描述
我不知道如何解決這個(gè)問題.我正在開發(fā)一個(gè)圖形編輯器,可以在其中繪制弧(線).我手動(dòng)將弧組件的大小設(shè)置為 1000x1000,因此當(dāng)拖動(dòng)此弧時(shí)我不必更改它&掉了.然后我將所需尺寸的弧線繪制到該組件中.每個(gè)弧組件都安裝了鼠標(biāo)監(jiān)聽器.問題是,只有最上面的弧組件才能獲得鼠標(biāo)消息.我還在 JPane 本身上安裝了一個(gè)鼠標(biāo)單擊偵聽器,但是最上面的組件上的鼠標(biāo)單擊偵聽器只會(huì)獲取所有事件...很難描述這個(gè)問題,所以我將為您提供簡單的圖像
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
所以第一個(gè)解決方案是以某種方式將收到的消息轉(zhuǎn)發(fā)到下面的組件,但是我不知道該怎么做,所以你有什么想法嗎?
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 ?
第二種解決方案是,不要為弧對象設(shè)置恒定尺寸,而是以某種方式旋轉(zhuǎn)該弧所在的矩形,我的意思是這樣.
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.
那么,您對如何解決這個(gè)問題有任何想法嗎?或者如果您有任何其他想法,如何解決這個(gè)問題,我很高興,我只需要正確的弧線會(huì)對鼠標(biāo)點(diǎn)擊做出反應(yīng).
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.
我不認(rèn)為我的代碼會(huì)有所幫助,但在這里
I dont think that the my code will somehow helps, but here it is
arcObject.setSize(1000, 1000); // !
然后,在這個(gè)組件中,我畫了一個(gè)圓弧
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));
推薦答案
我是這樣解決的:
- 將所有創(chuàng)建的弧保存到列表中
- 當(dāng)鼠標(biāo)點(diǎn)擊時(shí),循環(huán)遍歷所有弧線
創(chuàng)建新的 Line2D 對象,將圓弧的參數(shù)保存在列表中,如下所示:
- save all created arcs to the List
- when mouse clicked, loop through all the arcs
create new Line2D object, with the parameters of the arc saved in the list, like this:
Line2D 線 = 新 Line2D.Double(...)
Line2D line = new Line2D.Double(...)
檢查這個(gè)條件
if (line.ptLineDist(x, y) < 3) {...//我們點(diǎn)擊了就行了,做點(diǎn)什么
if (line.ptLineDist(x, y) < 3) { ... //we clicked on the line, do something
}
這篇關(guān)于將事件轉(zhuǎn)發(fā)到所有組件 JAVA的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!