0

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);
}
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
JAVA
  • 29
  • 5

2 Answers2

0

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();

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • If you want to monitor the mouse outside your frame, you'll want to look @ http://docs.oracle.com/javase/7/docs/api/java/awt/MouseInfo.html#getPointerInfo() this will need to be polled via a thread, but please, only update ui components from within the EDT – MadProgrammer Jul 27 '12 at 06:47
0

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();

        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366