📄 panel.java
字号:
if(container==null) throw new IllegalArgumentException("Cannot set a null container to a Panel. Use remove() instead."); if (this.container != null) this.container.parent = null; this.container = container; if (container.parent != null) { if (container.parent instanceof Container) ((Container) container.parent).remove(container); } container.parent = this; valid = false; } public void remove(Component c) { if (container != c) { throw new IllegalArgumentException("Container "+c+" is not inside this panel."); } else if(container!=null) { container.parent = null; container = null; valid = false; } } public int countComponents() { if(container!=null)// TODO Auto-generated method stub return container.countComponents(); else return 0; } /** * * Sets the viewport position relative to the top left corner of the * container inside this panel. * * * @param x * the distance (in pixels) of the left side of the viewport from * the left side of the container * @param y * the distance (in pixels) of the top side of the viewport from * the top side of the container */ public boolean setViewPortPosition(int x, int y) { if (container == null) return false; int tx = decorLeft - container.x; int ty = decorTop - container.y; if (x + viewPortWidth > container.width) x = container.width - viewPortWidth; else if (x < 0) x = 0; if (y + viewPortHeight > container.height) y = container.height - viewPortHeight; else if (y < 0) y = 0; if(x==tx && y==ty) // nothing changed return false { return false; } // ok now set the correct offset to the container. container.x = decorLeft - x; container.y = decorTop - y; viewPortPositionChanged = true; repaint(); return true; // change in the viewport position was successfull. } public int getViewPortPositionX() { if (container == null) return 0; return decorLeft - container.x; } public int getViewPortPositionY() { if (container == null) return 0; return decorTop - container.y; } public int getViewPortWidth() { return viewPortWidth; } public int getViewPortHeight() { return viewPortHeight; } protected void pointerDragged(int x, int y) { if (container == null) return; if (x > decorLeft && x < width - decorRight && y > decorTop && y < height - decorBottom) { container.pointerDragged(x - container.x, y - container.y); } } protected void pointerPressed(int x, int y) { if (container == null) return; if (x > decorLeft && x < width - decorRight && y > decorTop && y < height - decorBottom) { container.pointerPressed(x - container.x, y - container.y); } } protected void pointerReleased(int x, int y) { if(closeOnOutofBoundsPointerEvents && (x<0 || x>width || y<0 || y>height)) FireScreen.getScreen().removeComponent(this); if (container == null) return; int rightMargin = decorRight; if((scrollBarPolicy & VERTICAL_SCROLLBAR) == VERTICAL_SCROLLBAR && rightMargin<2*theme.scrollSize) { rightMargin = 2*theme.scrollSize; } if (x > decorLeft && x < width - rightMargin && y > decorTop && y < height - decorBottom) { container.pointerReleased(x - container.x, y - container.y); return; } // check if click is on scrollbar if (((scrollBarPolicy & VERTICAL_SCROLLBAR) == VERTICAL_SCROLLBAR) && x > width - rightMargin && y < height - decorBottom) { if (y > scrollY ) // scroll down { scrollDown(); } else // scroll up { scrollUp(); } return; } else if (((scrollBarPolicy & HORIZONTAL_SCROLLBAR) == HORIZONTAL_SCROLLBAR) && y > height - decorBottom && y < height - theme.scrollSize) { if (x > scrollX) { scrollRight(); } else { scrollLeft(); } return; } } public void scrollDown() { if ((scrollBarPolicy & VERTICAL_SCROLLBAR) != VERTICAL_SCROLLBAR) return; ScrollAnimation anim = new ScrollAnimation(); anim.setup(this,null,new Integer(FireScreen.DOWN)); if(this.animation!=null) { // do not scroll with animation if thereis another running. anim.forceComplete(); } else { FireScreen.getScreen().registerAnimation(anim); } } public void scrollUp() { if ((scrollBarPolicy & VERTICAL_SCROLLBAR) != VERTICAL_SCROLLBAR) return; ScrollAnimation anim = new ScrollAnimation(); anim.setup(this,null,new Integer(FireScreen.UP)); if(this.animation!=null) { // do not scroll with animation if thereis another running. anim.forceComplete(); } else { FireScreen.getScreen().registerAnimation(anim); } } public void scrollLeft() { if ((scrollBarPolicy & VERTICAL_SCROLLBAR) != VERTICAL_SCROLLBAR) return; ScrollAnimation anim = new ScrollAnimation(); anim.setup(this,null,new Integer(FireScreen.LEFT)); if(this.animation!=null) { // do not scroll with animation if thereis another running. anim.forceComplete(); } else { FireScreen.getScreen().registerAnimation(anim); } } public void scrollRight() { if ((scrollBarPolicy & VERTICAL_SCROLLBAR) != VERTICAL_SCROLLBAR) return; ScrollAnimation anim = new ScrollAnimation(); anim.setup(this,null,new Integer(FireScreen.RIGHT)); if(this.animation!=null) { // do not scroll with animation if thereis another running. anim.forceComplete(); } else { FireScreen.getScreen().registerAnimation(anim); } } protected void keyPressed(int keyCode) { if (container != null) container.keyPressed(keyCode); } protected void keyReleased(int keyCode) { if (keyCode == FireScreen.leftSoftKey || keyCode == FireScreen.rightSoftKey) { super.keyReleased(keyCode); return; } if (container != null) { FireScreen screen = FireScreen.getScreen(); int gameCode = screen.getGameAction(keyCode); if(gameCode==Canvas.LEFT) { scrollLeft(); return; } if(gameCode==Canvas.RIGHT) { scrollRight(); return; } if (gameCode == Canvas.UP || gameCode == Canvas.DOWN ) { // first find the next selectable component. if(focusableComponents==null) focusableComponents = container.generateListOfFocusableComponents(true); int step; int index=0; if (gameCode == Canvas.UP) { step = -1; // previous component index=focusableComponents.size()-1; } else { step = +1; // next component index=0; } int vpx = getViewPortPositionX(); int vpy = getViewPortPositionY(); int vpmx = vpx+viewPortWidth; int vpmy = vpy + viewPortHeight; Component lastSelected = screen.getSelectedComponent(); if(lastSelected!=null) { int lastPos = focusableComponents.indexOf(lastSelected); if(lastPos>-1) { index = lastPos+step; } if((index<0 || index>focusableComponents.size()-1) && (vpy==0 || vpmy>=container.height)) {// end of visible components vector. Disselect last selected screen.setSelectedComponent(null); } } // try to find the first component inside the view port for(int i=index;i>-1 && i<focusableComponents.size();i +=step) { Component next = (Component)focusableComponents.elementAt(i); // check to see if the component is visible int realx=0,realy=0; Component tmp = next; int fh = next.getFont().getHeight(); while(tmp!=null && tmp!=container) { realx+=tmp.x; realy+=tmp.y; tmp = tmp.parent; } if(realx>=vpx && realy>=vpy && realx<(vpmx) && realy<(vpmy-fh)) { // component is visible, send the event to it. next.keyReleased(keyCode); screen.setSelectedComponent(next); return; } } // no selectable component found. just scroll // just scroll if(gameCode==Canvas.UP) scrollUp(); if(gameCode==Canvas.DOWN) scrollDown(); return; } } super.keyReleased(keyCode); } protected void keyRepeated(int keyCode) { if (container != null) { int gameCode = FireScreen.getScreen().getGameAction(keyCode); if(gameCode==Canvas.LEFT) { scrollLeft(); return; } if(gameCode==Canvas.RIGHT) { scrollRight(); return; } if(gameCode==Canvas.UP) { scrollUp(); return; } if(gameCode==Canvas.DOWN) { scrollDown(); return; } container.keyRepeated(keyCode); } } public Vector generateListOfFocusableComponents(boolean recursive) { if(container!=null) return container.generateListOfFocusableComponents(recursive); else return new Vector(); } public int[] getMinSize() { if (parent == null) { FireScreen screen = FireScreen.getScreen(); return new int[]{screen.getWidth(), screen.getHeight()}; } return super.getMinSize(); } public int getScrollBarPolicy() { return scrollBarPolicy; } public boolean isCloseOnOutofBoundsPointerEvents() { return closeOnOutofBoundsPointerEvents; } public void setCloseOnOutofBoundsPointerEvents(boolean closeOnOutofBoundsPointerEvents) { this.closeOnOutofBoundsPointerEvents = closeOnOutofBoundsPointerEvents; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -