📄 screencanvas.java
字号:
component.requestFocus();
focusedIndex = index;
}
} else {
if (focusedIndex >= index) focusedIndex++;
}
}
}
/**
* Removes the component, specified by <code>index</code> from the screen
*
* @param index the index of the component to be removed.
*/
public void remove(int index) {
if (focusedIndex >= index) focusedIndex--;
components.removeElementAt(index);
}
/**
* Removes the specified component from this container.
*
* @param component the component to be removed
*/
public void remove(Component component) {
int index;
while ((index = components.lastIndexOf(component)) != -1) {
remove(index);
}
}
/**
* Removes all the components from this container.
*/
public void removeAll() {
focusedIndex = -1;
components.removeAllElements();
}
/**
* Paints the screen's body, which is all of the components that have been
* added to the screen.
*
* @param g The graphics object to draw to.
*/
public void paintBody(Graphics g) {
Component currentComponent = null;
int currentOffset = 0;
int height = 0;
int paintPos = 0;
recalcAdjustment();
final int cSize = components.size();
final int BODY_AREA_BOTTOM = HEIGHT - softKeyBarHeight;
for (int index = 0; index < cSize; index++) {
currentComponent = (Component) components.elementAt(index);
height = currentComponent.getHeight();
paintPos = currentComponent.screenY = currentOffset - bodyOffset + titleHeight;
currentComponent.isVisible = paintPos + height > titleHeight && paintPos < BODY_AREA_BOTTOM;
currentComponent.isCompletelyVisible = currentComponent.isVisible && paintPos + height <= BODY_AREA_BOTTOM && paintPos >= titleHeight;
if (currentComponent.isVisible) {
currentComponent.paint(g);
}
currentOffset += height;
}
}
protected void recalcAdjustment() {
if (adjustFocus) {
Component currentComponent = null;
final int BODY_AREA_BOTTOM = HEIGHT - softKeyBarHeight;
if (focusedIndex >= 0 && focusedIndex < components.size()) {
int fY = -bodyOffset + titleHeight;
for (int index = 0; index < focusedIndex; index++) {
currentComponent = (Component) components.elementAt(index);
fY += currentComponent.getHeight();
}
final int fH = ((Component) components.elementAt(focusedIndex)).getHeight();
final int fYH = fY + fH;
boolean isFocusedCompletelyVisible = (fYH > titleHeight && fY < BODY_AREA_BOTTOM) &&
fYH <= BODY_AREA_BOTTOM && fY >= titleHeight;
if (!isFocusedCompletelyVisible) {
if (fY < titleHeight) {
bodyOffset -= titleHeight - fY;
} else {
bodyOffset += fYH - BODY_AREA_BOTTOM;
}
}
}
adjustFocus = false;
}
}
/**
* Get height of all components
*
* @return height in pixes of all components in vector {@link #components} for current screen
*/
protected int getComponentsHeight() {
int height = 0;
Component c = null;
final int cSize = components.size();
for (int i = 0; i < cSize; i++) {
c = (Component) components.elementAt(i);
height += (c.getHeight()/*+ Style.V_GAP*/);
}
return height;
}
/**
* Scrolls to the top of the screen and
* sets first focusable component to focused state
*/
protected void scrollTop() {
int first = getFirstFocusable();
if (first != -1) changeFocus(first);
bodyOffset = 0;
}
/**
* Scrolls <code>CANVAS_SCROLL_STEP</code> pixels up
*
* @return true if scrolling is possible, false otherwise
*/
protected boolean scrollUp() {
if ((getScrollDirections() & SCROLL_UP) > 0) {
bodyOffset -= (bodyOffset > CANVAS_SCROLL_STEP) ?
CANVAS_SCROLL_STEP : bodyOffset;
repaint();
return true;
}
return (false);
}
/**
* Scrolls <code>CANVAS_SCROLL_STEP</code> pixels down
*
* @return true if scrolling is possible, false otherwise
*/
protected boolean scrollDown() {
if ((getScrollDirections() & SCROLL_DOWN) > 0) {
int x = getComponentsHeight() - paintAreaHeight - bodyOffset;
bodyOffset += CANVAS_SCROLL_STEP -
((x >= CANVAS_SCROLL_STEP) ? 0 : CANVAS_SCROLL_STEP - x);
repaint();
return true;
}
return false;
}
/**
* Gets previous focusable component
*
* @return index of previous focusable component,
* if there is no previous focusable components returns -1
*/
protected int getPreviousFocusable() {
int nRes = -1;
if (focusedIndex == 0) return nRes;
Component c = null;
for (int i = focusedIndex - 1; i >= 0; i--) {
c = (Component) components.elementAt(i);
if (c.focusable) {
nRes = i;
break;
}
}
c = null;
return nRes;
}
/**
* Gets next focusable component
*
* @return index of next focusable component,
* if there is no focusable components returns -1
*/
protected int getNextFocusable() {
int nRes = -1;
Component c = null;
if (focusedIndex == components.size()) return nRes;
for (int i = focusedIndex + 1; i < components.size(); i++) {
c = (Component) components.elementAt(i);
if (c.focusable) {
nRes = i;
break;
}
}
c = null;
return nRes;
}
/**
* Gets first focusable component
*
* @return index of first focusable component,
* if there is no focusable components returns -1
*/
protected int getFirstFocusable() {
int nRes = -1;
Component c = null;
int size = components.size();
for (int i = 0; i < size; i++) {
c = (Component) components.elementAt(i);
if (c.focusable) {
nRes = i;
break;
}
}
c = null;
return nRes;
}
/**
* Gets the last focusable component
*
* @return index of last focusable component,
* if there is no focusable components returns -1
*/
protected int getLastFocusable() {
int nRes = -1;
Component c = null;
for (int i = components.size() - 1; i >= 0; i--) {
c = (Component) components.elementAt(i);
if (c.focusable) {
nRes = i;
break;
}
}
c = null;
return nRes;
}
/**
* Moves the focus to the specified component.
*
* @param newIndex The index of component gaining focus
*/
public void changeFocus(int newIndex) {
if (newIndex != focusedIndex) {
Component c;
if (focusedIndex >= 0 && focusedIndex < components.size()) {
c = (Component) components.elementAt(focusedIndex);
c.releaseFocus();
}
focusedIndex = newIndex;
c = (Component) components.elementAt(focusedIndex);
c.requestFocus();
refocus();
}
}
/**
* Adjust the screen to make focused component visible
*/
public void refocus() {
adjustFocus = true;
repaint();
}
/**
* Class destructor. Helps VM to perform garbage collection
*/
public void destructor() {
screenTitle = null;
if (components != null) {
components.removeAllElements();
components = null;
}
softKeyBar = null;
offScreen = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -