⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicescreen.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	 * the BlackBerry experience, we need to create a standard LCDUI menu
	 * which the BlackBerry framework ties to these buttons.
	 */
	private static boolean blackberry;
	
	/**
	 * When <code>true</code> this is running in IBM's J9 JVM (also known as
	 * WEME or WebSphere Everyplace Micro Edition).  When <code>false</code>
	 * it is not.
	 * <p>
	 * For Windows Mobile and Palm phones the only reliable J2ME JVM is
	 * IBM's J9 JVM.  There are some other good implementations that
	 * come with some phones, such as the Motorola Q which uses Motorola's
	 * JVM.
	 * <p>
	 * J9 does not support detecting soft menu buttons.  We need to use
	 * its title area and menu bar instead of our own to capture these
	 * events.  This also hooks into the design of these devices more
	 * closely so the application resembles others on the phone.
	 */
	private static boolean ibmJ9;
	
	/**
	 * When <code>true</code> this is running on Tao's JVM.  When <code>false</code>
	 * it is not.
	 * <p>
	 * The Tao JVM runs on Windows Mobile and behaves similarly to IBM's J9.
	 */
	private static boolean tao;
	
	/**
	 * The screen that uses this object for screen operations.
	 * <p>
	 * This should only be <code>null</code> when using a dummy screen to get
	 * the dimensions.  For more information see the <code>sizeChanged</code>
	 * method's comments for more information.
	 */
	private final DeviceScreen master;
	
	/**
	 * Executes the <code>keyRepeated</code> job every <code>REPEAT_PERIOD</code>.
	 * This value will be <code>null</code> if the user is not holding down
	 * any keys.
	 * <p>
	 * Note we have to create new <code>Timer</code> objects every time we
	 * repeat keys.  It does not work to create a timer once here and
	 * then try to reuse it.  <i>To prevent multiple timer objects
	 * synchronize access on <code>this</code> when dealing with
	 * <code>keyRepeatTimer</code>.</i>
	 */
	private Timer keyRepeatTimer = null;
	
	/**
	 * If <code>true</code> the left menu button text should be highlighted.  This
	 * happens when the user presses the left menu button to indicate the
	 * event was received.  Normally this will be <code>false</code> showing no
	 * menu work is in progress.
	 */
	private boolean highlightLeftMenu;
	
	/**
	 * If <code>true</code> the right menu button text should be highlighted.  This
	 * happens when the user presses the right menu button to indicate the
	 * event was received.  Normally this will be <code>false</code> showing no
	 * menu work is in progress.
	 */
	private boolean highlightRightMenu;

	/**
	 * When we do not paint the menu bar, such as with BlackBerries and IBM's
	 * J9 JVM, this contains the left menu choice.  Otherwise this will be
	 * <code>null</code>.
	 * 
	 * @see #lcduiRightMenuCommand
	 */
	private Command lcduiLeftMenuCommand;
	
	/**
	 * When we do not paint the menu bar, such as with BlackBerries and IBM's
	 * J9 JVM, this contains the right menu choice.  Otherwise this will be
	 * <code>null</code>.
	 * 
	 * @see #lcduiLeftMenuCommand
	 */
	private Command lcduiRightMenuCommand;
		
	/**
	 * Static initializer for data that is shared by all screens.
	 */
	static
	{
        String platform = System.getProperty( "microedition.platform" );
        platform = platform.toLowerCase();
        
		// Check if running on a BlackBerry.
		try
		{
			Class.forName( "net.rim.device.api.ui.UiApplication" );
			blackberry = true;
		}
		catch (ClassNotFoundException e)
		{
			blackberry = false;
		}
		
		// Check if running on IBM's J9 JVM.
		try
		{
			Class.forName( "java.lang.J9VMInternals" );
			ibmJ9 = true;
		}
		catch (ClassNotFoundException e)
		{
			ibmJ9 = false;
		}
		
		// Check if running on Tao's JVM.
		if ( platform.indexOf("intent") > -1 )
		{
			tao = true;
		}
		else
		{
			tao = false;
		}
	}
	
	/**
	 * Constructs a wrapper for a <code>Canvas</code>.
	 * 
	 * @param master is the J4ME screen that uses this object. 
	 */
	public CanvasWrapper (DeviceScreen master)
	{
		this.master = master;
		
		// Always remove the menu bar and replace with our own.
		//  There are special cases, like BlackBerry's and IBM's J9
		//  on Windows Mobile, where this is not true.  These are
		//  handled by setMenuText() and other methods.
		setFullScreenMode( true );
		
		// Register for getting LCDUI menu commands.
		setCommandListener( this );
	}

	/**
	 * Causes the <code>keyRepeated</code> method to fire on devices that do
	 * not natively support it.
	 */
	private final class KeyRepeater
		extends TimerTask
	{
		private int key;
		
		public KeyRepeater (int key)
		{
			this.key = key;
		}
		
		public void run ()
		{
			if ( master.isShown() )
			{
				try
				{
					master.keyRepeated( key );
				}
				catch (Throwable t)
				{
					t.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * If <code>keyRepeatTimer</code> is running, this method stops it.
	 */
	private synchronized void stopRepeatTimer ()
	{
		if ( keyRepeatTimer != null )
		{
			keyRepeatTimer.cancel();
			keyRepeatTimer = null;
		}
	}
	
	/**
	 * Called when the user presses any key.  This method checks
	 * for joystick movements which work the Yardage Anywhere
	 * cursor.
	 * 
	 * @param key is code of the key that was pressed.
	 */
	protected void keyPressed (int key)
	{
		int translatedKey = translateKeyCode( key );

		// Stop simulating key repeated events.
		//   Holding a key down, then pressing another key, will stop
		//   the first key's keyReleased() method from being called on
		//   some phones like Sony Ericssons.  Kill the repeat timer
		//   here so that key no longer receives keyRepeat() events.
		stopRepeatTimer();
		
		// Notify the master.
		master.keyPressed( translatedKey );
		
		// If this is a menu key raise an event.
		if ( translatedKey == DeviceScreen.MENU_LEFT )
		{
			// Highlight the menu option immediately.
			if ( master.hasMenuBar() )
			{
				highlightLeftMenu = true;
				repaintMenuBar( true );
			}
			
			// Raise a menu event.
			master.declineNotify();
		}
		else if ( translatedKey == DeviceScreen.MENU_RIGHT )
		{
			// Highlight the menu option immediately.
			if ( master.hasMenuBar() )
			{
				highlightRightMenu = true;
				repaintMenuBar( true );
			}
			
			// Raise a menu event.
			master.acceptNotify();
		}
		
		// Do not forward the key event!
		//  super.keyPressed and .keyReleased can cause platform-specific
		//  and often undesirable behavior.  For example on the Sony Ericsson
		//  w810i they can turn on the music player or browser.
		
		// Start a timer for generating key repeat events.
		synchronized ( this )  // synchronize so we don't ever create more than one timer
		{
			keyRepeatTimer = new Timer();
			keyRepeatTimer.schedule( new KeyRepeater(translatedKey), REPEAT_PERIOD, REPEAT_PERIOD );
		}
	}
	
	/**
	 * Called when the user releases any key.
	 * 
	 * @param key is code of the key that was released.
	 */
	protected void keyReleased (int key)
	{
		// Stop simulating key repeated events.
		stopRepeatTimer();
		
		// Notify the master.
		int translatedKey = translateKeyCode( key );
		master.keyReleased( translatedKey );
		
		// If this is a menu key stop highlighting it.
		if ( master.hasMenuBar() )
		{
			if ( translatedKey == DeviceScreen.MENU_LEFT )
			{
				highlightLeftMenu = false;
				repaintMenuBar( false );
			}
			else if ( translatedKey == DeviceScreen.MENU_RIGHT )
			{
				highlightRightMenu = false;
				repaintMenuBar( false );
			}
		}
		
		// Do not forward the key event!
		//  super.keyPressed and .keyReleased can cause platform-specific
		//  and often undesirable behavior.  For example on the Sony Ericsson
		//  w810i they can turn on the music player or browser.
	}

	/**
	 * Maps key values to the constants defined in the outter class.
	 * 
	 * @param key is code of the button pressed.
	 * @return The integer value for the key.
	 */
	private int translateKeyCode (int key)
	{
		// Translate key codes used on the Tao JVM.
		if ( tao )
		{
			// Center joystick button is a Return.
			if ( key == 13 )
			{
				return FIRE;
			}
		}
		
		// Is it a normal key?
		//   BlackBerry devices give the trackwheel and trackball movements
		//   as values through 6 and getGameAction does not translate them.
		//   There are no ASCII values used below 8 (backspace) so this is
		//   fine.
		if ( key > 6 )
		{
			return key;
		}
		
		// Is it a well defined game key such as a joystick movement?
		int action;
		
		try
		{
			action = getGameAction( key );
		}
		catch (Exception e)
		{
			// Some phones throw an exception for unsupported keys.
			// For example the Sony Ericsson K700.
			return key;
		}

		if ( action != 0 )
		{
			// We make all action keys negative.  This allows code to
			// check for special keys by seeing if the value is less
			// than 0.
			return -1 * action;
		}
			
		// Is it the left menu button?
		if ( (key == -6) || (key == -21) || (key == -1) )
		{
			// -6:   The Sun WTK emulator and Sony Ericcson phones
			// -21:  Motorola phones such as the SLVR
			// -1:   Siemens
			return DeviceScreen.MENU_LEFT;
		}
		
		// Is it the right menu button?
		if ( (key == -7) || (key == -22) || (key == -4) )
		{
			// -7:   The Sun WTK emulator and Sony Ericcson phones
			// -22:  Motorola phones such as the SLVR
			// -4:   Siemens
			return DeviceScreen.MENU_RIGHT;
		}
		
		// Otherwise it is undefined such as:
		//   Motorola center "Menu" soft key:  -23
		//   Sony Ericsson "Return" soft key under the left soft key:  -11
		//   Sony Ericsson "Clear" soft key under the right soft key:  -8
		//   Sony Ericsson "Camera" key on side of phone:  -25
		//   Sony Ericsson "Volume Up" key on side of phone:  -36
		//   Sony Ericsson "Volume Down" key on side of phone:  -37
		//   Sony Ericsson "Play/Pause Music" key on side of phone:  -23 (Note this is the same as Motorola's center menu button)
		//   Sony Ericsson "Music Player" key:  -22 (Note this is the same as Motorola's right menu button)
		//   Sony Ericsson "Internet Browser" key:  (Unavailable)
		return key;
	}
	
	/**
	 * Called when a stylus presses the screen.
	 *
	 * @param x is the horizontal location where the pointer was pressed
	 * @param y is the vertical location where the pointer was pressed
	 */
	protected void pointerPressed (int x, int y)
	{
		Theme theme = UIManager.getTheme();
		boolean processed = false;
		
		// Was the stylus pressed over a menu item?
		if ( master.hasMenuBar() )
		{
			int menuHeight = theme.getMenuHeight();
			int menuStart = super.getHeight() - menuHeight;
			
			if ( y > menuStart )
			{
				// The user clicked on the menu.
				int width = super.getWidth();
				
				if ( x < (width / 2) )
				{
					// The left menu item was clicked.
					master.declineNotify();
				}
				else
				{
					// The right menu item was clicked.
					master.acceptNotify();
				}
				
				processed = true; 
			}
		}
		
		// Was the stylus pressed over the title bar?
		boolean hasTitle = master.hasTitleBar();
		
		if ( (processed == false) && (hasTitle == true) )
		{
			int titleHeight = theme.getTitleHeight();
			
			if ( y < titleHeight )
			{
				// Ignore clicks on the title bar.
				processed = true;
			}
		}

		// Notify the master.
		if ( processed == false )
		{
			// Adjust the press location to fit in the canvas area.
			int py = y;
			
			if ( hasTitle )
			{
				py -= theme.getTitleHeight();
			}
			
			// Forward the event.
			master.pointerPressed( x, py );
		}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -