Category: Java


Java: Checking which scrolling direction is enabled in Mac OS Lion

August 16th, 2011 — 8:08pm

With the introduce of Mac OS Lion Apple added the option to enable „natural scrolling“ which simply inverts the scrolling direction of input devices to get an iOS like scrolling behavior. However, some Apps may require to ‚invert the inverted scrolling‘, like Apps which use scrolling for zooming, which indeed should not be inverted. Unfortunately Apple dropped the development of Java and didn’t added a method or property to get the scrolling behaviour.

So today I tried to find a solution which helps to find out if natural scrolling is enabled or not in Java. I know that the preference would be saved in a .plist file so I checked with fseventer which one it was. After this the only thing I had to do was finding a library which allowed me to read .plist files and to check the scrolling property.

You will need this library to run the snippet.

try {
    File globalPref = new File(System.getProperty("user.home") + "/Library/Preferences/.GlobalPreferences.plist");
    NSDictionary dict = (NSDictionary)PropertyListParser.parse(globalPref);
 
    NSNumber pref = (NSNumber)dict.objectForKey("com.apple.swipescrolldirection");
 
    if(pref.boolValue()) {
        //natural scrolling is enabled
    }
 
 
} catch (Exception ex) {
    System.out.println("Faild to parse plist: " + ex.getMessage());
}

Hopefully this small snippet will be useful.

1 comment » | Digitale Welt, Java, Macintosh, Programmieren

Handling touch events in Java on Mac OS X

Oktober 30th, 2010 — 12:58pm

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.

1 comment » | Java, Macintosh, Programmieren

Back to top