gtkcolorchooserpanel.java

来自「JAVA 所有包」· Java 代码 · 共 1,284 行 · 第 1/3 页

JAVA
1,284
字号
        }        triangle.setColor(hue, saturation, brightness);        label.setBackground(color);        // Force Integer to pad the string with 0's by adding 0x1000000 and        // then removing the first character.        String hexString = Integer.toHexString(                  (color.getRGB() & 0xFFFFFF) | 0x1000000);        colorNameTF.setText("#" + hexString.substring(1));        if (updateSpinners) {            redSpinner.setValue(new Integer(color.getRed()));            greenSpinner.setValue(new Integer(color.getGreen()));            blueSpinner.setValue(new Integer(color.getBlue()));            hueSpinner.setValue(new Integer((int)(hue * 360)));            saturationSpinner.setValue(new Integer((int)(saturation * 255)));            valueSpinner.setValue(new Integer((int)(brightness * 255)));        }        settingColor = false;    }    public Color getColor() {        return label.getBackground();    }    /**     * ChangeListener method, updates the necessary display widgets.     */    public void stateChanged(ChangeEvent e) {        if (settingColor) {            return;        }        Color color = getColor();        if (e.getSource() == hueSpinner) {            setHue(((Number)hueSpinner.getValue()).floatValue() / 360, false);        }        else if (e.getSource() == saturationSpinner) {            setSaturation(((Number)saturationSpinner.getValue()).                          floatValue() / 255);        }        else if (e.getSource() == valueSpinner) {            setBrightness(((Number)valueSpinner.getValue()).                          floatValue() / 255);        }        else if (e.getSource() == redSpinner) {            setRed(((Number)redSpinner.getValue()).intValue());        }        else if (e.getSource() == greenSpinner) {            setGreen(((Number)greenSpinner.getValue()).intValue());        }        else if (e.getSource() == blueSpinner) {            setBlue(((Number)blueSpinner.getValue()).intValue());        }    }    /**     * Flag indicating the angle, or hue, has changed and the triangle     * needs to be recreated.     */    private static final int FLAGS_CHANGED_ANGLE = 1 << 0;    /**     * Indicates the wheel is being dragged.     */    private static final int FLAGS_DRAGGING = 1 << 1;    /**     * Indicates the triangle is being dragged.     */    private static final int FLAGS_DRAGGING_TRIANGLE = 1 << 2;    /**     * Indicates a color is being set and we should ignore setColor     */    private static final int FLAGS_SETTING_COLOR = 1 << 3;    /**     * Indicates the wheel has focus.     */    private static final int FLAGS_FOCUSED_WHEEL = 1 << 4;    /**     * Indicates the triangle has focus.     */    private static final int FLAGS_FOCUSED_TRIANGLE = 1 << 5;    /**     * Class responsible for rendering a color wheel and color triangle.     */    private class ColorTriangle extends JPanel {        /**         * Cached image of the wheel.         */        private Image wheelImage;        /**         * Cached image of the triangle.         */        private Image triangleImage;        /**         * Angle triangle is rotated by.         */        private double angle;        /**         * Boolean bitmask.         */        private int flags;        /**         * X location of selected color indicator.         */        private int circleX;        /**         * Y location of selected color indicator.         */        private int circleY;        public ColorTriangle() {            enableEvents(AWTEvent.FOCUS_EVENT_MASK);            enableEvents(AWTEvent.MOUSE_EVENT_MASK);            enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);            setMinimumSize(new Dimension(getWheelRadius() * 2 + 2,                                         getWheelRadius() * 2 + 2));            setPreferredSize(new Dimension(getWheelRadius() * 2 + 2,                                           getWheelRadius() * 2 + 2));            // We want to handle tab ourself.            setFocusTraversalKeysEnabled(false);            // PENDING: this should come from the style.            getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");            getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");            getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left");            getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right");            getInputMap().put(KeyStroke.getKeyStroke("KP_UP"), "up");            getInputMap().put(KeyStroke.getKeyStroke("KP_DOWN"), "down");            getInputMap().put(KeyStroke.getKeyStroke("KP_LEFT"), "left");            getInputMap().put(KeyStroke.getKeyStroke("KP_RIGHT"), "right");            getInputMap().put(KeyStroke.getKeyStroke("TAB"), "focusNext");            getInputMap().put(KeyStroke.getKeyStroke("shift TAB"),"focusLast");            ActionMap map = (ActionMap)UIManager.get(                                       "GTKColorChooserPanel.actionMap");            if (map == null) {                map = new ActionMapUIResource();                map.put("left", new ColorAction("left", 2));                map.put("right", new ColorAction("right", 3));                map.put("up", new ColorAction("up", 0));                map.put("down", new ColorAction("down", 1));                map.put("focusNext", new ColorAction("focusNext", 4));                map.put("focusLast", new ColorAction("focusLast", 5));		UIManager.getLookAndFeelDefaults().put(                             "GTKColorChooserPanel.actionMap", map);            }            SwingUtilities.replaceUIActionMap(this, map);        }        /**         * Returns the GTKColorChooserPanel.         */        GTKColorChooserPanel getGTKColorChooserPanel() {            return GTKColorChooserPanel.this;        }        /**         * Gives focus to the wheel.         */        void focusWheel() {            setFocusType(1);        }        /**         * Gives focus to the triangle.         */        void focusTriangle() {            setFocusType(2);        }        /**         * Returns true if the wheel currently has focus.         */        boolean isWheelFocused() {            return isSet(FLAGS_FOCUSED_WHEEL);        }        /**         * Resets the selected color.         */        public void setColor(float h, float s, float b) {            if (isSet(FLAGS_SETTING_COLOR)) {                return;            }            setAngleFromHue(h);            setSaturationAndBrightness(s, b);        }        /**         * Returns the selected color.         */        public Color getColor() {            return GTKColorChooserPanel.this.getColor();        }        /**         * Returns the x location of the selected color indicator.         */        int getColorX() {            return circleX + getIndicatorSize() / 2 - getWheelXOrigin();        }        /**         * Returns the y location of the selected color indicator.         */        int getColorY() {            return circleY + getIndicatorSize() / 2 - getWheelYOrigin();        }        protected void processEvent(AWTEvent e) {            if (e.getID() == MouseEvent.MOUSE_PRESSED ||                   ((isSet(FLAGS_DRAGGING) ||isSet(FLAGS_DRAGGING_TRIANGLE)) &&                   e.getID() == MouseEvent.MOUSE_DRAGGED)) {                // Assign focus to either the wheel or triangle and attempt                // to drag either the wheel or triangle.                int size = getWheelRadius();                int x = ((MouseEvent)e).getX() - size;                int y = ((MouseEvent)e).getY() - size;                if (!hasFocus()) {                    requestFocus();                }                if (!isSet(FLAGS_DRAGGING_TRIANGLE) &&                      adjustHue(x, y, e.getID() == MouseEvent.MOUSE_PRESSED)) {                    setFlag(FLAGS_DRAGGING, true);                    setFocusType(1);                }                else if (adjustSB(x, y, e.getID() ==                                        MouseEvent.MOUSE_PRESSED)) {                    setFlag(FLAGS_DRAGGING_TRIANGLE, true);                    setFocusType(2);                }                else {                    setFocusType(2);                }            }            else if (e.getID() == MouseEvent.MOUSE_RELEASED) {                // Stopped dragging                setFlag(FLAGS_DRAGGING_TRIANGLE, false);                setFlag(FLAGS_DRAGGING, false);            }            else if (e.getID() == FocusEvent.FOCUS_LOST) {                // Reset the flags to indicate no one has focus                setFocusType(0);            }            else if (e.getID() == FocusEvent.FOCUS_GAINED) {                // Gained focus, reassign focus to the wheel if no one                // currently has focus.                if (!isSet(FLAGS_FOCUSED_TRIANGLE) &&                          !isSet(FLAGS_FOCUSED_WHEEL)) {                    setFlag(FLAGS_FOCUSED_WHEEL, true);                    setFocusType(1);                }                repaint();            }            super.processEvent(e);        }        public void paintComponent(Graphics g) {            super.paintComponent(g);            // Draw the wheel and triangle            int size = getWheelRadius();            int width = getWheelWidth();            Image image = getImage(size);            g.drawImage(image, getWheelXOrigin() - size,                        getWheelYOrigin() - size, null);            // Draw the focus indicator for the wheel            if (hasFocus() && isSet(FLAGS_FOCUSED_WHEEL)) {                g.setColor(Color.BLACK);                g.drawOval(getWheelXOrigin() - size, getWheelYOrigin() - size,                           2 * size, 2 * size);                g.drawOval(getWheelXOrigin() - size + width, getWheelYOrigin()-                           size + width, 2 * (size - width), 2 *                           (size - width));            }            // Draw a line on the wheel indicating the selected hue.            if (Math.toDegrees(Math.PI * 2 - angle) <= 20 ||                     Math.toDegrees(Math.PI * 2 - angle) >= 201) {                g.setColor(Color.WHITE);            }            else {                g.setColor(Color.BLACK);            }            int lineX0 = (int)(Math.cos(angle) * size);            int lineY0 = (int)(Math.sin(angle) * size);            int lineX1 = (int)(Math.cos(angle) * (size - width));            int lineY1 = (int)(Math.sin(angle) * (size - width));            g.drawLine(lineX0 + size, lineY0 + size, lineX1 + size,                       lineY1 + size);            // Draw the focus indicator on the triangle            if (hasFocus() && isSet(FLAGS_FOCUSED_TRIANGLE)) {                Graphics g2 = g.create();                int innerR = getTriangleCircumscribedRadius();                int a = (int)(3 * innerR / Math.sqrt(3));                g2.translate(getWheelXOrigin(), getWheelYOrigin());                ((Graphics2D)g2).rotate(angle + Math.PI / 2);                g2.setColor(Color.BLACK);                g2.drawLine(0, -innerR, a / 2, innerR / 2);                g2.drawLine(a / 2, innerR / 2, -a / 2, innerR / 2);                g2.drawLine(-a / 2, innerR / 2, 0, -innerR);                g2.dispose();            }            // Draw the selected color indicator.            g.setColor(Color.BLACK);            g.drawOval(circleX, circleY, getIndicatorSize() - 1,                       getIndicatorSize() - 1);            g.setColor(Color.WHITE);            g.drawOval(circleX + 1, circleY + 1, getIndicatorSize() - 3,                       getIndicatorSize() - 3);        }        /**         * Returns an image representing the triangle and wheel.         */        private Image getImage(int size) {            if (!isSet(FLAGS_CHANGED_ANGLE) && wheelImage != null &&                        wheelImage.getWidth(null) == size * 2) {                return wheelImage;            }            if (wheelImage == null || wheelImage.getWidth(null) != size) {                wheelImage = getWheelImage(size);            }            int innerR = getTriangleCircumscribedRadius();            int triangleSize = (int)(innerR * 3.0 / 2.0);            int a = (int)(2 * triangleSize / Math.sqrt(3));            if (triangleImage == null || triangleImage.getWidth(null) != a) {                triangleImage = new BufferedImage(a, a,                                                  BufferedImage.TYPE_INT_ARGB);            }            Graphics g = triangleImage.getGraphics();            g.setColor(new Color(0, 0, 0, 0));            g.fillRect(0, 0, a, a);            g.translate((int)(a / 2), 0);            paintTriangle(g, triangleSize, getColor());            g.translate((int)(-a / 2), 0);            g.dispose();            g = wheelImage.getGraphics();            g.setColor(new Color(0, 0, 0, 0));            g.fillOval(getWheelWidth(), getWheelWidth(),                       2 * (size - getWheelWidth()),                       2 * (size - getWheelWidth()));            double rotate = Math.toRadians(-30.0) + angle;            g.translate(size, size);            ((Graphics2D)g).rotate(rotate);            g.drawImage(triangleImage, -a / 2,                        getWheelWidth() - size, null);            ((Graphics2D)g).rotate(-rotate);            g.translate(a / 2, size - getWheelWidth());            setFlag(FLAGS_CHANGED_ANGLE, false);            return wheelImage;        }        private void paintTriangle(Graphics g, int size, Color color) {            float[] colors = Color.RGBtoHSB(color.getRed(),                                            color.getGreen(),                                            color.getBlue(), null);            float hue = colors[0];            double dSize = (double)size;            for (int y = 0; y < size; y++) {                int maxX = (int)(y * Math.tan(Math.toRadians(30.0)));                float factor = maxX * 2;                if (maxX > 0) {                    float value = (float)(y / dSize);                    for (int x = -maxX; x <= maxX; x++) {                        float saturation = (float)x / factor + .5f;                        g.setColor(Color.getHSBColor(hue, saturation, value));                        g.fillRect(x, y, 1, 1);                    }                }                else {                    g.setColor(color);                    g.fillRect(0, y, 1, 1);                }            }        }        /**         * Returns a color wheel image for the specified size.         *         * @param size Integer giving size of color wheel.         * @return Color wheel image         */        private Image getWheelImage(int size) {            int minSize = size - getWheelWidth();            int doubleSize = size * 2;            BufferedImage image = new BufferedImage(doubleSize, doubleSize,                                              BufferedImage.TYPE_INT_ARGB);            for (int y = -size; y < size; y++) {                int ySquared = y * y;                for (int x = -size; x < size; x++) {                    double rad = Math.sqrt(ySquared + x * x);                    if (rad < size && rad > minSize) {                        int rgb = colorWheelLocationToRGB(x, y, rad) |                              0xFF000000;                        image.setRGB(x + size, y + size, rgb);                    }                }            }            wheelImage = image;            return wheelImage;        }

⌨️ 快捷键说明

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