Why are only the co-ordinates of the mouse displayed?

111 views Asked by At

when the Thread thread is running, (after clicking record) it only displays the position the mouse was in when the thread started? how can i make it constantly update, displaying where the mouse is even if i move it around the frame?

@Override public void actionPerformed(ActionEvent e)
{       
    thread = new Thread(this);

    if(e.getSource() == record)
    {
        thread.start();
        System.out.println("record");
    }

    if(e.getSource() == stopRecording)
    {
        setVisible(false);
        System.out.println("stop recording");
    }

}

@Override public void run()
{       
    setTitle("979");

    setSize(screen.width, screen.height);
    addMouseListener(this);
    setLocationRelativeTo(null);
    setLayout(transFlo);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    add(stopRecording);     
    setOpacity(0.50f);      
    setVisible(true);

    while(true)
    {
        repaint();
    }

}

@Override public void paint(Graphics g)
{
    g.drawString(mousePOS + x + space + y, 250, 250);
}
2

There are 2 answers

2
MadProgrammer On

I put together another example. This is essentially a mouse monitor, it shows that, if done correctly, you don't need the Thread

public class MouseFrame extends javax.swing.JFrame implements AWTEventListener, ActionListener {

    private boolean monitor = false;
    private Point mousePoint;

    /**
     * Creates new form MouseFrame
     */
    public MouseFrame() {

        setLayout(new GridBagLayout());

        JButton btnToggle = new JButton("Start");
        add(btnToggler);

        btnToggle.addActionListener(this);

        setSize(400, 400);

    }

    public void actionPerformed(java.awt.event.ActionEvent evt) {

        monitor = !monitor;

        if (monitor) {

            btnTrigger.setText("Stop");
            Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_MOTION_EVENT_MASK);

        } else {

            btnTrigger.setText("Start");
        Toolkit.getDefaultToolkit().removeAWTEventListener(this);

    }

    }

    @Override
    public void paint(Graphics grphcs) {

        super.paint(grphcs);

        Graphics2D g2d = (Graphics2D) grphcs;

        if (monitor) {

            g2d.setColor(Color.RED);
            FontMetrics fm = g2d.getFontMetrics();
        g2d.drawString(mousePoint.x + "x" + mousePoint.y, 25, fm.getHeight() + 25);

    }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MouseFrame().setVisible(true);
            }
        });
    }

    @Override
    public void eventDispatched(AWTEvent evt) {

        if (evt instanceof MouseEvent) {

            MouseEvent me = (MouseEvent) evt;
            mousePoint = SwingUtilities.convertPoint(me.getComponent(), me.getPoint(), this);

            repaint();

        }
    }
}
1
MadProgrammer On

Okay, just to reiterate; PLEASE read The Event Dispatching Thread,

then read Concurrency in Swing

and finally have a read of How to Write a Mouse Listener

ADDINTIONAL

If you want to monitor the global mouse events (all mouse events that pass through the system), then you will want to take a look at Toolkit.addAWTEventListener

This will allow you to monitor all the mouse events without the need to attach mouse listeners to all the components

SIMPLE MOUSE EXAMPLE

Here is a simple example of a panel that monitors the mouse :P

public class CrayPanel extends javax.swing.JPanel implements MouseMotionListener, MouseListener {

    private List<Point> lstPoints;

    /**
     * Creates new form CrayPanel
     */
    public CrayPanel() {

        lstPoints = new ArrayList<Point>(25);

        addMouseListener(this);
        addMouseMotionListener(this);

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        if (lstPoints.size() > 1) {

            Graphics2D g2d = (Graphics2D) g;

            g2d.setColor(Color.RED);
            Point startPoint = lstPoints.get(0);
            for (int index = 1; index < lstPoints.size(); index++) {

                Point toPoint = lstPoints.get(index);
                g2d.drawLine(startPoint.x, startPoint.y, toPoint.x, toPoint.y);

                startPoint = toPoint;

            }

        }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        lstPoints.clear();
        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mouseExited(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }
}