Application Class Design

How To Create A Java Application With Touch Handling

Create a new Java App without adding code, as explained in How To Create A Basic Java Application.

Add a new java class to the project

Add a java file

Click on the Add new symbol in the iLCD Manager XE. Store the new file with the name TouchButton.java inside the iLCD project folder.
A new java class with the name TouchButton.java is added to the Java App, and will be displayed in the Java App tree view.

Add the following code to the project to create a simple touch button which prints a message on the screen.

Add code to the TouchButton class:

In order to handle touch events the class the TouchButton class has to implement the OnTouchListener interface.This is done by adding implements OnTouchListener to the class declaration.

public class TouchButton implements OnTouchListener

Import all classes from the javax.events and the hw package.

import javax.events.*;

import hw.*;

Add a counter variable to class TouchButton class.

  private int countMethodCall;

Add a constructor to the TouchButton class.

  public TouchButton() throws ILCDException
  {
    // draw a rectangle for a button on the screen
    Control.setCursorPosition(200, 30);
    Attribute.setBackgroundColor(0x00FF00);
    Draw.drawRectangle(
         Draw.RECTANGLE_FILLED_WITH_BACKGROUND_COLOR, 125, 50);

    // write a string on the button
    Control.setCursorPosition(210, 45);
    Draw.writeText("Push the button!");

    // create a touch field over the rectangle
    Control.setCursorPosition(200, 30);
    EventManagement.getTouchEventDispatcher().addListener(this);
    Touch.setTouchFieldHeight(50);
    Touch.setTouchFieldWidth(125);
    Touch.createDefineTouchField(1, 0);

    countMethodCall = 0;
  }

Now the interface method OnTouch() has to be implemented.

  public void onTouch(TouchEvent event)
  {
    try
    {
       if (event.isTouchReleased())
       {      
          // count each button press
          countMethodCall++;            

          // write a string on the button
          Control.setCursorPosition(0, 45);
          Draw.writeText(" " + countMethodCall +
                         ". onTouch() call \r");
       }
    }
    catch(ILCDException exc)
    {
       Logger.e(exc.getMessage());         
    }
  }   


Add code to the constructor of the App class:

    try
    {
       General.resetAll();
       Control.setCursorPosition(0, 45);
       Draw.writeText("0. onTouch() call \r");

       // enable touch field reporting
       Touch.setTouchFieldReportingEnabled(true);

       // create an instance of the button class
       TouchButton button = new TouchButton();                 
    }
    catch(Exception exc)
    {
       Logger.e(exc.getMessage());
    }

Put the following code into the constructor App(). Note that touch field reporting has to be enabled with setTouchFieldReportingEnabled.

After coding is done

Run the program

Click the Run button after the coding is done. Now, the panel draws a first message and a button with another message to the panel. When the button on the panel is pressed the first message changes the text and the background color of this message. The first message changes the text every time when the button is pressed. Set a breakpoint in the OnTouch() method to verify the touch event handling.