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

📄 devicescreen.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	{
		// Full screen mode off?
		if ( fullScreenMode == false )
		{
			// There is some menu text?
			if ( (leftMenu != null) || (rightMenu != null) )
			{
				// Device supports menus?
				if ( slave.supportsMenuBar() )
				{
					return true;
				}
			}
		}
		
		// If we made it here no menu bar should be displayed.
		return false;
	}
	
	/**
	 * Returns the width of the usuable portion of this canvas.  The usable
	 * portion excludes anything on the sides of the screen such as scroll
	 * bars.
	 * 
	 * @return The number of pixels wide the usable portion of the canvas is.
	 */
	public int getWidth ()
	{
		return slave.getWidth();
	}
	
	/**
	 * Returns the height of the usuable portion of this canvas.  The usable
	 * portion excludes the title area and menu bar unless this canvas has been
	 * set to full screen mode.
	 * 
	 * @return The number of pixels high the usable portion of the canvas is.
	 */
	public int getHeight ()
	{
		Theme theme = UIManager.getTheme();
		
		// Get the height of the entire canvas.
		int height = getScreenHeight();

		// Remove the height of the title bar. 
		if ( hasTitleBar() )
		{
			height -= theme.getTitleHeight();
		}
			
		// Remove the height of the menu bar.
		if ( hasMenuBar() )
		{
			height -= theme.getMenuHeight();
		}
		
		return height;
	}
	
	/**
	 * Gets the width of the entire screen in pixels.
	 * <p>
	 * <i>Platform bug note.</i>  Motorola and early Nokia phones return the
	 * incorrect size until after the first screen has actually been displayed.
	 * So, for example, calling this from a constructor before any screen has
	 * been displayed will give incorrect data.  The workaround is to put up
	 * another screen first, such as a splash screen.
	 * 
	 * @return The number of pixels wide the entire screen is.
	 */
	public int getScreenWidth ()
	{
		return slave.getWidth();
	}

	/**
	 * Gets the height of the entire screen in pixels.  This includes
	 * the title area at the top of the screen and menu bar at the bottom.
	 * Use <code>getHeight</code> to get the actual usable area of the canvas.
	 * <p>
	 * <i>Platform bug note.</i>  Motorola and early Nokia phones return the
	 * incorrect size until after the first screen has actually been displayed.
	 * So, for example, calling this from a constructor before any screen has
	 * been displayed will give incorrect data.  The workaround is to put up
	 * another screen first, such as a splash screen.
	 * 
	 * @return The number of pixels high the entire screen is.
	 */
	public int getScreenHeight ()
	{
		return slave.getHeight();
	}

	/**
	 * Method called by the framework when the user clicks on the left menu button.
	 * The default implementation does nothing.
	 * 
	 * @see #getLeftMenuText()
	 */
	protected void declineNotify ()
	{
	}
	
	/**
	 * Method called by the framework when the user clicks the right menu button.
	 * The default implementation does nothing.
	 * 
	 * @see #getRightMenuText()
	 */
	protected void acceptNotify ()
	{
	}
	
	/**
	 * Called when a key is pressed.  It can be identified using the
	 * constants defined in this class.
	 * <p>
	 * Special keys, like the joystick and menu buttons, have negative
	 * values.  Input characters, like the number keys, are positive.
	 * <p>
	 * Unlike the MIDP <code>Canvas</code> class which requires tranlation of game keys
	 * this implementation does not.  The <code>keyCode</code> value will match the
	 * constants of this class.
	 * 
	 * @param keyCode is the key code of the key that was pressed.
	 *  Negative values are special keys like the joystick and menu buttons.
	 */
	protected void keyPressed (int keyCode)
	{
	}

	/**
	 * Called when a key is repeated (held down).  It can be identified using the
	 * constants defined in this class.
	 * <p>
	 * Special keys, like the joystick and menu buttons, have negative
	 * values.  Input characters, like the number keys, are positive.
	 * <p>
	 * Unlike the MIDP <code>Canvas</code> class which requires tranlation of game keys
	 * this implementation does not.  The <code>keyCode</code> value will match the
	 * constants of this class.
	 * <p>
	 * Also unlike the MIDP <code>Canvas</code> class, this implementation always
	 * supports this method and does so the same across all devices.
	 * 
	 * @param keyCode is the key code of the key that was held down.
	 *  Negative values are special keys like the joystick and menu buttons.
	 */
	protected void keyRepeated (int keyCode)
	{
	}
	
	/**
	 * Called when a key is released.  It can be identified using the
	 * constants defined in this class.
	 * <p>
	 * Special keys, like the joystick and menu buttons, have negative
	 * values.  Input characters, like the number keys, are positive.
	 * <p>
	 * Unlike the MIDP <code>Canvas</code> class which requires tranlation of game keys
	 * this implementation does not.  The <code>keyCode</code> value will match the
	 * constants of this class.
	 * 
	 * @param keyCode is the key code of the key that was released.
	 *  Negative values are special keys like the joystick and menu buttons.
	 */
	protected void keyReleased (int keyCode)
	{
	}
	
	/**
	 * Called when the pointer is pressed.
	 * 
	 * @param x is the horizontal location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 * @param y is the vertical location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 */
	protected void pointerPressed (int x, int y)
	{
	}
	
	/**
	 * Called when the pointer is released.
	 * 
	 * @param x is the horizontal location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 * @param y is the vertical location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 */
	protected void pointerReleased (int x, int y)
	{
	}
	
	/**
	 * Called when the pointer is dragged.
	 * 
	 * @param x is the horizontal location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 * @param y is the vertical location where the pointer was pressed
	 *  relative to the canvas area (i.e. does not include the title or
	 *  menu bars).
	 */
	protected void pointerDragged (int x, int y)
	{
	}
		
	/**
	 * Requests a repaint for the entire <code>Canvas</code>. The effect is identical to
	 * <code>repaint(0, 0, getWidth(), getHeight());</code>. 
	 */
	public void repaint ()
	{
		// Make sure the wrapper is in full-screen mode.
		//   There is a bug on some implementations that turns the screen
		//   off full-screen mode.  This can be seen when going to a
		//   javax.microedition.lcdui.TextBox screen and back to this one.
		slave.setFullScreenMode( true );
		
		// Do the repaint.
		slave.repaint();
	}
	
	/**
	 * Requests a repaint for the specified region of the <code>Canvas<code>.  Calling this
	 * method may result in subsequent call to <code>paint()</code>, where the passed
	 * <code>Graphics</code> object's clip region will include at least the specified region.
	 * <p>
	 * If the canvas is not visible, or if width and height are zero or less, or if
	 * the rectangle does not specify a visible region of the display, this call has
	 * no effect.
	 * <p>
	 * The call to <code>paint()</code> occurs asynchronously of the call to <code>repaint()</code>.
	 * That is, <code>repaint()</code> will not block waiting for <code>paint()</code> to finish.  The
	 * <code>paint()</code> method will either be called after the caller of <code>repaint()</code> returns
	 * to the implementation (if the caller is a callback) or on another thread entirely.
	 * <p>
	 * To synchronize with its <code>paint()</code> routine applications can use
	 * <code>serviceRepaints()</code>, or they can code explicit synchronization into their
	 * <code>paint()</code> routine.
	 * <p>
	 * The origin of the coordinate system is above and to the left of the pixel in
	 * the upper left corner of the displayable area of the <code>Canvas</code>. The 
	 * X-coordinate is positive right and the Y-coordinate is positive downwards.
	 * 
	 * @param x is the x coordinate of the rectangle to be repainted.
	 * @param y is the y coordinate of the rectangle to be repainted.
	 * @param width is the width of the rectangle to be repainted.
	 * @param height is the height of the rectangle to be repainted.
	 * @see Canvas#serviceRepaints()
	 */
	public void repaint (int x, int y, int width, int height)
	{
		if ( hasTitleBar() )
		{
			// Offset the user's y by the height of the title bar.
			Theme theme = UIManager.getTheme();
			int titleHeight = theme.getTitleHeight();
			y += titleHeight;
		}
		
		slave.repaint( x, y, width, height );
	}
	
	/**
	 * Forces any pending repaint requests to be serviced immediately.  This
	 * method blocks until the pending requests have been serviced.  If there
	 * are no pending repaints, or if this canvas is not visible on the
	 * display, this call does nothing and returns immediately.
	 */
	public void serviceRepaints ()
	{
		slave.serviceRepaints();
	}
	
	/**
	 * Paints the background of the main section of the screen.  This includes
	 * everything except for the title bar at the top and menu bar at the bottom.
	 * However, if this canvas is in full screen mode, then this method paints the entire
	 * screen.
	 * <p>
	 * After this method is called, the <code>paintCanvas</code> method will be.
	 * <p>
	 * Override this method to change the background for just this screen.  Override
	 * <code>Theme.paintBackground</code> to change the background for the entire application.
	 * 
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @see #paint(Graphics)
	 */
	protected void paintBackground (Graphics g)
	{
		UIManager.getTheme().paintBackground( g );
	}
	
	/**
	 * Paints the main section of the screen.  This includes everything except
	 * for the title bar at the top and menu bar at the bottom.  However,
	 * if this canvas is in full screen mode, then this method paints the entire
	 * screen.
	 * <p>
	 * Before this method is called, the <code>paintBackground</code> method will be.
	 * Any painting done here will go over the background.
	 * <p>
	 * Override this method to paint the main area of the screen.
	 * 
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @see #paintBackground(Graphics)
	 */
	protected abstract void paint (Graphics g);

	/**
	 * Paints the title bar of the canvas.  This method is called only
	 * when the title has been set through <code>setTitle</code> and the canvas
	 * is not in full screen mode.
	 * <p>
	 * Override this method to change the appearance of the title bar
	 * for just this canvas.  To change them for the entire application,
	 * override <code>Theme.paintTitleBar</code>.
	 * 
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @param title is the text for the title bar as defined by the
	 *  canvas class.
	 * @param width is the width of the title bar in pixels.
	 * @param height is the height of the title bar in pixels.
	 */
	protected void paintTitleBar (Graphics g, String title, int width, int height)
	{
		UIManager.getTheme().paintTitleBar( g, title, width, height );
	}

	/**
	 * Paints the menu bar at the bottom of the canvas.  This method is
	 * not called if the canvas is in full screen mode.
	 * <p>
	 * Override this method to change the appearance or functionality of
	 * the menu for just this canvas.  To change them for the entire
	 * application, override <code>Theme.paintMenuBar</code>.  Be careful not
	 * to write strings that are too long and will not fit on the menu bar.
	 * 
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @param left is the text to write on the left side of the menu bar.
	 *  The left side is associated with dimissing input such as a
	 *  "Cancel" button.
	 * @param highlightLeft is <code>true</code> if the menu text <code>left</code>
	 *  should be highlighted to indicate the left menu button is currently
	 *  pressed.
	 * @param right is the text to write on the right side of the menu bar.
	 *  The right side is associated with accepting input such as an
	 *  "OK" button.
	 * @param highlightRight is <code>true</code> if the menu text <code>right</code>
	 *  should be highlighted to indicate the right menu button is currently
	 *  pressed.
	 * @param width is the width of the menu bar in pixels.
	 * @param height is the height of the menu bar in pixels.
	 */
	protected void paintMenuBar (Graphics g,
			String left, boolean highlightLeft,
			String right, boolean highlightRight, 
			int width, int height)
	{
		UIManager.getTheme().paintMenuBar( g, left, highlightLeft, right, highlightRight, width, height );
	}
	
	/**
	 * Returns if the clip area of <code>g</code> intersects the given rectangle.
	 * 
	 * @param g is the current <code>Graphics</code> object for a <code>paint</code>
	 *  operation.
	 * @param x is the pixel at the left edge of the rectangle.
	 * @param y is the pixel at the top edge of the rectangle.
	 * @param w is width of the rectangle in pixels.
	 * @param h is height of the rectangle in pixels.
	 * @return <code>true</code> if the rectangle is to be painted by <code>g</code>;
	 *  <code>false</code> otherwise.
	 */
	public static boolean intersects (Graphics g, int x, int y, int w, int h)
	{
		// Get the graphic's clip dimensions.
		int gx = g.getClipX();
		int gy = g.getClipY();
		int gw = g.getClipWidth();
		int gh = g.getClipHeight();
		
		// Make the width/height into the right/bottom.
		gw += gx;
		gh += gy;
		w += x;
		h += y;
		
		// Check for intersections.
		//  (overflow || intersect)
		boolean intersects =
			(w < x || w > gx) &&
			(h < y || h > gy) &&
			(gw < gx || gw > x) &&
			(gh < gy || gh > y);
		return intersects;
	}
}

/**
 * Wraps the LCDUI's <code>Canvas</code> class.  It masks differences between
 * platforms.  It is used as the actual <code>Screen</code> for all J4ME screens.
 */
final class CanvasWrapper
	extends javax.microedition.lcdui.Canvas
	implements CommandListener
{
	/**
	 * The interval, in milliseconds, between signaling repeat events.
	 * It can be thought of as events raised per second by taking 1000
	 * and dividing it by this number (e.g. 1000 / 250 = 4 events per second).
	 */
	private static final short REPEAT_PERIOD = 200;

	/**
	 * When <code>true</code> this is running on a BlackBerry device.  When
	 * <code>false</code> it is not.
	 * <p>
	 * BlackBerry phones have limited native MIDlet support.  The biggest
	 * issue is they do not have the left and right menu buttons on other
	 * MIDP 2.0 phones.  Instead they have a "Menu" key and a "Return"
	 * key that are big parts of the overall BlackBerry experience.  To
	 * capture these key events, and make the application consistent with

⌨️ 快捷键说明

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