📄 defaultswatchchooserpanel.java
字号:
/* DefaultSwatchChooserPanel.java -- Copyright (C) 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.colorchooser;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.Icon;import javax.swing.JColorChooser;import javax.swing.JLabel;import javax.swing.JPanel;/** * This class is the DefaultSwatchChooserPanel. This chooser panel displays a * set of colors that can be picked. Recently picked items will go into a * side panel so the user can see the history of the chosen colors. */class DefaultSwatchChooserPanel extends AbstractColorChooserPanel{ /** The main panel that holds the set of choosable colors. */ MainSwatchPanel mainPalette; /** A panel that holds the recent colors. */ RecentSwatchPanel recentPalette; /** The mouse handlers for the panels. */ MouseListener mouseHandler; /** * This the base class for all swatch panels. Swatch panels are panels that * hold a set of blocks where colors are displayed. */ abstract static class SwatchPanel extends JPanel { /** The width of each block. */ protected int cellWidth = 10; /** The height of each block. */ protected int cellHeight = 10; /** The gap between blocks. */ protected int gap = 1; /** The number of rows in the swatch panel. */ protected int numRows; /** The number of columns in the swatch panel. */ protected int numCols; /** * Creates a new SwatchPanel object. */ SwatchPanel() { super(); setBackground(Color.WHITE); } /** * This method returns the preferred size of the swatch panel based on the * number of rows and columns and the size of each cell. * * @return The preferred size of the swatch panel. */ public Dimension getPreferredSize() { int height = numRows * cellHeight + (numRows - 1) * gap; int width = numCols * cellWidth + (numCols - 1) * gap; Insets insets = getInsets(); return new Dimension(width + insets.left + insets.right, height + insets.top + insets.bottom); } /** * This method returns the color for the given position. * * @param x The x coordinate of the position. * @param y The y coordinate of the position. * * @return The color at the given position. */ public abstract Color getColorForPosition(int x, int y); /** * This method initializes the colors for the swatch panel. */ protected abstract void initializeColors(); } /** * This is the main swatch panel. This panel sits in the middle and allows a * set of colors to be picked which will move to the recent swatch panel. */ static class MainSwatchPanel extends SwatchPanel { /** The color describing (204, 255, 255) */ public static final Color C204255255 = new Color(204, 204, 255); /** The color describing (255, 204, 204) */ public static final Color C255204204 = new Color(255, 204, 204); /** The color describing (204, 255, 204) */ public static final Color C204255204 = new Color(204, 255, 204); /** The color describing (204, 204, 204) */ public static final Color C204204204 = new Color(204, 204, 204); /** The color (153, 153, 255). */ public static final Color C153153255 = new Color(153, 153, 255); /** The color (51, 51, 255). */ public static final Color C051051255 = new Color(51, 51, 255); /** The color (153, 0, 153). */ public static final Color C153000153 = new Color(153, 0, 153); /** The color (0, 51, 51). */ public static final Color C000051051 = new Color(0, 51, 51); /** The color (51, 0, 51). */ public static final Color C051000051 = new Color(51, 0, 51); /** The color (51, 51, 0). */ public static final Color C051051000 = new Color(51, 51, 0); /** The color (102, 102, 0). */ public static final Color C102102000 = new Color(102, 102, 0); /** The color (153, 255, 153). */ public static final Color C153255153 = new Color(153, 255, 153); /** The color (102, 255, 102). */ public static final Color C102255102 = new Color(102, 255, 102); /** The color (0, 102, 102). */ public static final Color C000102102 = new Color(0, 102, 102); /** The color (102, 0, 102). */ public static final Color C102000102 = new Color(102, 0, 102); /** The color (0, 153, 153). */ public static final Color C000153153 = new Color(0, 153, 153); /** The color (153, 153, 0). */ public static final Color C153153000 = new Color(153, 153, 0); /** The color (204, 204, 0). */ public static final Color C204204000 = new Color(204, 204, 0); /** The color (204, 0, 204). */ public static final Color C204000204 = new Color(204, 0, 204); /** The color (0, 204, 204). */ public static final Color C000204204 = new Color(0, 204, 204); /** The color (51, 255, 51). */ public static final Color C051255051 = new Color(51, 255, 51); /** The color (255, 51, 51). */ public static final Color C255051051 = new Color(255, 51, 51); /** The color (255, 102, 102). */ public static final Color C255102102 = new Color(255, 102, 102); /** The color (102, 102, 255). */ public static final Color C102102255 = new Color(102, 102, 255); /** The color (255, 153, 153). */ public static final Color C255153153 = new Color(255, 153, 153); static Color[] colors = { // Row 1 Color.WHITE, new Color(204, 255, 255), C204255255, C204255255, C204255255, C204255255, C204255255, C204255255, C204255255, C204255255, C204255255, new Color(255, 204, 255), C255204204, C255204204, C255204204, C255204204, C255204204, C255204204, C255204204, C255204204, C255204204, new Color(255, 255, 204), C204255204, C204255204, C204255204, C204255204, C204255204, C204255204, C204255204, C204255204, C204255204, // Row 2 C204204204, new Color(153, 255, 255), new Color(153, 204, 255), C153153255, C153153255, C153153255, C153153255, C153153255, C153153255, C153153255, new Color(204, 153, 255), new Color(255, 153, 255), new Color(255, 153, 204), C255153153, C255153153, C255153153, C255153153, C255153153, C255153153, C255153153, new Color(255, 204, 153), new Color(255, 255, 153), new Color(204, 255, 153), C153255153, C153255153, C153255153, C153255153, C153255153, C153255153, C153255153, new Color(153, 255, 204), // Row 3 C204204204, new Color(102, 255, 255), new Color(102, 204, 255), new Color(102, 153, 255), C102102255, C102102255, C102102255, C102102255, C102102255, new Color(153, 102, 255), new Color(204, 102, 255), new Color(255, 102, 255), new Color(255, 102, 204), new Color(255, 102, 153), C255102102, C255102102, C255102102, C255102102, C255102102, new Color(255, 153, 102), new Color(255, 204, 102), new Color(255, 255, 102), new Color(204, 255, 102), new Color(153, 255, 102), C102255102, C102255102, C102255102, C102255102, C102255102, new Color(102, 255, 153), new Color(102, 255, 204), // Row 4 new Color(153, 153, 153), new Color(51, 255, 255), new Color(51, 204, 255), new Color(51, 153, 255), new Color(51, 102, 255), C051051255, C051051255, C051051255, new Color(102, 51, 255), new Color(153, 51, 255), new Color(204, 51, 255), new Color(255, 51, 255), new Color(255, 51, 204), new Color(255, 51, 153), new Color(255, 51, 102), C255051051, C255051051, C255051051, new Color(255, 102, 51), new Color(255, 153, 51), new Color(255, 204, 51), new Color(255, 255, 51), new Color(204, 255, 51), new Color(153, 255, 51), new Color(102, 255, 51), C051255051, C051255051, C051255051, new Color(51, 255, 102), new Color(51, 255, 153), new Color(51, 255, 204), // Row 5 new Color(153, 153, 153), new Color(0, 255, 255), new Color(0, 204, 255), new Color(0, 153, 255), new Color(0, 102, 255), new Color(0, 51, 255), new Color(0, 0, 255), new Color(51, 0, 255), new Color(102, 0, 255), new Color(153, 0, 255), new Color(204, 0, 255), new Color(255, 0, 255), new Color(255, 0, 204), new Color(255, 0, 153), new Color(255, 0, 102), new Color(255, 0, 51), new Color(255, 0, 0), new Color(255, 51, 0), new Color(255, 102, 0), new Color(255, 153, 0), new Color(255, 204, 0), new Color(255, 255, 0), new Color(204, 255, 0), new Color(153, 255, 0), new Color(102, 255, 0), new Color(51, 255, 0), new Color(0, 255, 0), new Color(0, 255, 51), new Color(0, 255, 102), new Color(0, 255, 153), new Color(0, 255, 204), // Row 6 new Color(102, 102, 102), C000204204, C000204204, new Color(0, 153, 204), new Color(0, 102, 204), new Color(0, 51, 204), new Color(0, 0, 204), new Color(51, 0, 204), new Color(102, 0, 204), new Color(153, 0, 204), C204000204, C204000204, C204000204, new Color(204, 0, 153), new Color(204, 0, 102), new Color(204, 0, 51), new Color(204, 0, 0), new Color(204, 51, 0), new Color(204, 102, 0), new Color(204, 153, 0), C204204000, C204204000, C204204000, new Color(153, 204, 0), new Color(102, 204, 0), new Color(51, 204, 0), new Color(0, 204, 0), new Color(0, 204, 51), new Color(0, 204, 102), new Color(0, 204, 153), new Color(0, 204, 204), // Row 7 new Color(102, 102, 102), C000153153, C000153153, C000153153, new Color(0, 102, 153), new Color(0, 51, 153), new Color(0, 0, 153), new Color(51, 0, 153), new Color(102, 0, 153), C153000153, C153000153, C153000153, C153000153, C153000153, new Color(153, 0, 102), new Color(153, 0, 51), new Color(153, 0, 0), new Color(153, 51, 0), new Color(153, 102, 0), C153153000, C153153000, C153153000, C153153000, C153153000, new Color(102, 153, 0), new Color(51, 153, 0), new Color(0, 153, 0), new Color(0, 153, 51), new Color(0, 153, 102), C000153153, C000153153, // Row 8 new Color(51, 51, 51), C000102102, C000102102, C000102102, C000102102, new Color(0, 51, 102), new Color(0, 0, 102), new Color(51, 0, 102), C102000102, C102000102, C102000102, C102000102, C102000102, C102000102, C102000102, new Color(102, 0, 51), new Color(102, 0, 0), new Color(102, 51, 0), C102102000, C102102000, C102102000, C102102000, C102102000, C102102000, C102102000, new Color(51, 102, 0), new Color(0, 102, 0), new Color(0, 102, 51), C000102102, C000102102, C000102102, // Row 9. Color.BLACK, C000051051, C000051051, C000051051, C000051051, C000051051, new Color(0, 0, 51), C051000051, C051000051, C051000051, C051000051, C051000051, C051000051, C051000051, C051000051, C051000051, new Color(51, 0, 0), C051051000, C051051000, C051051000, C051051000, C051051000, C051051000, C051051000, C051051000, new Color(0, 51, 0), C000051051, C000051051, C000051051, C000051051, new Color(51, 51, 51) }; /** * Creates a new MainSwatchPanel object. */ MainSwatchPanel() { super(); numCols = 31; numRows = 9; initializeColors(); revalidate(); } /** * This method returns the color for the given position. * * @param x The x location for the position. * @param y The y location for the position. * * @return The color for the given position. */ public Color getColorForPosition(int x, int y) { if (x % (cellWidth + gap) > cellWidth || y % (cellHeight + gap) > cellHeight) // position is located in gap. return null; int row = y / (cellHeight + gap); int col = x / (cellWidth + gap); return colors[row * numCols + col]; } /** * This method initializes the colors for the main swatch panel. */ protected void initializeColors() { // Unnecessary } /** * This method paints the main graphics panel with the given Graphics * object. * * @param graphics The Graphics object to paint with. */ public void paint(Graphics graphics) { int index = 0; Insets insets = getInsets(); int currX = insets.left; int currY = insets.top; Color saved = graphics.getColor(); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { graphics.setColor(colors[index++]); graphics.fill3DRect(currX, currY, cellWidth, cellHeight, true); currX += gap + cellWidth; } currX = insets.left; currY += gap + cellHeight; } graphics.setColor(saved); } /** * This method returns the tooltip text for the given MouseEvent. * * @param e The MouseEvent to find tooltip text for. * * @return The tooltip text. */ public String getToolTipText(MouseEvent e) { Color c = getColorForPosition(e.getX(), e.getY()); if (c == null) return null; return (c.getRed() + "," + c.getGreen() + "," + c.getBlue()); } } /** * This class is the recent swatch panel. It holds recently selected colors. */ static class RecentSwatchPanel extends SwatchPanel { /** The array for storing recently stored colors. */ Color[] colors; /** The default color. */ public static final Color defaultColor = Color.GRAY; /** The index of the array that is the start. */ int start = 0; /** * Creates a new RecentSwatchPanel object. */ RecentSwatchPanel()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -