鼠标事件自动转换为相对于它发生的组件,即点0x0始终是组件的左上角.
通过使用RingChart r =((Canvas)e.getSource()).getParent(),您已经有效地更改了引用,现在意味着该位置不再有效.
更新与图片
让我们举个例子……
蓝色框与红色框的相对位置为50px x 50px.如果你点击蓝色框,让我们说在25×25,鼠标坐标将相对于蓝色框(0x0将是蓝色框的左上角).
如果然后将此事件传递给红色框并尝试使用其中的坐标,您会发现坐标现在位于红色框的左上角和蓝色框之间,因为坐标是上下文敏感的.
为了使它工作,您需要将鼠标事件位置从蓝色框转换为红色框,这将使其成为75×75
现在,当你将鼠标事件传递给RingChart时,我不知道你在做什么,所以我只是猜测这是你正面临的问题.
使用点击代码更新
好吧,比方说,你有一个100×100的画布.您以50×50点击该画布.然后,您将该值传递回链中.
public void mouseClick(MouseEvent evt){
//evt = SwingUtilities.convertMouseEvent(this, evt, c);
if(evt.getButton() == MouseEvent.BUTTON1 && animation == null){
for(Element e : elements){
// Here, we are asking the shape if it contains the point 50x50...
// Not 150x150 which would be the relative position of the click
// in the context to the RingChart, which is where all your objects
// are laid out.
// So even the original Canvas you clicked on will return
// false because it's position + size (100x100x width x height)
// does not contain the specified point of 50x50...
if(e.getShape() != null && e.getShape().contains(evt.getPoint())){
//do some stuff
}
}
}
}
更新
我认为你的参考方式错误…
public static?MouseEvent?convertMouseEvent(Component?source,
MouseEvent?sourceEvent,
Component?destination)
我认为它应该读起来像
evt = SwingUtilities.convertMouseEvent(evt.getComponent(), evt, this);
使用代码示例更新
好的,所以,我把这个小例子放在一起……
public class TestMouseClickPoint extends JFrame {
private ContentPane content;
public TestMouseClickPoint() throws HeadlessException {
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
content = new ContentPane();
add(content);
}
protected void updateClickPoint(MouseEvent evt) {
content.updateClickPoint(evt);
}
protected class ContentPane extends JPanel {
private Point relativePoint;
private Point absolutePoint;
public ContentPane() {
setPreferredSize(new Dimension(600, 600));
setLayout(null); // For testing purpose only...
MousePane mousePane = new MousePane();
mousePane.setBounds(100, 100, 400, 400);
add(mousePane);
}
protected void updateClickPoint(MouseEvent evt) {
absolutePoint = new Point(evt.getPoint());
evt = SwingUtilities.convertMouseEvent(evt.getComponent(), evt, this);
relativePoint = new Point(evt.getPoint());
System.out.println(absolutePoint);
System.out.println(relativePoint);
repaint();
}
protected void paintCross(Graphics2D g2d, Point p) {
g2d.drawLine(p.x - 5, p.y - 5, p.x + 5, p.y + 5);
g2d.drawLine(p.x - 5, p.y + 5, p.x + 5, p.y - 5);
}
/*
* This is not recommended, but I want to paint ontop of everything...
*/
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if (relativePoint != null) {
g2d.setColor(Color.BLACK);
paintCross(g2d, relativePoint);
}
if (absolutePoint != null) {
g2d.setColor(Color.RED);
paintCross(g2d, absolutePoint);
}
}
}
protected class MousePane extends JPanel {
private Point clickPoint;
public MousePane() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickPoint = e.getPoint();
TestMouseClickPoint.this.updateClickPoint(e);
repaint();
}
});
setBorder(new LineBorder(Color.RED));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
if (clickPoint != null) {
g2d.drawLine(clickPoint.x, clickPoint.y - 5, clickPoint.x, clickPoint.y + 5);
g2d.drawLine(clickPoint.x - 5, clickPoint.y, clickPoint.x + 5, clickPoint.y);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
new TestMouseClickPoint().setVisible(true);
}
}
基本上,它将描绘三点.单击鼠标的点(相对于事件源),父容器中的未转换点以及父容器的转换点.
您需要做的下一件事是确定鼠标位置实际已被转换,失败了.我可能需要查看代码的工作示例,以确定您实际在做什么.