問題描述
在 Java 中實現Kings' Corners"(美化多人紙牌).
Implementing "Kings' Corners" (glorified multiplayer Solitaire) in Java.
我試圖讓玩家將一張牌(圖像)從他們的手中拖到桌子上的其他地方.問題是玩家的手是扇形的",因此卡片的圖像會旋轉并且它們重疊.
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.
這是一手牌的例子:
我考慮過將每張卡片設為 JPanel
,但問題是我必須在其 矩形 JPanel<內繪制旋轉的卡片/code>,因為它們本身不能旋轉.理想情況下,我想避免使用鼠標 x,y 公式來確定選擇哪張卡.
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.
使用事件驅動的方法,我如何確定從手牌中選擇哪張牌?
Using an event-driven approach, how can I determine which card is chosen from the hand?
推薦答案
AWT(和 Swing)組件通常是矩形的(與軸對齊).
AWT (and Swing) components are normally rectangular (aligned to the axes).
但這不一定是這樣 - 雖然實際邊界必須是矩形,但組件使用的實際區域可以更小.組件支持 contains(Point)
方法,只要出現點屬于哪個組件的問題(例如,鼠標單擊),事件調度機制就會調用該方法.(不同組件的重疊將由父容器內的 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()
方法實現此方法,使用仿射變換的矩形作為您的形狀.每個旋轉的組件都將知道自己的形狀(或從其 AffineTransform 動態生成它,它也將用于繪畫本身).
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).
有一個自定義的 LayoutManager 也可以安排您的卡片.(不要使用 CardLayout,盡管有這個名字.)
Have a custom LayoutManager which arranges your cards, too. (Don't use CardLayout, despite the name.)
我不確定我是否會遵循為每張卡片設置單獨組件的方式,但您肯定需要一些代表旋轉矩形的對象.
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.
這篇關于如何在 Java 中檢測 Image 對象上的鼠標單擊事件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!