JTablet 2.0 SDK

JTablet gives you open-source (zlib license) access to tablet input in your Java applications and Applets.

Table of contents

Getting started

This guide is quick: less reading; more programming!

What you need

Just include jtablet-thin.jar. This tiny (21k) jar includes everything you need to detect and use JTablet!

Out with the old...

This is the only complicated part of JTablet.

JTablet 2 is installed by a user, so you need to ensure a compatible version is installed. Out-dated installations will conflict if you use newer features.

The simplest is to use the checkCompatibility method:

public class DetectionApplet extends Applet {
    public void init() {
        // Displays alert if an incompatible version of JTablet is detected
        if (!JTabletExtension.checkCompatibility(this, "1.2.0")) {
            return;
        }
        add(new Label("Installed JTablet version: " + 
                JTabletExtension.getInstalledVersion()));
    }
}

Listening intently...

JTablet 2 is based on AWT event listeners, specifically MouseListener and MouseMotionListener. If you've used those, you're already a JTablet expert!

Simply use TabletManager to add a TabletListener to a component:

TabletManager.getDefaultManager().addTabletListener(component, listener);

You can either implement TabletListener and all of its methods, or extend TabletAdapter and specify just the methods you need:

TabletListener listener = new TabletAdapter() {
    public void cursorDragged(TabletEvent event) {
    	System.out.println("Cursor was dragged! " + event);
    }
};

The TabletEvent object provides all the necessary information about the cursor that you may need. Get sub-pixel positions with event.getFloatX() and getFloatY(), pressure with getPressure(), and so on.

TabletListener listener = new TabletAdapter() {
    public void cursorDragged(TabletEvent event) {
    	System.out.println("Dragging at " + event.getFloatX() + 
                              "," + event.getFloatY() +
                           " with pressure " + 
                              (event.getPressure() * 100) + "%");
    }
};

Left to your own devices...

JTablet represents different devices (mouse, stylus, stylus eraser, etc.) as different TabletDevice objects, accessed with event.getDevice(). Use device.getType() to get the type of device, and other methods to check capabilities of the device.

TabletListener.cursorEntered triggers when new devices enter tablet proximity:

    public void cursorEntered(TabletEvent event) {
    	System.out.println(event.getDevice() + " entered component");
    }
    public void cursorExited(TabletEvent event) {
    	System.out.println(event.getDevice() + " left component");
    }

I hope that gets you started. Be sure to check out the many more details in the Javadocs! Read on for a step-by-step tutorial to build a drawing Applet.

Step-by-step Tutorial: Build a Drawing Applet

Let's make a drawing Applet using JTablet. We'll use Java2D for the actual drawing, and start by setting up our canvas (BufferedImage) and painting it to the screen:

public class DrawApplet extends Applet {
    
    BufferedImage image;
    Graphics2D graphics;
    
    public void init() {
        // Create a drawing canvas
        image = new BufferedImage(getWidth(), getHeight(), 
                                  BufferedImage.TYPE_INT_ARGB);
        graphics = image.createGraphics();
    }
    
    // Paint the canvas to the screen
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }
}

Next, we'll use TabletAdapter to detect cursor drag events:

    // Create the tablet listener
    TabletListener listener = new TabletAdapter() {
        // Draw circles when the cursor is dragged
        public void cursorDragged(TabletEvent event) {
            float radius = event.getPressure() * 10;
            
            // Create an ellipse using floating points from TabletEvent
            graphics.fill(new Ellipse2D.Float(
                event.getFloatX() - radius,
                event.getFloatY() - radius,
                radius * 2,
                radius * 2
            ));
            
            repaint();
        }
    };            

Then we'll add the listener to the Applet component:

    TabletManager.getDefaultManager().addTabletListener(this, listener);

To make it prettier, we can paint the canvas white and enable anti-aliasing and sub-pixel stroke rendering:

    // Paint it white
    graphics.setBackground(Color.WHITE);
    graphics.clearRect(0, 0, getWidth(), getHeight());
    
    // Enable anti-aliasing and sub-pixel rendering 
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                              RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
                              RenderingHints.VALUE_STROKE_PURE);

Enable eraser drawing

Now let's detect a tablet eraser and switch between drawing black and white. We can do this by adding a cursorEntered event to our listener:

        // Detect when a new cursor enters the canvas
        public void cursorEntered(TabletEvent event) {
            // Set the color to white if the device is an eraser
            if (event.getDevice().getType() == TabletDevice.Type.ERASER) {
                graphics.setColor(Color.WHITE);
            } else {
                graphics.setColor(Color.BLACK);
            }
        }

And that's it...

This should give you a good starting point. Full example source: DrawApplet.java

For Questions