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

📄 devicescreen.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		// Continue processing the pointer event.
		super.pointerPressed( x, y );
	}
	
	/**
	 * Called when a stylus drags across 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 pointerDragged (int x, int y)
	{
		// Adjust the press location to fit in the canvas area.
		int py = y;
		
		if ( master.hasTitleBar() )
		{
			Theme theme = UIManager.getTheme();
			py -= theme.getTitleHeight();
		}
		
		// Notify the master.
		master.pointerDragged( x, py );
		
		// Forward the pointer event.
		super.pointerDragged( x, y );
	}

	/**
	 * Called when a stylus is lifted off 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 pointerReleased (int x, int y)
	{
		// Adjust the press location to fit in the canvas area.
		int py = y;
		
		if ( master.hasTitleBar() )
		{
			Theme theme = UIManager.getTheme();
			py -= theme.getTitleHeight();
		}
		
		// Notify the master.
		master.pointerReleased( x, py );
		
		// Forward the pointer event.
		super.pointerReleased( x, y );
	}
	
	/**
	 * Sets the title bar text.  This is called by the <code>master</code> when
	 * its title is changed.
	 * 
	 * @param title is the text that appears in the title bar across the
	 *  top of the screen.
	 *  
	 * @see DeviceScreen#setTitle(String)
	 */
	public void setTitle (String title)
	{
		// Does this JVM support our title bar feature?
		if ( supportsTitleBar() == false )
		{
			// These JVMs always shows a title bar.  We might as well use that
			// instead of painting a duplicate on our own.
			super.setTitle( title );
		}
		
		// For other JVMs we'll display the title on our own.  There is no
		// need to have the LCDUI do it for us.
	}
	
	/**
	 * Sets the menu bar text.  This is called by the <code>master</code> when
	 * its menu is changed.
	 * 
	 * @param left is the text for the negative menu option or <code>null</code>
	 *  to remove the button.  Negative menu options are things like canceling
	 *  a form and moving back to a previous screen.
	 * @param right is the text for the positive menu option or <code>null</code>
	 *  to remove the button.  Positive menu options are things like accepting
	 *  a form, advancing to the next screen, or displaying a menu.
	 *  
	 * @see DeviceScreen#setMenuText(String, String)
	 */
	public void setMenuText (String left, String right)
	{
		// Does this JVM support our own menu bar?
		if ( supportsMenuBar() == false )
		{
			// BlackBerry phones do not have MIDP 2.0 left and right menu buttons.
			// We must capture the BlackBerry phone's "Menu" and "Return" buttons
			// using the LCDUI menu functionality.
			//
			// IBM's J9 does not forward the left and right menu button codes to
			// us.  We have to use LCDUI menu functionality.
			
			// Remove the existing menu commands.
			if ( lcduiLeftMenuCommand != null )
			{
				removeCommand( lcduiLeftMenuCommand );
				lcduiLeftMenuCommand = null;
			}
			
			if ( lcduiRightMenuCommand != null )
			{
				removeCommand( lcduiRightMenuCommand );
				lcduiRightMenuCommand = null;
			}
			
			// Register new LCDUI menu commands.
			if ( left != null )
			{
				int position;
				
				if ( blackberry )
				{
					// This will be hooked up to the "Return" key and appear
					// second on the BlackBerry menu.
					position = 2;
				}
				else  // ibmJ9
				{
					// This will make the button appear on the left side of the menu.
					position = 1;
				}
				
				lcduiLeftMenuCommand = new Command( left, Command.CANCEL, position );
				addCommand( lcduiLeftMenuCommand );
			}
			
			if ( right != null )
			{
				int position;
				
				if ( blackberry )
				{
					// This will appear first on the BlackBerry menu.
					position = 1;
				}
				else  // ibmJ9
				{
					// This will make the button appear on the right side of the menu.
					position = 2;
				}
				
				lcduiRightMenuCommand = new Command( right, Command.OK, position );
				addCommand( lcduiRightMenuCommand );
				
				// Add a dummy left menu for IBM's J9 JVM.  Otherwise the right
				// menu would actually be on the left.
				if ( (ibmJ9 || tao) && (left == null) )
				{
					lcduiLeftMenuCommand = new Command( "", Command.CANCEL, 1 );
					addCommand( lcduiLeftMenuCommand );
				}
			}
		}
	}

	/**
	 * Indicates that a command event <code>c</code> has occurred on
	 * <code>Displayable<code> d.
	 * 
	 * @param c is a <code>Command</code> object identifying the command.
	 * @param d is the <code>Displayable</code> on which this event has occurred.
	 *  It will always be <code>this</code> screen.
	 *  
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction (Command c, Displayable d)
	{
		// Was it our left menu command?
		if ( (lcduiLeftMenuCommand != null) && (c == lcduiLeftMenuCommand) )
		{
			keyPressed( DeviceScreen.MENU_LEFT );
			keyReleased( DeviceScreen.MENU_LEFT );
		}
		
		// Was it our right menu command?
		if ( (lcduiRightMenuCommand != null) && (c == lcduiRightMenuCommand) )
		{
			keyPressed( DeviceScreen.MENU_RIGHT );
			keyReleased( DeviceScreen.MENU_RIGHT );
		}
	}
	
	/**
	 * Returns if the device supports having a menu bar at the bottom of the
	 * screen.  If not then no menu bar will painted at the bottom of the
	 * screen and a standard LCDUI menu will be used instead.
	 * 
	 * @return <code>true</code> if the device supports a menu bar or <code>false</code>
	 *  if it does not.
	 */
	public boolean supportsMenuBar ()
	{
		if ( blackberry || ibmJ9 || tao )
		{
			// These JVMs do not show our menu bar at the bottom of the
			// screen.  Instead they use the LCDUI menu system.
			return false;
		}
		else
		{
			// This phone can have a menu bar at the bottom of the screen.
			return true;
		}
	}
	
	/**
	 * Returns if the device supports having a title bar at the top of the
	 * screen.  If not then no title bar will painted at the top of the
	 * screen and a standard LCDUI title will be used instead.
	 * 
	 * @return <code>true</code> if the device supports a title bar or <code>false</code>
	 *  if it does not.
	 */
	public boolean supportsTitleBar ()
	{
		if ( ibmJ9 || tao )
		{
			// These JVMs always show a title bar.  We might as well use it.
			return false;
		}
		else
		{
			// This phone can have a title bar at the top of the screen.
			return true;
		}
	}
	
	/**
	 * Called when this screen is no longer going to be displayed.
	 * 
	 * @see javax.microedition.lcdui.Canvas#hideNotify()
	 */
	protected void hideNotify ()
	{
		// If the key repeat timer is running, stop it.
		stopRepeatTimer();

		// Don't highlight the menu options (in case we return to this screen).
		highlightLeftMenu = false;
		highlightRightMenu = false;
		
		// Continue to hide the screen.
		super.hideNotify();
	}

	/**
	 * Forces a repaint of the menu bar.
	 * 
	 * @param immediate when <code>true</code> finishes the painting before
	 *  this method returns; <code>false</code> does it in the normal paint
	 *  cycle.
	 */
	private void repaintMenuBar (boolean immediate)
	{
		Theme theme = UIManager.getTheme();
		int menuHeight = theme.getMenuHeight();
		int y = getHeight() - menuHeight;

		this.repaint( 0, y, getWidth(), menuHeight );
		
		if ( immediate )
		{
			this.serviceRepaints();
		}
	}
	
	/**
	 * Paints the screen.  If not in full screen mode, this includes the
	 * title bar and menu bar.
	 * <p>
	 * Typically derviced classes will only override the <code>paintCanvas</code>
	 * method.
	 * 
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @see #paintTitleBar(Graphics, String, int, int, int, int)
	 * @see #paintBackground(Graphics, int, int, int, int)
	 * @see #paintCanvas(Graphics, int, int, int, int)
	 * @see #paintMenuBar(Graphics, String, String, int, int, int, int)
	 */
	protected void paint (Graphics g)
	{
		try
		{
			// Get some painting attributes.
			Theme theme = UIManager.getTheme();
			
			int width = getWidth();
			int height = getHeight();
	
			int titleHeight = 0;
			int menuHeight = 0;
			
			String title = null;
			String leftMenuText = null;
			String rightMenuText = null;
	
			// Get the original clip.
			int clipX = g.getClipX();
			int clipY = g.getClipY();
			int clipWidth = g.getClipWidth();
			int clipHeight = g.getClipHeight();
			
			// Paint the title bar and/or menu bar?
			if ( master.isFullScreenMode() == false )
			{
				// Paint the title bar at the top of the screen.
				title = master.getTitle();
				
				if ( master.hasTitleBar() )
				{
					// We'll paint the title after the canvas area.
					titleHeight = theme.getTitleHeight();
				}
				
				// Paint the menu bar at the bottom of the screen.
				if ( master.hasMenuBar() )
				{
					// Set the menu text.
					leftMenuText = master.getLeftMenuText();
					rightMenuText = master.getRightMenuText();
				
					if ( leftMenuText == null )
					{
						leftMenuText = "";
					}
					
					if ( rightMenuText == null )
					{
						rightMenuText = "";
					}
					
					// Set the height of the menu.
					menuHeight = theme.getMenuHeight();
				}
	
				// Set the graphics object for painting the canvas area.
				height = height - titleHeight - menuHeight;
				
				g.translate( 0, titleHeight );
				g.clipRect( 0, 0, width, height );
			}
			
			// Paint the canvas area.
			if ( DeviceScreen.intersects(g, 0, 0, width, height) )
			{
				master.paintBackground( g );
			
				g.setFont( theme.getFont() );
				g.setColor( theme.getFontColor() );
				master.paint( g );
			}
	
			// Restore the original graphics object.
			g.translate( 0, -titleHeight );
			g.setClip( clipX, clipY, clipWidth, clipHeight );
	
			// Paint the title bar.
			//  We do this after the canvas so it will paint over any
			//  canvas spillage.
			if ( titleHeight > 0 )
			{
				if ( DeviceScreen.intersects(g, 0, 0, width, titleHeight) )
				{
					// Set the graphics object for painting the title bar.
					g.clipRect( 0, 0, width, titleHeight );
					
					// Actually paint the title bar.
					master.paintTitleBar( g, title, width, titleHeight );
		
					// Restore the original graphics object.
					g.setClip( clipX, clipY, clipWidth, clipHeight );
				}
			}
	
			// Paint the menu bar.
			if ( menuHeight > 0 )
			{
				int y = getHeight() - menuHeight;
				
				if ( DeviceScreen.intersects(g, 0, y, width, menuHeight) )
				{
					// Set the graphics object for painting the menu bar.
					g.translate( 0, y );
					g.clipRect( 0, 0, width, menuHeight );
	
					// Clear the background first.
					//   On the Sony Ericsson w810i things drawn accidentally
					//   over the menu space can show through gradient backgrounds.
					//   Clear the area completely first.
					int menuBackgroundColor = theme.getMenuBarBackgroundColor();
					g.setColor( menuBackgroundColor );
					g.fillRect( 0, 0, width, menuHeight );
					
					// Actually paint the menu bar.
					master.paintMenuBar( g,
							leftMenuText, highlightLeftMenu,
							rightMenuText, highlightRightMenu, 
							width, menuHeight );
					
					// Restore the original graphics object.
					g.translate( 0, -y );
					g.setClip( clipX, clipY, clipWidth, clipHeight );
				}
			}
		}
		catch (Throwable t)
		{
			// Unhandled exception in paint() will crash an application and not
			// tell you why.  This lets the programmer know what caused the problem.
			String name = master.getTitle();
			
			if ( name == null )
			{
				name = master.getClass().getName();
			}
			
			Log.warn("Unhandled exception in paint for " + name, t);
		}
	}
}

⌨️ 快捷键说明

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