Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Advanced touch techniques

[ Edited ]

Hi everyone,

 finally I managed to build  touch and gestures sample app. Now, it's more a layout question - how do I put one field above another? Take a look at this code & shot. Now I use simple VerticalFieldManager and add text label and my *touch handler field*. But what if I want text label to sit in the center of the blue bitmap? And thinking further, I'd like to add complicated graphics on the top layer while being able to handle touch events. Is the only way to do this  - many horizontal and vertical field managers sitiing in each other? 

 

package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchGesture;
import net.rim.device.api.ui.VirtualKeyboard;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.TouchEvent;

public class MyScreen extends MainScreen
{	
	LabelField touch_type;
	public boolean onMenu(int instance) {
		return instance == Menu.INSTANCE_CONTEXT ? false : super.onMenu(instance);
		}
	
    public MyScreen()
    {    
    	super(NO_SYSTEM_MENU_ITEMS);
    	getScreen().getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE_FORCE);
    	
    	VerticalFieldManager vf = new VerticalFieldManager();
    	touch_type = new LabelField("SOME TEXT", FIELD_HCENTER);
    	vf.add(touch_type);
    	vf.add(new HandleTouch());
    	add(vf);
    }
    
    class HandleTouch extends Field {

		protected void layout(int width, int height) {
			setExtent(360, 460);
		}

		public void paint(Graphics graphics) {
			graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(), Bitmap.getBitmapResource("bg.png"), 0, 0);
		}
    	
		protected void drawFocus(Graphics g, boolean on) {}
		
		public boolean isFocusable() { return true;}
		
		protected boolean touchEvent(TouchEvent message) {
			switch( message.getEvent() ) {
			case TouchEvent.CLICK:
				System.out.println("-------------->CLICK");
				touch_type.setText("CLICK");
				return true;
			case TouchEvent.DOWN:
				System.out.println("-------------->DOWN");
				touch_type.setText("DOWN");
				return true;	
			case TouchEvent.MOVE:
				System.out.println("--------------->MOVE");
				touch_type.setText("MOVE");
				return true;	
			case TouchEvent.UNCLICK:
				System.out.println("---------------->UNCLICK");
				touch_type.setText("UNCLICK");
				return true;
			case TouchEvent.GESTURE:
				TouchGesture gesture = message.getGesture();
				int gestureCode = gesture.getEvent();
					switch (gestureCode) {
					case TouchGesture.HOVER:
						System.out.println("------>HOVER");
						touch_type.setText("HOVER");
						return true;
					case TouchGesture.SWIPE:
						System.out.println("------>SWIPE");
						touch_type.setText("SWIPE");
						return true;	
					case TouchGesture.TAP:
						System.out.println("------>TAP");
						touch_type.setText("TAP");
						return true;
					case TouchGesture.CLICK_REPEAT:
						System.out.println("----->CLICK REPEAT");
						touch_type.setText("CLICK REPEAT");
						return true;
					case TouchGesture.DOUBLE_TAP:
						System.out.println("------>DOUBLE TAP");
						touch_type.setText("DOUBLE TAP");
						return true;	
				}	
			}
			//System.out.println("PRINT ME SOMETHING IN ANY CASE");
			super.touchEvent(message);
			return false;
		}
		
		public HandleTouch() {
		}
    }
      
}

   Clip.jpg

 

    

  

Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Advanced touch techniques

In addition to my question, I wanted to draw a simple rectangle on click event. But it throws me an error and doesn't draw anything:

 

Graphics g; //in the HandleTouch class
case TouchEvent.CLICK:
		int x = message.getGlobalX(1);
		int y = message.getGlobalY(1);
		g.setColor(Color.YELLOW);
		g.drawRect(x, y, 50, 50);
		touch_type.setText("CLICK");
		return true;

   Error it throws -  "NullPoint Exception". I'm sure I'm doing something wrong, could you tell me what exactly?

Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Advanced touch techniques

Nobody knows how to draw one field above another or simple rectangle on touch??

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Advanced touch techniques

Try storing the rectangle information and calling invalidate. Then draw the rectangle there.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Advanced touch techniques

Thanx for replying. I think I don't get what you mean, can you please explain where should I store rectangle info? Should I call invalidate() and drawRect() in the click touch event? 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Advanced touch techniques

Store the X, Y position of the rectangle in a variable above the layout function. Move the draw code you have in the touchEvent function into the paint function. After you set the text to "CLICK", call invalidate.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Advanced touch techniques

Great! Smiley Happy Thank you very much, works perfectly. BTW, may be you know the answer to my 1st question about multiple graphics layers. Imagine that I want many different pngs to sit on the background png. How do I do this? Using multiple managers and bitmap fields? But let's say I want then drag one of this png over the screen, what'll happen? As I understand, managers provide some kind of a grid with fields, and how do we drag one field over the grid in arbitrary directions?
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Advanced touch techniques

When you want some image in the background and other images in the foreground, don't create a BitmapField with that background image - add it to the manager with setBackground (need OS 4.6+ for that) or draw it in paint or paintBackground.

 

More information here:

Use a background image in application screens

Make sure you read the comments if you want to scroll the screen on top of an immobile image.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!