问题描述
在 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 对象上的鼠标单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!