views:

58

answers:

1

I'm writing a class that will allow users on other computers to control the contents of a JPanel, for use in a shared display system. java.awt.Robot will allow me to perform mouse clicks and keyboard input, but it does not have a mouseDragged(MouseEvent evt) method. My client has clientMouseReleased (and Pressed) methods as part of a MouseAdapter listener that look like:

private void clientMouseReleased(java.awt.event.MouseEvent evt)
{
    try
    {
        switch (evt.getButton())
        {
            case 1:
                remoteDesktop.mouseRelease(evt.getX(), evt.getY(), InputEvent.BUTTON1_MASK);
                break;
            case 2:
                remoteDesktop.mouseRelease(evt.getX(), evt.getY(), InputEvent.BUTTON2_MASK);
                break;
            case 3:
                remoteDesktop.mouseRelease(evt.getX(), evt.getY(), InputEvent.BUTTON3_MASK);
                break;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

Where the remoteDesktop is a class containing a java.awt.Robot, and it executes a Robot.mouseMove and Robot.mouseRelease (or Robot.mousePress) in method the client calls. I'd like to be able to write the mouseDragged in the same way, i.e., like this:

private void clientMouseDragged(java.awt.event.MouseEvent evt)
{
    try
    {
        switch (evt.getButton())
        {
            case 1:
                remoteDesktop.mouseDragged(evt.getX(), evt.getY(), InputEvent.BUTTON1_MASK);
                break;
            case 2:
                remoteDesktop.mouseDragged(evt.getX(), evt.getY(), InputEvent.BUTTON2_MASK);
                break;
            case 3:
                remoteDesktop.mouseDragged(evt.getX(), evt.getY(), InputEvent.BUTTON3_MASK);
                break;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}
A: 

Well, the high level idea is that you need to add mouse state to your program.

Your remoteDesktop will need to keep the state of the mouse.

private boolean mouseDown = false;

Then, on mouse press and release actions you need to set the flag appropriately.

Then, you need to add logic in the mouse move code.

if (mouseDown) 
    handelDrag();
else 
    handleMove();

That's the general idea of how I would go about it.

jjnguy