开发者

Mouse Pointer Problem in Java Swing

I have created the following simple Java Swing program which outputs a 3*3 square in the window every time the user clicks their mouse. The squares remain in the window even if the user clicks more than once. The program compiles and runs just fine, however, when one clicks in the window the square is drawn far below where the mouse pointer is. I've been racking my brain over this one for a while -- what can I change here to get the square to appear exactly with the pointer on each click? Many thanks for any help!

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class ClickCloud extends JComponent {

    final ArrayList<Point2D> points = new ArrayList<Point2D>();

    public void addPoint(Point2D a) {
        points.add(a);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for (int i = 0; i < points.size(); i++) {
            Point2D aPoint = points.get(i);
            g2.draw(new Rectangle2D.Double(aPoint.getX(), aPoint.getY(), 3, 3));
        }
    }

    public static void main(String[] args) {
        final ClickCloud cloud = new ClickCloud();
        JFrame aFrame = new JFrame();

        class ClickListen implements MouseListener {

            @Override
            public void mouseClicked(MouseEvent arg0) {
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
            }

            @Override
            public void mouseExited(MouseEvent arg0开发者_如何学Go) {
            }

            public void mousePressed(MouseEvent arg0) {

                cloud.addPoint(arg0.getPoint());
                cloud.repaint();

            }

            @Override
            public void mouseReleased(MouseEvent arg0) {

            }
        }

        aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aFrame.setSize(500, 500);
        aFrame.add(cloud);
        aFrame.addMouseListener(new ClickListen());
        aFrame.setVisible(true);
    }
}


You're adding the MouseListener to the JFrame, but displaying the results in the JComponent and relative to the JComponent. So the location of the Point clicked will be relative to the JFrame's coordinates, but then displayed relative to the JComponent's coordinates which will shift things down by the distance of the title bar. Instead simply add the MouseListener to the same component that is responsible for displaying the results so that the display and clicking 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 posting a decent SSCCE as this makes it so much easier to analyse and solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜