windowsscrollbarui.java

来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 462 行 · 第 1/2 页

JAVA
462
字号
            }            highlightGrid.paint(g, x, y, w, h);        }    }              /**      * {@inheritDoc}     * @since 1.6     */    @Override    protected void setThumbRollover(boolean active) {        boolean old = isThumbRollover();        super.setThumbRollover(active);        // we need to repaint the entire scrollbar because state change for thumb        // causes state change for incr and decr buttons on Vista        if(XPStyle.isVista() && active != old) {            scrollbar.repaint();        }    }    /**     * WindowsArrowButton is used for the buttons to position the     * document up/down. It differs from BasicArrowButton in that the     * preferred size is always a square.     */    private class WindowsArrowButton extends BasicArrowButton {        public WindowsArrowButton(int direction, Color background, Color shadow,			 Color darkShadow, Color highlight) {	    super(direction, background, shadow, darkShadow, highlight);	}        public WindowsArrowButton(int direction) {            super(direction);        }	public void paint(Graphics g) {	    XPStyle xp = XPStyle.getXP();	    if (xp != null) {		ButtonModel model = getModel();                Skin skin = xp.getSkin(this, Part.SBP_ARROWBTN);		State state = null;                boolean jointRollover = XPStyle.isVista() && (isThumbRollover() ||                    (this == incrButton && decrButton.getModel().isRollover()) ||                    (this == decrButton && incrButton.getModel().isRollover()));		// normal, rollover, pressed, disabled		if (model.isArmed() && model.isPressed()) {		    switch (direction) {			case NORTH: state = State.UPPRESSED;    break;			case SOUTH: state = State.DOWNPRESSED;  break;			case WEST:  state = State.LEFTPRESSED;  break;			case EAST:  state = State.RIGHTPRESSED; break;		    }		} else if (!model.isEnabled()) {		    switch (direction) {			case NORTH: state = State.UPDISABLED;    break;			case SOUTH: state = State.DOWNDISABLED;  break;			case WEST:  state = State.LEFTDISABLED;  break;			case EAST:  state = State.RIGHTDISABLED; break;		    }		} else if (model.isRollover() || model.isPressed()) {		    switch (direction) {			case NORTH: state = State.UPHOT;    break;			case SOUTH: state = State.DOWNHOT;  break;			case WEST:  state = State.LEFTHOT;  break;			case EAST:  state = State.RIGHTHOT; break;		    }                } else if (jointRollover) {                    switch (direction) {                        case NORTH: state = State.UPHOVER;    break;                        case SOUTH: state = State.DOWNHOVER;  break;                        case WEST:  state = State.LEFTHOVER;  break;                        case EAST:  state = State.RIGHTHOVER; break;                    }		} else {		    switch (direction) {			case NORTH: state = State.UPNORMAL;    break;			case SOUTH: state = State.DOWNNORMAL;  break;			case WEST:  state = State.LEFTNORMAL;  break;			case EAST:  state = State.RIGHTNORMAL; break;		    }		}		skin.paintSkin(g, 0, 0, getWidth(), getHeight(), state);	    } else {		super.paint(g);	    }	}        public Dimension getPreferredSize() {            int size = 16;            if (scrollbar != null) {                switch (scrollbar.getOrientation()) {                case JScrollBar.VERTICAL:                    size = scrollbar.getWidth();                    break;                case JScrollBar.HORIZONTAL:                    size = scrollbar.getHeight();                    break;                }                size = Math.max(size, 5);            }            return new Dimension(size, size);        }    }    /**     * This should be pulled out into its own class if more classes need to     * use it.     * <p>     * Grid is used to draw the track for windows scrollbars. Grids     * are cached in a HashMap, with the key being the rgb components     * of the foreground/background colors. Further the Grid is held through     * a WeakRef so that it can be freed when no longer needed. As the     * Grid is rather expensive to draw, it is drawn in a BufferedImage.     */    private static class Grid {        private static final int BUFFER_SIZE = 64;        private static HashMap map;        private BufferedImage image;        static {            map = new HashMap();        }        public static Grid getGrid(Color fg, Color bg) {            String key = fg.getRGB() + " " + bg.getRGB();            WeakReference ref = (WeakReference)map.get(key);            Grid grid = (ref == null) ? null : (Grid)ref.get();            if (grid == null) {                grid = new Grid(fg, bg);                map.put(key, new WeakReference(grid));            }            return grid;        }        public Grid(Color fg, Color bg) {            int cmap[] = { fg.getRGB(), bg.getRGB() };            IndexColorModel icm = new IndexColorModel(8, 2, cmap, 0, false, -1,                                                      DataBuffer.TYPE_BYTE);            image = new BufferedImage(BUFFER_SIZE, BUFFER_SIZE,                                      BufferedImage.TYPE_BYTE_INDEXED, icm);            Graphics g = image.getGraphics();            try {                g.setClip(0, 0, BUFFER_SIZE, BUFFER_SIZE);                paintGrid(g, fg, bg);            }            finally {                g.dispose();            }        }        /**         * Paints the grid into the specified Graphics at the specified         * location.         */        public void paint(Graphics g, int x, int y, int w, int h) {            Rectangle clipRect = g.getClipBounds();            int minX = Math.max(x, clipRect.x);            int minY = Math.max(y, clipRect.y);            int maxX = Math.min(clipRect.x + clipRect.width, x + w);            int maxY = Math.min(clipRect.y + clipRect.height, y + h);            if (maxX <= minX || maxY <= minY) {                return;            }            int xOffset = (minX - x) % 2;            for (int xCounter = minX; xCounter < maxX;                 xCounter += BUFFER_SIZE) {                int yOffset = (minY - y) % 2;                int width = Math.min(BUFFER_SIZE - xOffset,                                     maxX - xCounter);                for (int yCounter = minY; yCounter < maxY;                     yCounter += BUFFER_SIZE) {                    int height = Math.min(BUFFER_SIZE - yOffset,                                          maxY - yCounter);                    g.drawImage(image, xCounter, yCounter,                                xCounter + width, yCounter + height,                                xOffset, yOffset,                                xOffset + width, yOffset + height, null);                    if (yOffset != 0) {                        yCounter -= yOffset;                        yOffset = 0;                    }                }                if (xOffset != 0) {                    xCounter -= xOffset;                    xOffset = 0;                }            }        }        /**         * Actually renders the grid into the Graphics <code>g</code>.         */        private void paintGrid(Graphics g, Color fg, Color bg) {            Rectangle clipRect = g.getClipBounds();            g.setColor(bg);            g.fillRect(clipRect.x, clipRect.y, clipRect.width,                       clipRect.height);            g.setColor(fg);            g.translate(clipRect.x, clipRect.y);            int width = clipRect.width;            int height = clipRect.height;            int xCounter = clipRect.x % 2;            for (int end = width - height; xCounter < end; xCounter += 2) {                g.drawLine(xCounter, 0, xCounter + height, height);            }            for (int end = width; xCounter < end; xCounter += 2) {                g.drawLine(xCounter, 0, width, width - xCounter);            }            int yCounter = ((clipRect.x % 2) == 0) ? 2 : 1;            for (int end = height - width; yCounter < end; yCounter += 2) {                g.drawLine(0, yCounter, width, yCounter + width);            }            for (int end = height; yCounter < end; yCounter += 2) {                g.drawLine(0, yCounter, height - yCounter, height);            }            g.translate(-clipRect.x, -clipRect.y);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?