You add a MouseListener to the JFrame, but show the results in JComponent and relative to JComponent. Thus, the click point of the point will refer to the coordinates of the JFrame, but then displayed relative to the coordinates of the JComponent, which are shifted down by the distance of the title bar. Instead, simply add a MouseListener to the same component that is responsible for displaying the results so that the displayed and sliding coordinates match:
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aFrame.setSize(500, 500); aFrame.add(cloud); //!! aFrame.addMouseListener(new ClickListen()); // !! Removed cloud.addMouseListener(new ClickListen()); // !! added aFrame.setVisible(true);
By the way: thanks for creating and publishing a decent SSCCE , as this makes it easier to analyze and solve your problem.
source share