为什么我的鼠标事件坐标返回错误的值?

时间:2015-05-04 01:07:26

标签: java swing mouseevent

我有两个课程:Connection extends JComponentPostMachineView extends JPanel implements MouseListener。 我试图做的是在Connection组件内按下鼠标按钮时触发方法。 我使用SwingUtilities.convertPointToScreen,以便鼠标事件坐标相对于我绘制的位置。问题是它返回的xy数字略有不同。 代码如下:

public class PostMachineView extends JPanel implements MouseListener {
    ArrayList<Connection> connections;

    public PostMachineView() {
        connections = new ArrayList<>();
        setSize(new Dimension(800, 800));
        setBackground(Color.white);

        //here i create the points i'll need for the connection and instantiate it
        Point p1 = new Point(100, 100);
        Point p2 = new Point(100, 200);
        Connection conn = new Connection(p1, p2);
        //add the connection component to this panel
        add(conn);
        //set the connection mouse listener
        conn.addMouseListener(this);

        connections.add(conn);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        //this will be done in a different way, just trying to get it to work first
         Point p1 = new Point(100, 100);
         Point p2 = new Point(100, 200);

         ((Connection)getComponent(0)).paintComponent(g, p1, p2);
    }

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("clicked in the component");
    }

    @Override
    public void mouseReleased(MouseEvent e) {}
}

public class Connection extends JComponent {

    private Rectangle2D.Double curveControl;
    private GeneralPath path;

    public Connection(Point p1, Point p2) {
        path = new GeneralPath();
        path.moveTo(p1.getX(), p1.getY());
        int[] mid = getMidPointCoords(p1, p2);
        curveControl = new Rectangle2D.Double(mid[0] - 5, mid[1] - 5, 10, 10);
        path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
                curveControl.getCenterX(), curveControl.getCenterY(),
                p2.getX(), p2.getY());

    }
    public int[] getMidPointCoords(Point p1, Point p2) {
        int[] v = { 0, 0 };
        v[0] = (int) ((p1.getX() + p2.getX()) / 2);
        v[1] = (int) ((p1.getY() + p2.getY()) / 2);
        return v;
    }

    public void paintComponent(Graphics g, Point p1, Point p2) {
        path.moveTo(p1.getX(), p1.getY());
        path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
                curveControl.getCenterX(), curveControl.getCenterY(),
                p2.getX(), p2.getY());

        Graphics2D g2d = (Graphics2D) g;

        g2d.draw(path);
        g2d.setColor(Color.red);
        g2d.fill(curveControl);
        g2d.setColor(Color.black);
    }

    @Override
    public boolean contains(int x, int y) {
    //here is where i use swingutilities to convert the coordinates
        Point p = new Point(x, y);
        SwingUtilities.convertPointToScreen(p, this);
        return curveControl.contains(p.getX(), p.getY());
    }
}

我还有一个Teste类,它只是实例化一个JFrame并向其添加PostMachineView

public class Teste  {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(new Dimension(800,800));
        f.add(new PostMachineView());
        f.setResizable(false);
        f.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:2)

此...

Connection

这是一个非常糟糕的主意。 PostMachineView组件已经是Connection组件的子组件。您应该只是更新mouseClicked的状态(可能通过repaint事件)并致电Connection进行更新。

public void paintComponent(Graphics g, Point p1, Point p2) { path.moveTo(p1.getX(), p1.getY()); path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(), curveControl.getCenterX(), curveControl.getCenterY(), p2.getX(), p2.getY()); Graphics2D g2d = (Graphics2D) g; g2d.draw(path); g2d.setColor(Color.red); g2d.fill(curveControl); g2d.setColor(Color.black); } ,这......

public void add(Point p1, Point p2) {
    path.moveTo(p1.getX(), p1.getY());
    path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
                    curveControl.getCenterX(), curveControl.getCenterY(),
                    p2.getX(), p2.getY());
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.draw(path);
    g2d.setColor(Color.red);
    g2d.fill(curveControl);
    g2d.setColor(Color.black);
    g2d.dispose();
}

应该分为两种方法,一种是添加新点,另一种是绘制它...

MouseListener

由于Connection已附加到@Override public boolean contains(int x, int y) { //here is where i use swingutilities to convert the coordinates Point p = new Point(x, y); SwingUtilities.convertPointToScreen(p, this); return curveControl.contains(p.getX(), p.getY()); } 组件,因此...

public boolean contains(int x, int y) {
    return curveControl.contains(x, y);
}

不再有意义,说实话,我会非常小心搞乱这个

充其量它应该更像......

MouseListener

但请记住,curveControl将不再生成事件,除非x / y坐标位于ArrayList<Connection> connections;内,这可能不是您想要的。

哦,这{{1}}让我感到害怕......但是,我没有你的问题的背景,真正知道你的意图是什么......