?? screencanvas.java
字號:
package com.jmobilecore.ui.core;
import javax.microedition.lcdui.*;
import java.util.Vector;
/**
* A screen that contains {@link Component}s.
*
* @author Greg Gridin
*/
public class ScreenCanvas extends PlatformCanvas {
/**
* Off the screen image for double buffer painting
*/
protected Image offScreen;
/**
* Scrolling direction. Used with {@link #drawTriangle drawTriangle}
*/
public final static int SCROLL_UP = 0x0001;
/**
* Scrolling direction. Used with {@link #drawTriangle drawTriangle}
*/
public final static int SCROLL_DOWN = 0x0002;
/**
* Scrolling step
*/
protected final static int CANVAS_SCROLL_STEP = 10;
/**
* Screen title
*/
protected Label screenTitle;
/**
* Vector of GUI components
*/
protected Vector components = null;
/**
* SoftKeyBar for current screen
* this component is not in components vector
*/
protected SoftKeyBar softKeyBar = null;
/**
* Currently focused component. Index points to components vector
*/
protected int focusedIndex = -1;
/**
* y-coordinate offset within the body of the first component to display. For
* example, if the screen container contains more components than can be
* displayed at one time vertically, this offset will be adjusted to ensure
* that the screen is scrolled to always display the currently-focused
* component.
*/
protected int bodyOffset = 0;
/**
* Indicated if focus ajjustment is required
*
* @see #recalcAdjustment
* @see #refocus
*/
protected boolean adjustFocus = false;
/**
* Creates a new <code>ScreenCanvas</code> instance.
*/
public ScreenCanvas() {
this(null, new SoftKeyBar());
}
/**
* Creates a new <code>ScreenCanvas</code> instance.
*
* @param title The screen title, one line text
*/
public ScreenCanvas(Label title) {
this(title, new SoftKeyBar());
}
/**
* Creates a new <code>ScreenCanvas</code> instance.
*
* @param title Title for current screen
* @param softKeyBar Container for soft keys for current screen
*/
public ScreenCanvas(Label title, SoftKeyBar softKeyBar) {
screenTitle = title;
components = new Vector();
this.softKeyBar = softKeyBar;
if (!DOUBLE_BUFFER) {
offScreen = Image.createImage(WIDTH, HEIGHT);
}
}
/**
* Height of the title in pixels
*/
protected int titleHeight;
/**
* Height of the scrollbar in pixels
*/
protected int softKeyBarHeight;
/**
* Height of the scrollable area (of current screen) in pixels
*/
protected int paintAreaHeight;
/**
* Paints the screen's body, which is all of the components that have been
* added to the screen.
*
* @param g Graphics context
*/
public void paint(Graphics g) {
Graphics saved = g;
int oldColor = g.getColor();
if (offScreen != null) {
g = offScreen.getGraphics();
}
titleHeight = (screenTitle != null) ? screenTitle.height : 0;
softKeyBarHeight = (softKeyBar != null) ? softKeyBar.height : 0;
paintAreaHeight = HEIGHT - titleHeight - softKeyBarHeight;
g.setClip(0, 0, WIDTH, HEIGHT);
paintBodyBackground(g);
paintBody(g);
if (screenTitle != null) screenTitle.paint(g);
if (softKeyBar != null) softKeyBar.paint(g);
paintScroller(g);
g.setColor(oldColor);
if (g != saved) {
saved.drawImage(offScreen, 0, 0, Graphics.LEFT | Graphics.TOP);
}
saved = null;
}
/**
* Paints the screen's body background
*
* @param g Graphics context
*/
protected void paintBodyBackground(Graphics g) {
g.setColor(Style.BACKGROUND_COLOR);
g.fillRect(0, titleHeight, WIDTH, paintAreaHeight);
}
/**
* Paint scrolling indicator(s) at the bottom of the screen to indicate that
* the display can scroll vertically.
*
* @param g Graphics context
*/
protected void paintScroller(Graphics g) {
int scrollDirections = getScrollDirections();
int arrowMaxHeight = softKeyBarHeight / 2 - 1;
if ((scrollDirections & SCROLL_UP) > 0) {
ScreenCanvas.drawTriangle(g, WIDTH / 2,
HEIGHT - softKeyBarHeight, arrowMaxHeight,
SCROLL_UP, Style.ARROW_COLOR);
}
if ((scrollDirections & SCROLL_DOWN) > 0) {
ScreenCanvas.drawTriangle(g, WIDTH / 2,
HEIGHT - 1, arrowMaxHeight,
SCROLL_DOWN, Style.ARROW_COLOR);
}
}
/**
* Support method for painting scroller
*
* @see #paintScroller
*/
public static void drawTriangle(Graphics g,
int tipX, int tipY,
int maxHeight,
int direction,
int color) {
int yIncrement = 1;
if (direction == ScreenCanvas.SCROLL_DOWN) yIncrement = -1;
if (color!=Color.TRANSPARENT) g.setColor(color);
for (int height = 0; height < maxHeight; height++) {
g.drawLine(tipX, tipY, tipX + height * 2, tipY);
tipX--; // start one pixel over
tipY += yIncrement; // next row to draw
}
}
/**
* Processes a press of a key.
*/
protected void keyPressed(int keyCode) {
if (softKeyBar != null && softKeyBar.keyPressed(keyCode))
return;
if (focusedIndex >= 0 && focusedIndex < components.size()) {
Component focusedComponent = (Component) components.elementAt(focusedIndex);
if (focusedComponent.keyPressed(keyCode)) return;
}
int n = -1;
if (keyCode == KEY_UP) {
if ((n = getPreviousFocusable()) != -1) {
changeFocus(n);
} else {
scrollUp();
}
} else if (keyCode == KEY_DOWN) {
if ((n = getNextFocusable()) != -1) {
changeFocus(n);
} else {
scrollDown();
}
}
}
/**
* Returns the directions the body can be scrolled in.
* This should be dynamic depending upon what portion of the body area is
* displayed.
*
* @return The directions that the body area can be scrolled. Should be 0 if
* all body elements can be displayed at once, but if not it should be an
* appropriate bitwise or of {@link #SCROLL_UP} and/or {@link #SCROLL_DOWN}
*/
protected int getScrollDirections() {
int directions = 0;
if (bodyOffset > 0) directions |= SCROLL_UP;
if (bodyOffset + paintAreaHeight < getComponentsHeight())
directions |= SCROLL_DOWN;
return directions;
}
/**
* Adds the component to the screen.
* <p/>
* More presicely it adds a component to vector of components for current screen
* Components are displayed in order of {@link #components} one component per line
*
* @param component the component to be added
*/
public void add(Component component) {
component.parentScreen = this;
components.addElement(component);
if (component.focusable && focusedIndex == -1) {
component.requestFocus();
focusedIndex = components.size() - 1;
}
}
/**
* Adds the component to the screen at the given position.
* <p/>
* More presicely it adds a component to vector of components for current screen
* Components are displayed in order of {@link #components} one component per line
*
* @param component the component to be added
* @param index the position at which to insert the component,
* or <code>-1</code> to append the component to the end
*/
public void add(Component component, int index) {
if (index == -1) {
add(component);
} else {
component.parentScreen = this;
components.insertElementAt(component, index);
if (focusedIndex == -1) {
if (component.focusable) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -