📄 view.java
字号:
/* ------------------------------------------------------------------------- * Copyright 2004-2005 Nokia Corporation All rights reserved. Nokia Mobile Phones Restricted Rights: Use, duplication, or disclosure by the U.S. Government is subject to restrictions as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR 52.227-14 Alt. III, as applicable. This software is proprietary to and embodies the confidential technology of Nokia Possession, use, or copying of this software and media is authorized only pursuant to a valid written license from Nokia or an authorized sublicensor. Nokia - Wireless Software Solutions * ------------------------------------------------------------------------- */package samples.ui;import java.util.Vector;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** * This class implements the concept of a view. See the * documentation for the <code>ViewCanvas</code> class for more details. * * @see ViewCanvas */public class View { /* Key code for left soft button. */ public static final int LEFT_SOFT_BUTTON = -6; /* Key code for right soft button. */ public static final int RIGHT_SOFT_BUTTON = -7; /* Key code for right enter/fireoft button. */ public static final int ENTER_BUTTON = -5; /* RGB value for default title color. */ public static int TITLE_COLOR = 0x00787d84; private static final int ANY = 0; private static final int NORTH = 1; private static final int SOUTH = 2; private static final int EAST = 3; private static final int WEST = 4; private static final int MARGIN = 5; protected int debugKey = 0; private static Image blip; private static int blipX; protected static boolean waiting; protected static String test_font = "Font1"; //protected static Font SOFT_BUTTON_FONT = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); //protected static Font TITLE_FONT = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL); protected static CustomFont SOFT_BUTTON_FONT = new CustomFont(test_font); protected static CustomFont TITLE_FONT = new CustomFont(test_font); protected Canvas canvas; protected Vector children; protected Component focus; protected String name; protected String leftSoft; protected String rightSoft; protected Image bgImg; protected int background; protected int highlight; protected int titleX; protected boolean showTitle; protected boolean active; private int blipStart; private int blipStop; protected int softkeyColor = TITLE_COLOR; /** Perform initialization for this class. */ public static void initialize() { try { blip = Image.createImage("/progress_blip.png"); } catch (Exception e) {} blipX = MARGIN; } /** * Create a new instance of this class. * * @param canvas The canvas that this view is associated with. * @param name The name of this view. */ public View( Canvas canvas, String name) { this.name = name; this.canvas = canvas; children = new Vector(); background = 0x00ffffff; highlight = 0x00ff0000; showTitle = true; titleX = 0; blipStart = 0; /*if (blip!=null) blipStop = canvas.getWidth()-blip.getWidth(); else blipStop = canvas.getWidth(); */ //May need this for IN-game reg */ blipStop = 20; } /** * Sets the Canvas for this instance * * @param canvas The Canvas for this instance */ public void setCanvas( Canvas canvas) { this.canvas = canvas; } /** * Gets the Canvas for this instance * * @return The Canvas for this instance */ public Canvas getCanvas() { return canvas; } /** * Gets the flag which determines whether the title of the View * is displayed. * * @return The flag determining whether to show the title of this View */ public boolean isShowTitle() { return showTitle; } /** * Sets the flag which determines whether the title of the View * is displayed. * * @param show The flag determining whether to show the title of this View */ public void setShowTitle( boolean show) { this.showTitle = show; } /** * Sets the Font used for drawing the soft button labels on this View * * @param font The Font used for drawing the soft button labels */ public void setSoftButtonFont(CustomFont font) { if (font != null) { SOFT_BUTTON_FONT = font; } } /** * Sets the Font used for drawing the title of this View * * @param font The Font used for drawing the title of this View */ public void setTitleFont(CustomFont font) { if (font != null) { TITLE_FONT = font; } } /** * Set the name of this instance. * * @param name The new name of the instance. */ public void setName(String name) { this.name = name; repaint(); } /** * Gets the name of this instance. * * @return name The name of this instance */ public String getName() { return name; } /** * Gets the name of this instance. * * @return name The name of this instance */ public boolean hasName( String name) { if (this.name == null || name == null ) return false; return name.equals(this.name); } /** * Sets the color used to draw soft key labels for this instance. * * @param color The color used to draw soft key labels */ public void setSoftkeyColor( int color) { this.softkeyColor = color; } /** * Gets the color used to draw soft key labels for this instance. * * @return The color used to draw soft key labels */ public int getSoftkeyColor() { return softkeyColor; } /** * This method is called by the managing canvas just before * a view becomes active, and just after it becomes inactive. * * @param active Whether not this instance is active */ public void setActive(boolean active) { //System.out.println("View.setActive(): " + this.active); this.active = active; if (!active) setWaiting(false); } /** * Return <code>true</code> if this instance is currently active, * <code>false</code> otherwise. */ public boolean isActive() { return active; } /** * Set the idle state of this instance. If the argument is * <code>true</code>, then an animated progess bar is displayed * at the bottom of this view. If the argument is <code>false * </code>, then the progress bar is turned off. * * @param w Whether or not this instance is idle. */ public void setWaiting(boolean w) { if (waiting == w) return; waiting = w; if (waiting) { Thread thread = new Thread() { public void run() { int xd = 3; blipX = blipStart; // (blipStop-blipStart-blip.getWidth())/2; while (waiting) { blipX += xd; if (blipX < blipStart || blipX + blip.getWidth() > blipStop) { xd = -xd; blipX += 2 * xd; } repaint(); try {Thread.sleep(100);} catch (InterruptedException e) {} } } }; thread.start(); } repaint(); //System.out.println("View.setWaiting(): " + waiting); } /** Return whether or not this instance is idle. */ public boolean isWaiting() { return waiting; } /** * Add a component to this instance. * * @param comp The component to add. */ public void add(Component comp) { //System.out.println("View.add(): " + comp); comp.setView(this); if (focus == null && comp.isFocusable()) { focus = comp; focus.setFocus(true); } if (!contains(comp)) { children.addElement(comp); } repaint(); } /** * Remove a component from this instance. * * @param comp The component to remove. */ public void remove(Component comp) { //System.out.println("View.remove(), this = " + this + ", comp = " + comp); comp.setView(null); if (focus == comp) { focus = findClosestNeighbor(focus, ANY); focus.setFocus(true); //System.out.println("new focus: " + focus); } //System.out.println("children before remove: " + children); children.removeElement(comp); //System.out.println("children after remove: " + children); repaint(); } /** * Return whether or not this instance contains * a specific component. * * @param comp The component to check for. */ public boolean contains(Component comp) { return children.contains(comp); } /** * Set focus to a specific component. * * @param comp The component to focus on. */ public void setFocus(Component comp) { //System.out.println("View.setFocus(): " + comp); if (comp.isFocusable() && children.contains(comp)) { focus = comp; focus.setFocus(true); } } /** Return the component that currently has focus. */ public Component getFocus() { return focus; } /** * Set the label for the left soft button. Setting the label * to <code>null</code> will disable the soft button. * * @param leftSoft The label for the button. */ public void setLeftSoftButton( String leftSoft) { this.leftSoft = leftSoft; blipStart = leftSoft == null ? 0 : SOFT_BUTTON_FONT.stringWidth(leftSoft) + 2 * MARGIN; blipX = blipStart; repaint(); } /** Return the label for the left soft button. */ public String getLeftSoftButton() { return leftSoft; } /** * Set the label for the right soft button. Setting the label * to <code>null</code> will disable the soft button. * * @param rightSoft The label for the button. */ public void setRightSoftButton( String rightSoft) { this.rightSoft = rightSoft; blipStop = rightSoft == null ? getWidth() : getWidth() - SOFT_BUTTON_FONT.stringWidth(rightSoft) - 2 * MARGIN; repaint(); } /** Return the label for the right soft button. */ public String getRightSoftButton() { return rightSoft; } /** * Set the highlight color for the focus component. * * @param highlight The RGB color value to use as a highlight. */ public void setHighlight(int highlight) { this.highlight = highlight; } /** Get the highlight color for the focus component. */ public int getHighlight() { return highlight; } /** * Set the background color for this instance. * * @param background The RGB color value to use as a background. */ public void setBackground(int background) { this.background = background; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -