Handling touch events in Java on Mac OS X

Two days ago I discovered that Apple has implemented multi touch support with the Java update 2 for Snow Leopard and update 7 for Leopard into the Java API. Immediately I had to play with it and have written following little sample application. Further I’ve discovered that Apple also implemented support to detect horizontal scrolling, by abusing the shift modifier on MouseWheelEvents. It is quit fun to use and I have the idea of an MultiTouch Tetris („Touchris“) floating around in my head 😉

package net.saraarauhito.gesturetest;
 
import com.apple.eawt.event.GestureUtilities;
import com.apple.eawt.event.MagnificationEvent;
import com.apple.eawt.event.MagnificationListener;
import com.apple.eawt.event.RotationEvent;
import com.apple.eawt.event.RotationListener;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
 
/**
 *
 * @author Jan-Peter Zurek
 */
public class Gesturetest {
 
    public static JFrame f;
    public static double rot = 0;
    public static Rectangle r = new Rectangle(125, 125, 50, 50);
    public static BufferedImage img;
 
    public static void main(String[] args) throws Exception {
        // Put your own image here
        img = ImageIO.read(new File("your_image.jpg"));
 
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
            f = new JFrame();
            f.setSize(300, 300);
            JPanel p = new JPanel();
            p.setSize(300, 300);
            f.add(p);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            f.createBufferStrategy(2);
 
            f.addMouseWheelListener(new MouseWheelListener() {
 
                /* Apple added support for horizontal scrolling by
                 * using the shift modifier
                 */
                public void mouseWheelMoved(MouseWheelEvent e) {
                    if(!e.isShiftDown()) {
                        if(e.getWheelRotation() < 0)                             
                            r.y -= 2;
                         if(e.getWheelRotation() > 0)
                            r.y += 2;
                    } else {
                        if(e.getWheelRotation() < 0)
                             r.x -= 2;
                         if(e.getWheelRotation() > 0)
                            r.x += 2;
                    }
 
                    draw();
                }
 
            });
 
            GestureUtilities.addGestureListenerTo(p, new MagnificationListener() {
 
                public void magnify(MagnificationEvent e) {
                    if(e.getMagnification() > 0) {
                        r.setBounds((int)r.getX() - 1, (int)r.getY() - 1,
                                (int)r.getWidth() + 2, (int)r.getHeight() + 2);
                    } else if (e.getMagnification() < 0) {
                         r.setBounds((int)r.getX() + 1, (int)r.getY() + 1,
                                 (int)r.getWidth() -2, (int)r.getHeight() - 2);
                     }
                     draw();
                 }
             });
             GestureUtilities.addGestureListenerTo(p, new RotationListener() {
                     public void rotate(RotationEvent e) {
                         if(e.getRotation() > 0)
                            rot -= 2;
                        if(e.getRotation() < 0)
                            rot += 2;
 
                        draw();
                    }
                });
            }
        });
    }
 
    private static void draw() {
        Graphics2D g = (Graphics2D) f.getGraphics();
        int mx = r.x + (r.width / 2);
        int my = r.y + (r.height / 2);
        g.clearRect(0, 0, 300, 300)
        g.rotate(rot / 100, mx, my);
        g.setColor(Color.red);
        g.drawImage(img, r.x, r.y, r.width, r.height, null);
        g.rotate(-(rot / 100), mx, my);
        g.dispose();
    }
}

Use rotation and zoom gesture to rotate and zoom the picture and move it with two fingers around the window.

Category: Java, Macintosh, Programmieren One comment »

One Response to “Handling touch events in Java on Mac OS X”

  1. liudonghua

    It’s really helpful for me, but when I run on MacOSX 10.9, I just got a blank window!


Leave a Reply



 

Back to top