Multi-Touch in Swing
Asked Answered
M

1

6

I was under the impression that a touch screen emulated mouse events, however how would I handle multiple touch events?

I have tried adding a JPanel with a MouseMotionListener and outputting the values to the console. I however, am only getting values for the first touch on the screen.

I then tried splitting the screen down the middle, with a JPanel on either side, and a separate MouseMotionListener on each side, each one outputting to the console.

In this configuration, I still only see the values for the first finger on the screen.

Here is what I have so far:

JPanel jPaneL = new JPanel();
jPaneL.setName("Left");
jPaneL.addMouseMotionListener(mouseMotionL);
jPaneL.setBounds(0, 0, 960, 1280);
jPaneL.setBackground(Color.BLACK);

JPanel jPaneR = new JPanel();
jPaneR.setName("Right");
jPaneR.addMouseMotionListener(mouseMotionR);
jPaneR.setBounds(960, 0, 960, 1080);
jPaneR.setBackground(Color.RED);

jFrame.add(jPaneL);
jFrame.add(jPaneR);

And my MouseMotionListener, L and R are the exact same.

private static MouseMotionListener mouseMotionX = new MouseMotionListener() {

    @Override
    public void mouseDragged(MouseEvent e) {
        String s = ((JPanel) e.getSource()).getName();
        System.out.println(s);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        String s = ((JPanel) e.getSource()).getName();
        System.out.println(s);
    }
};

I have found libraries that support multi-touch, but I am looking to do this without using third-party libraries. How can I go about doing this using just native mouse listeners?

Millerite answered 3/3, 2014 at 22:27 Comment(4)
JavaFX -- you will thank me later ;-P -- not but in all seriousness, Swing is very old and may not do what you want for modern touch screen devices. JavaFX (the Swing replacement in Java8) has a lot of built-in things that help address touch screen devices. You can use JavaFX with java7, you just need to include the jfxrt.jar lib in your classpath.Adame
I primarily do Android-Dev, and learned Swing a while ago, so that is how I was attempting to build this for my surface. Thanks, I will look into using JavaFX.Millerite
it's very similar to swing syntax-wise if you decide to code it in java... another option with javafx is you can write you ui using FXML, a subset of xml and is extremely quick to write out a complex UI, then in java code you just add functionality. Check out SceneBuilder for a UI that lets you drag-n-drop to create a FXML document (its official from oracle). Also you can skin your program using CSS if you go with javafx. there's plenty to love :)Adame
My layout is going to be very dynamic, so this seems like it will be the best approach. I will post back with my findings as I learn JavaFX. Thanks again. :) In the meantime, if anyone posted an answer for Swing, I would not be disappointed. Haha.Millerite
T
3

First I want to say that I would really recommend using JavaFX for such an application. You can also integrate your JavaFX project in existing swing applications using JFXPanel.

However I have heard about MT4j which is a framework for multitouch in Java application which also supports Swing. I have not worked with it. So I can not tell you how good it works.

Tribunal answered 18/7, 2014 at 20:0 Comment(1)
Some sample code: mt4j.googlecode.com/svn/trunk/examples/basic/javaGUI/…Tribunal

© 2022 - 2024 — McMap. All rights reserved.