Implementation

For information about the design and functionality of the MIDlet, see section Design.

To implement multipoint touch handling in the MIDlet:

  1. Enable multipoint touch events by setting the following attribute in the MIDlet JAD file:

    Nokia-UI-Enhancement: EnableMultiPointTouchEvents
  2. Catch touch events by overriding selected Canvas methods. Every event creates a new Point instance whose state depends on the event type.

        protected void pointerPressed(int x, int y)
        {
            int pointerId = this.getPointerEventId();
            this.addPoint(pointerId, new Point(x, y, Point.PRESSED));
            this.exitButtonPointerDown = this.exitButtonHitTest(x, y);
            this.inactivityCounter[pointerId] = 0;
        }
    
        protected void pointerDragged(int x, int y)
        {
            int pointerId = this.getPointerEventId();
            this.addPoint(pointerId, new Point(x, y, Point.DRAGGED));
            this.exitButtonPointerDown = this.exitButtonHitTest(x, y);
            this.inactivityCounter[pointerId] = 0;
        }
    
        protected void pointerReleased(int x, int y)
        {
            int pointerId = this.getPointerEventId();
            this.addPoint(pointerId, new Point(x, y, Point.RELEASED));
            if (this.exitButtonPointerDown && this.exitButtonHitTest(x, y))
            {
                this.inactivityTimer.cancel();
                Display.getDisplay(this.midlet).setCurrent(null);
                this.midlet.notifyDestroyed();
            }
            else
            {
                this.exitButtonPointerDown = false;
            }
            this.inactivityCounter[pointerId] = 0;
        }
  3. Obtain the pointer number of the pointer that was the source of the event by calling the System.getProperty("com.nokia.pointer.number") method. This method exists only in the scope of the methods pointerPressed, pointerDragged, and pointerReleased. Outside these methods, the returned value is null.

        private int getPointerEventId()
        {
            String idString = System.getProperty("com.nokia.pointer.number");
            int id = 0;
            if (idString != null)
            {
                id = Integer.parseInt(idString);
            }
            return id;
        }

    The currentPoints array stores the Points:

    private Point[][] currentPoints = new Point[6][25];

In conclusion, this example supports six simultaneous touches and each of them can have a maximum of 25 circles drawn on the screen to create the worm-like animation. When a point is created, it is stored to the correct index of the currentPoints array, depending on the pointer number.