Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Touch sample app

The touch sample application demonstrates how to create and display an image that changes in response to customized touch events. If you touch the screen and slide your finger to another location on the screen, the application draws a line to trace the movement. You can click menu items to specify the width and color of the lines and to erase the canvas.

File name

Description

TouchDemo.java

This file defines the TouchDemo class.

The TouchDemo class extends the UiApplication class to support a user interface and contains the following constructor and methods:

  • main(String[] args): provides the entry point to the sample application and starts the main thread for the application using enterEventDispatcher() from UiApplication
  • TouchDemo(): constructs a new TouchDemo object, and contains the code to check if the BlackBerry device has a touch screen using the Touchscreen.isSupported() method, and if it does, creates a TouchDemoScreen object and displays it

TouchDemoScreen.java

This file defines the TouchDemoScreen class and the ColorChangeDialog and WidthChangeDialog nested classes.

The TouchDemoScreen class extends the MainScreen class to support screen characteristics that are common to standard BlackBerry applications and contains the following constructor and methods:

  • TouchDemoScreen(): constructs a new TouchDemoScreen object, and contains the code that initializes an SVGImage object and creates and adds menu items for erasing the screen and for changing the width and color of the line
  • touchEvent(TouchEvent message): overrides the Screen class's implementation of the touchEvent(TouchEvent message) method so that when a touch event occurs, the sample application identifies the location and type of touch event, and performs actions depending on the type of touch event
  • drawLine(String color, int width, int x, int y): draws lines that trace TouchEvent.MOVE events
  • sublayout(int height, int width): overrides the Manager class's implementation of sublayout(int height, int width) to set the layout of the screen and the viewport dimensions of the SVGImage object depending on the orientation of the BlackBerry device
  • paint(Graphics graphics): overrides the Field class's implementation of the paint(Graphics graphics) method to repaint the screen each time a TouchEvent.MOVE event occurs

The ColorChangeDialog and WidthChangeDialog nested classes both extend the Dialog class to support the basic functionality of a dialog box. ColorChangeDialog and WidthChangeDialog contain the code for creating dialog boxes that allow the BlackBerry device user to change the color and width of the line.

This sample features the following classes:

net.rim.device.api.ui.TouchEvent

This class contains methods and constants for handling basic touch events that the BlackBerry device user inputs.

The sample application demonstrates how to use the getX() and getY() methods to retrieve the location of the touch event and the getEvent() method to identify the type of touch event.

net.rim.device.api.ui.Screen

This class contains the method touchEvent(TouchEvent message), which is invoked to handle touch events when they occur on a Screen object.

The sample application demonstrates how to override this method to customize the behavior of touch events. In the sample application, when a TouchEvent.MOVE event follows a TouchEvent.DOWN event, the application draws a line on the screen to trace the movement.

Install the sample app

Visit www.blackberry.com/go/touchsample to download the sample application.

Extract the touchdemo.zip file.

On the taskbar, click Start > Programs > Research In Motion > BlackBerry JDE 4.7.0 > JDE.

Open the workspace that you want to add the sample application to.

In the workspace tree, right-click the project that you want to add the sample application to.

Click Add project to <workspace directory>.

In the Add project to <workspace> dialog box, browse to the folder where you extracted the sample application.

Click the touchdemo.jdp file.

Click Open.

The sample app is also available in github.

Run the sample app

In the workspace where you added the touchdemo project, right-click touchdemo.

Click Build project.

On the taskbar, click Start > Applications > Research in Motion > BlackBerry JDE 4.7.0 > Device Simulator.

On the Home screen of the BlackBerry Smartphone Simulator, click the Downloads folder.

Click the Touch Demo icon.