📄 view.java
字号:
/** Get the background color for this instance. */ public int getBackground() { return background; } /** * Set the background image for this instance. * * @param bgImg The image to use as a background. */ public void setBackgoundImage(Image bgImg) { this.bgImg = bgImg; } /** Get the background image for this instance. */ public Image getBackgroundImage() { return bgImg; } // Adapted from atoms.alife.co.uk/sqrt, where it says: // "This code has been placed in the public domain." private static int sqrt(int x) { int v; int t = 1 << 30; int r = 0; int s; for (int i=0; i<16; i++) { s = t + r; r >>= 1; if (s <= x) { x -= s; r |= t; } t >>= 2; } return r; } private int getDistance(int a, int b, int oa, int ob, int distance) { int aa, bb, dd; if (oa < a) return -1; aa = Math.abs(a - oa); bb = Math.abs(b - ob); if (bb > aa) return -1; dd = sqrt(aa * aa + bb * bb); if (dd >= distance) return -1; return dd; } /** * Returns the first focusable component in the View (listed vertically, * from top of bottom). * * @return The first focusable component in the view */ protected Component getFirstFocusable() { Component comp; //System.out.println("Container.getFirstFocusable()"); for (int i=0; i<children.size(); i++) { comp = (Component)children.elementAt(i); if (comp.isFocusable()) return comp; } return null; } /** * Returns the last focusable component in the View (listed vertically, * from top of bottom). * * @return The last focusable component in the view */ protected Component getLastFocusable() { Component comp; //System.out.println("Container.getLastFocusable()"); for (int i=children.size()-1; i>=0; i--) { comp = (Component)children.elementAt(i); if (comp.isFocusable()) return comp; } return null; } /** * Given a component in the View, returns the next focusable component * (listed vertically, from top of bottom). If there isn't a "next" * one, then wraps the focus back around to the top and returns the * first focusable component in the view. * * @return The next focusable component in the view */ protected Component getNextFocusable( Component cur) { Component comp, next; //System.out.println("Container.getLastFocusable()"); next = getFirstFocusable(); for (int i=children.size()-1; i>=0; i--) { comp = (Component)children.elementAt(i); if (comp == cur) return next; if (comp.isFocusable()) next = comp; } return getLastFocusable(); } /** * Given a component in the View, returns the previous focusable * component (listed vertically, from top of bottom). If there * isn't a "previous" one, then wraps the focus around to the * bottom and returns the last focusable component in the view. * * @return The previous focusable component in the view */ private Component getPrevFocusable( Component cur) { Component comp, prev; prev = getLastFocusable(); for (int i=0; i<children.size(); i++) { comp = (Component)children.elementAt(i); if (comp == cur) return prev; if (comp.isFocusable()) prev = comp; } return getFirstFocusable(); } private Component findClosestNeighbor(Component comp, int dir) { Component neighbor, c; int distance, i, dd, xx, yy; if (comp == null) { if (dir == NORTH || dir == WEST) return getFirstFocusable(); else return getLastFocusable(); } distance = Integer.MAX_VALUE; neighbor = null; for (i=0; i<children.size(); i++) { c = (Component)children.elementAt(i); if (c == comp || !c.isFocusable()) continue; switch (dir) { case ANY: xx = Math.abs(comp.getX() - c.getX()); yy = Math.abs(comp.getY() - c.getY()); dd = sqrt(xx * xx + yy * yy); break; case NORTH: dd = getDistance(c.getY(), c.getX(), comp.getY(), comp.getX(), distance); break; case SOUTH: dd = getDistance(comp.getY(), comp.getX(), c.getY(), c.getX(), distance); break; case WEST: dd = getDistance(c.getX(), c.getY(), comp.getX(), comp.getY(), distance); break; case EAST: dd = getDistance(comp.getX(), comp.getY(), c.getX(), c.getY(), distance); break; default: throw new IllegalArgumentException("illegal direction: " + dir); } if (dd >= 0 && dd < distance) { distance = dd; neighbor = c; } } return neighbor == null ? comp : neighbor; } /** * Handle key presses. If a subclass overrides this method, * it should always make a call to the superclass implementation * to avoid unexpected behavior. * * @param key The key code for the key that was pressd. */ public void keyPressed(int key) { Component nfocus; boolean handled; int action = 0; // In case DELETE isn't supported as a valid keycode to // be passed to getGameAction on a particular handset. try { action = getGameAction( key); } catch (Exception e) {} handled = false; nfocus = focus; if (focus != null) handled = focus.keyPressed( action, key); if (!handled) { debugKey = action; if (action == Canvas.UP || action == Canvas.LEFT ) { debugKey = 22; nfocus = getPrevFocusable( focus); } if (action == Canvas.DOWN || action == Canvas.RIGHT) { debugKey = 33; nfocus = getNextFocusable( focus); } if (nfocus != focus) { focus.setFocus(false); focus = nfocus; focus.setFocus(true); handleFocusChanged(); repaint(); } if (key == LEFT_SOFT_BUTTON && leftSoft != null) { leftSoftButtonPressed( leftSoft); } if (key == RIGHT_SOFT_BUTTON && rightSoft != null) { rightSoftButtonPressed( rightSoft); } } return; } /** * Called when child widget focus changes, in case Views would * like to add special behavior. View's implementation does * nothing, subclasses must override. * */ public void handleFocusChanged() {} /** * Handle key releases. If a subclass overrides this method, * it should always make a call to the superclass implementation * to avoid unexpected behavior. * * @param key The key code for the key that was pressd. */ public void keyReleased(int key) { if (focus != null) focus.keyReleased(getGameAction(key), key); } /** * Callback method for left soft button presses. The base implementation * of this method does nothing. Subclasses need to override it to achieve * useful behavior. */ public void leftSoftButtonPressed(String label) { } /** * Callback method for right soft button presses. The base implementation * of this method does nothing. Subclasses need to override it to achieve * useful behavior. */ public void rightSoftButtonPressed(String label) { } /** Get the witdth of this instance. */ public int getWidth() { return canvas.getWidth(); } /** Get the height of this instance. */ public int getHeight() { return canvas.getHeight(); } /** Repaint this instance. */ public void repaint() { canvas.repaint(); } /** * Get the game action associated with a specific key code. * @see Canvas#getGameAction */ public int getGameAction(int key) { return canvas.getGameAction(key); } private String getDirectionAsString(int dir) { if (dir == ANY ) return "ANY"; if (dir == NORTH) return "NORTH"; if (dir == SOUTH) return "SOUTH"; if (dir == WEST ) return "WEST"; if (dir == EAST ) return "EAST"; throw new IllegalArgumentException("illegal direction: " + dir); } protected void paintLeftSoftButton(Graphics g) { if (leftSoft != null) { g.setColor(TITLE_COLOR);/*// g.setFont(SOFT_BUTTON_FONT); g.drawString( leftSoft, MARGIN, getHeight() - SOFT_BUTTON_FONT.getHeight(), Component.NW_ANCHOR );*/ SOFT_BUTTON_FONT.drawString( g, leftSoft, MARGIN, getHeight() - SOFT_BUTTON_FONT.getHeight() ); } } protected void paintRightSoftButton(Graphics g) { int sw; if (rightSoft != null) { g.setColor(TITLE_COLOR);/*// g.setFont(SOFT_BUTTON_FONT); g.drawString( rightSoft, getWidth() - sw - MARGIN, getHeight() - SOFT_BUTTON_FONT.getHeight(), Component.NW_ANCHOR );*/ sw = SOFT_BUTTON_FONT.stringWidth( rightSoft); SOFT_BUTTON_FONT.drawString( g, rightSoft, getWidth() - sw - MARGIN, getHeight() - SOFT_BUTTON_FONT.getHeight() ); } } protected void paintWaiting(Graphics g) { int yy; if (waiting) { yy = getHeight() - 10; g.setColor(TITLE_COLOR); g.drawLine(blipStart, yy, blipStop, yy); g.drawImage(blip, blipX, yy - blip.getHeight() / 2, Component.NW_ANCHOR); } } /** * Paint this instance. * * @param g The Graphics object to use for painting operations. */ public void paint(Graphics g) { Component comp; g.setClip( 0, 0, getWidth(), getHeight()); g.setColor(background); g.fillRect(0, 0, getWidth(), getHeight()); if (bgImg != null) { g.drawImage( bgImg, 0 + (getWidth() - bgImg.getWidth()) / 2, 0 + (getHeight() - bgImg.getHeight()) / 2, Component.NW_ANCHOR ); } if (showTitle && name != null) {// g.setFont(TITLE_FONT); g.setColor(TITLE_COLOR);/* g.drawString( name, 5, 0, Component.NW_ANCHOR );*/ TITLE_FONT.drawString( g, name, titleX, 0 ); } // Paint each component, except the in-focus component, // which is drawn later. for (int i=0; i<children.size(); i++) { comp = (Component)children.elementAt(i); if (comp == focus) continue; comp.paint( g); } g.setClip( 0, 0, getWidth(), getHeight()); paintLeftSoftButton(g); paintRightSoftButton(g); paintWaiting(g); // Paint the selected component last, potentially on top of // the other components (e.g. a selected List widget may be // expanded into its full drop-down menu state, which // will overlap other components.) if (focus != null) { focus.paint(g); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -