問(wèn)題描述
在 Java 中實(shí)現(xiàn)Kings' Corners"(美化多人紙牌).
Implementing "Kings' Corners" (glorified multiplayer Solitaire) in Java.
我試圖讓玩家將一張牌(圖像)從他們的手中拖到桌子上的其他地方.問(wèn)題是玩家的手是扇形的",因此卡片的圖像會(huì)旋轉(zhuǎn)并且它們重疊.
I'm trying to allow a player to drag a card (image) from their hand to somewhere else on the table. The problem is that the player's hand is "fanned" so the images of the cards are rotated and they overlap.
這是一手牌的例子:
我考慮過(guò)將每張卡片設(shè)為 JPanel
,但問(wèn)題是我必須在其 矩形 JPanel<內(nèi)繪制旋轉(zhuǎn)的卡片/code>,因?yàn)樗鼈儽旧聿荒苄D(zhuǎn).理想情況下,我想避免使用鼠標(biāo) x,y 公式來(lái)確定選擇哪張卡.
I've considered making each card a JPanel
, but the issue then is that I'd have to paint the card rotated inside its rectangular JPanel
, as they themselves can't be rotated. Ideally I'd like to avoid mouse-x,y formulas to determine which card is being chosen.
使用事件驅(qū)動(dòng)的方法,我如何確定從手牌中選擇哪張牌?
Using an event-driven approach, how can I determine which card is chosen from the hand?
推薦答案
AWT(和 Swing)組件通常是矩形的(與軸對(duì)齊).
AWT (and Swing) components are normally rectangular (aligned to the axes).
但這不一定是這樣 - 雖然實(shí)際邊界必須是矩形,但組件使用的實(shí)際區(qū)域可以更小.組件支持 contains(Point)
方法,只要出現(xiàn)點(diǎn)屬于哪個(gè)組件的問(wèn)題(例如,鼠標(biāo)單擊),事件調(diào)度機(jī)制就會(huì)調(diào)用該方法.(不同組件的重疊將由父容器內(nèi)的 z-order 處理.)
But this does not have to be the case - while the real bounds must be rectangular, the actual area which a component uses can be smaller. Component supports a contains(Point)
method, which will get called by the event dispatch mechanism whenever the question arises to which component a point belongs - for example, for mouse clicks. (Overlapping of different components will be handled by the z-order inside the parent container.)
您可以基于 Shape.contains()
方法實(shí)現(xiàn)此方法,使用仿射變換的矩形作為您的形狀.每個(gè)旋轉(zhuǎn)的組件都將知道自己的形狀(或從其 AffineTransform 動(dòng)態(tài)生成它,它也將用于繪畫(huà)本身).
You can implement this method based on the Shape.contains()
method, using a affine transformed rectangle as your shape. Each of your rotated components would know its own shape (or generate it on the fly from its AffineTransform, the same one which would also be used for painting itself).
有一個(gè)自定義的 LayoutManager 也可以安排您的卡片.(不要使用 CardLayout,盡管有這個(gè)名字.)
Have a custom LayoutManager which arranges your cards, too. (Don't use CardLayout, despite the name.)
我不確定我是否會(huì)遵循為每張卡片設(shè)置單獨(dú)組件的方式,但您肯定需要一些代表旋轉(zhuǎn)矩形的對(duì)象.
I'm not sure I would follow the way of having separate components for each card, but you certainly need some objects which represent the rotated rectangles.
這篇關(guān)于如何在 Java 中檢測(cè) Image 對(duì)象上的鼠標(biāo)單擊事件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!