Archive for Oktober 2010


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

jResizer for Mac released

Oktober 25th, 2010 — 10:32pm

Today I publish my first application for OS X, called jResizer. Recently I had to resize some images and stumbled upon the fact that the preview.app of OS X doesn’t offer any option to select the sampling mode for resizing an image like billinear or bicubic. Even the famous pixelmator doesn’t offer this option.

As I’ve recently pushed my Java knowledge a little, I decided to write a small, fully OS X integrated, Java application over the weekend for easy resizing pictures and the possibility to select between the different sampling modes.

You can use drag and drop to drop an image into the app or just press Command-O or the choose button to select an image. Change the resolution, press save, save as, command-s or shift-command-s and you will find your image saved as a .png file.

I’m not quit sure, but I believe that my App will require that you have the latest Java update from Apple installed.

For future releases I plan to ingrate several file formats for saving, unit selection (pixel/cm/inch) and batch resizing.

I want to thank Kenneth Orr from Exploding Pixels for his great work on MacWidgets, which I’ve slightly modified for my use.

jResizer Download

Comment » | Macintosh, Programmieren

Back to top