motifborders.java

来自「JAVA 所有包」· Java 代码 · 共 750 行 · 第 1/2 页

JAVA
750
字号
/* * @(#)MotifBorders.java	1.38 05/11/30 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.motif;import sun.swing.SwingUtilities2;import javax.swing.*;import javax.swing.border.*;import javax.swing.plaf.*;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.io.Serializable;/** * Factory object that can vend Icons appropriate for the basic L & F. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases.  The current serialization support is appropriate * for short term storage or RMI between applications running the same * version of Swing.  A future release of Swing will provide support for * long term persistence. * * @version 1.38 11/30/05 * @author Amy Fowler */public class MotifBorders {     public static class BevelBorder extends AbstractBorder implements UIResource {	private Color darkShadow = UIManager.getColor("controlShadow");	private Color lightShadow = UIManager.getColor("controlLtHighlight");	private boolean isRaised;	public BevelBorder(boolean isRaised, Color darkShadow, Color lightShadow) {	    this.isRaised = isRaised;            this.darkShadow = darkShadow;            this.lightShadow = lightShadow;	}	public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {	    g.setColor((isRaised) ? lightShadow : darkShadow);	    g.drawLine(x, y, x+w-1, y);           // top	    g.drawLine(x, y+h-1, x, y+1);         // left	    g.setColor((isRaised) ? darkShadow : lightShadow);	    g.drawLine(x+1, y+h-1, x+w-1, y+h-1); // bottom	    g.drawLine(x+w-1, y+h-1, x+w-1, y+1); // right	}	public Insets getBorderInsets(Component c) { 	    return getBorderInsets(c, new Insets(0,0,0,0));	}        public Insets getBorderInsets(Component c, Insets insets) {	    insets.top = insets.left = insets.bottom = insets.right = 1;	    return insets;	}	public boolean isOpaque(Component c) { 	    return true;	}    }    public static class FocusBorder extends AbstractBorder implements UIResource {        private Color focus;	private Color control;	public FocusBorder(Color control, Color focus) {            this.control = control;            this.focus = focus;	}	public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {	    if (((JComponent)c).hasFocus()) {	        g.setColor(focus); 		g.drawRect(x, y, w-1, h-1);	    } else {		g.setColor(control);		g.drawRect(x, y, w-1, h-1);	    }	}	public Insets getBorderInsets(Component c) { 	    return getBorderInsets(c, new Insets(0,0,0,0));	}        public Insets getBorderInsets(Component c, Insets insets) {	    insets.top = insets.left = insets.bottom = insets.right = 1;	    return insets;	}    }    public static class ButtonBorder extends AbstractBorder implements UIResource {        protected Color focus = UIManager.getColor("activeCaptionBorder");        protected Color shadow = UIManager.getColor("Button.shadow");        protected Color highlight = UIManager.getColor("Button.light");        protected Color darkShadow;        public ButtonBorder(Color shadow, Color highlight, Color darkShadow, Color focus) {            this.shadow = shadow;            this.highlight = highlight;            this.darkShadow = darkShadow;            this.focus = focus;        }              public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {            boolean isPressed = false;            boolean hasFocus = false;            boolean canBeDefault = false;            boolean isDefault = false;            if (c instanceof AbstractButton) {	        AbstractButton b = (AbstractButton)c;	        ButtonModel model = b.getModel();	        isPressed = (model.isArmed() && model.isPressed());	        hasFocus = (model.isArmed() && isPressed) || 	                   (b.isFocusPainted() && b.hasFocus());                if (b instanceof JButton) {                    canBeDefault = ((JButton)b).isDefaultCapable();                    isDefault = ((JButton)b).isDefaultButton();                }            }            int bx1 = x+1;            int by1 = y+1;            int bx2 = x+w-2;            int by2 = y+h-2;            if (canBeDefault) {                if (isDefault) {                    g.setColor(shadow);                     g.drawLine(x+3, y+3, x+3, y+h-4);                    g.drawLine(x+3, y+3, x+w-4, y+3);                          g.setColor(highlight);                        g.drawLine(x+4, y+h-4, x+w-4, y+h-4);                    g.drawLine(x+w-4, y+3, x+w-4, y+h-4);                }                bx1 +=6;                by1 += 6;                bx2 -= 6;                by2 -= 6;            }            if (hasFocus) {                g.setColor(focus);                if (isDefault) {                    g.drawRect(x, y, w-1, h-1);                } else {                    g.drawRect(bx1-1, by1-1, bx2-bx1+2, by2-by1+2);                }            }            g.setColor(isPressed? shadow : highlight);            g.drawLine(bx1, by1, bx2, by1);            g.drawLine(bx1, by1, bx1, by2);            g.setColor(isPressed? highlight : shadow);            g.drawLine(bx2, by1+1, bx2, by2);            g.drawLine(bx1+1, by2, bx2, by2);                  }	        public Insets getBorderInsets(Component c) {	    return getBorderInsets(c, new Insets(0,0,0,0));	}	public Insets getBorderInsets(Component c, Insets insets) {	    int thickness = (c instanceof JButton && ((JButton)c).isDefaultCapable())? 8 : 2;	    insets.top = insets.left = insets.bottom = insets.right = thickness;	    return insets;        }    }    public static class ToggleButtonBorder extends ButtonBorder {        public ToggleButtonBorder(Color shadow, Color highlight, Color darkShadow, Color focus) {             super(shadow, highlight, darkShadow, focus);        }        public void paintBorder(Component c, Graphics g, int x, int y, 			    int width, int height) {	    if (c instanceof AbstractButton) {	        AbstractButton b = (AbstractButton)c;	        ButtonModel model = b.getModel();	        if (model.isArmed() && model.isPressed() || model.isSelected()) {		    drawBezel(g, x, y, width, height,			      (model.isPressed() || model.isSelected()),			      b.isFocusPainted() && b.hasFocus(), shadow, highlight, darkShadow, focus);	        } else {		    drawBezel(g, x, y, width, height, 			      false, b.isFocusPainted() && b.hasFocus(),                              shadow, highlight, darkShadow, focus);                }	    } else {		        drawBezel(g, x, y, width, height, false, false,                           shadow, highlight, darkShadow, focus);            }        }        public Insets getBorderInsets(Component c) {	    return new Insets(2, 2, 3, 3);        }        public Insets getBorderInsets(Component c, Insets insets) {	    insets.top = insets.left = 2;	    insets.bottom = insets.right = 3;	    return insets;	}    }    public static class MenuBarBorder extends ButtonBorder {        public MenuBarBorder(Color shadow, Color highlight, Color darkShadow, Color focus) {            super(shadow, highlight, darkShadow, focus);        }        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {            JMenuBar menuBar = (JMenuBar)c;            if (menuBar.isBorderPainted() == true) {	        // this draws the MenuBar border	        Dimension size = menuBar.getSize();	        drawBezel(g,x,y,size.width,size.height,false,false,                          shadow, highlight, darkShadow, focus);	    }        }	public Insets getBorderInsets(Component c) { 	    return getBorderInsets(c, new Insets(0,0,0,0));	}        public Insets getBorderInsets(Component c, Insets insets) {	    insets.top = insets.left = insets.bottom = insets.right = 6;	    return insets;	}    }    public static class FrameBorder extends AbstractBorder implements UIResource {        JComponent jcomp;        Color frameHighlight;        Color frameColor;        Color frameShadow;        // The width of the border        public final static int BORDER_SIZE = 5;        /** Constructs an FrameBorder for the JComponent <b>comp</b>.        */        public FrameBorder(JComponent comp) {            jcomp = comp;        }        /** Sets the FrameBorder's JComponent.      */        public void setComponent(JComponent comp) {            jcomp = comp;        }        /** Returns the FrameBorder's JComponent.          * @see #setComponent          */        public JComponent component() {            return jcomp;        }        protected Color getFrameHighlight() {            return frameHighlight;        }        protected Color getFrameColor() {            return frameColor;        }            protected Color getFrameShadow() {            return frameShadow;        }        static Insets insets = new Insets(BORDER_SIZE, BORDER_SIZE,                                      BORDER_SIZE, BORDER_SIZE);        public Insets getBorderInsets(Component c) {            return insets;        }        public Insets getBorderInsets(Component c, Insets newInsets) {	    newInsets.top = insets.top;	    newInsets.left = insets.left;	    newInsets.bottom = insets.bottom;	    newInsets.right = insets.right;	    return newInsets;	}       /** Draws the FrameBorder's top border.         */        protected boolean drawTopBorder(Component c, Graphics g,                                     int x, int y, int width, int height) {            Rectangle titleBarRect = new Rectangle(x, y, width, BORDER_SIZE);            if (!g.getClipBounds().intersects(titleBarRect)) {                return false;            }            int maxX = width - 1;            int maxY = BORDER_SIZE - 1;            // Draw frame            g.setColor(frameColor);            g.drawLine(x, y + 2, maxX - 2, y + 2);            g.drawLine(x, y + 3, maxX - 2, y + 3);            g.drawLine(x, y + 4, maxX - 2, y + 4);            // Draw highlights            g.setColor(frameHighlight);            g.drawLine(x, y, maxX, y);            g.drawLine(x, y + 1, maxX, y + 1);            g.drawLine(x, y + 2, x, y + 4);            g.drawLine(x + 1, y + 2, x + 1, y + 4);            // Draw shadows            g.setColor(frameShadow);            g.drawLine(x + 4, y + 4, maxX - 4, y + 4);            g.drawLine(maxX, y + 1, maxX, maxY);            g.drawLine(maxX - 1, y + 2, maxX - 1, maxY);            return true;        }        /** Draws the FrameBorder's left border.          */        protected boolean drawLeftBorder(Component c, Graphics g, int x, int y,                                int width, int height) {            Rectangle borderRect =                 new Rectangle(0, 0, getBorderInsets(c).left, height);            if (!g.getClipBounds().intersects(borderRect)) {                return false;            }            int startY = BORDER_SIZE;            g.setColor(frameHighlight);            g.drawLine(x, startY, x, height - 1);            g.drawLine(x + 1, startY, x + 1, height - 2);            g.setColor(frameColor);            g.fillRect(x + 2, startY, x + 2, height - 3);            g.setColor(frameShadow);            g.drawLine(x + 4, startY, x + 4, height - 5);            return true;        }        /** Draws the FrameBorder's right border.          */        protected boolean drawRightBorder(Component c, Graphics g, int x, int y,                                 int width, int height) {            Rectangle borderRect = new Rectangle(                width - getBorderInsets(c).right, 0,

⌨️ 快捷键说明

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