⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultswatchchooserpanel.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    {      super();      numCols = 5;      numRows = 7;      initializeColors();      revalidate();    }    /**     * 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 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[getIndexForCell(row, col)];    }    /**     * This method initializes the colors for the recent swatch panel.     */    protected void initializeColors()    {      colors = new Color[numRows * numCols];      for (int i = 0; i < colors.length; i++)	colors[i] = defaultColor;    }    /**     * This method returns the array index for the given row and column.     *     * @param row The row.     * @param col The column.     *     * @return The array index for the given row and column.     */    private int getIndexForCell(int row, int col)    {      return ((row * numCols) + col + start) % (numRows * numCols);    }    /**     * This method adds the given color to the beginning of the swatch panel.     * Package-private to avoid an accessor method.     *     * @param c The color to add.     */    void addColorToQueue(Color c)    {      if (--start == -1)	start = numRows * numCols - 1;      colors[start] = c;    }    /**     * This method paints the panel with the given Graphics object.     *     * @param g The Graphics object to paint with.     */    public void paint(Graphics g)    {      Color saved = g.getColor();      Insets insets = getInsets();      int currX = insets.left;      int currY = insets.top;      for (int i = 0; i < numRows; i++)        {	  for (int j = 0; j < numCols; j++)	    {	      g.setColor(colors[getIndexForCell(i, j)]);	      g.fill3DRect(currX, currY, cellWidth, cellHeight, true);	      currX += cellWidth + gap;	    }	  currX = insets.left;	  currY += cellWidth + gap;        }    }    /**     * This method returns the tooltip text for the given MouseEvent.     *     * @param e The MouseEvent.     *     * @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 handles mouse events for the two swatch panels.   */  class MouseHandler extends MouseAdapter  {    /**     * This method is called whenever the mouse is pressed.     *     * @param e The MouseEvent.     */    public void mousePressed(MouseEvent e)    {      SwatchPanel panel = (SwatchPanel) e.getSource();      Color c = panel.getColorForPosition(e.getX(), e.getY());      recentPalette.addColorToQueue(c);      DefaultSwatchChooserPanel.this.getColorSelectionModel().setSelectedColor(c);      DefaultSwatchChooserPanel.this.repaint();    }  }  /**   * This is the layout manager for the main panel.   */  static class MainPanelLayout implements LayoutManager  {    /**     * This method is called when a new component is added to the container.     *     * @param name The name of the component.     * @param comp The added component.     */    public void addLayoutComponent(String name, Component comp)    {      // Nothing to do here.    }    /**     * This method is called to set the size and position of the child     * components for the given container.     *     * @param parent The container to lay out.     */    public void layoutContainer(Container parent)    {      Component[] comps = parent.getComponents();      Insets insets = parent.getInsets();      Dimension[] pref = new Dimension[comps.length];      int xpos = 0;      int ypos = 0;      int maxHeight = 0;      int totalWidth = 0;      for (int i = 0; i < comps.length; i++)        {	  pref[i] = comps[i].getPreferredSize();	  if (pref[i] == null)	    return;	  maxHeight = Math.max(maxHeight, pref[i].height);	  totalWidth += pref[i].width;        }      ypos = (parent.getSize().height - maxHeight) / 2 + insets.top;      xpos = insets.left + (parent.getSize().width - totalWidth) / 2;      for (int i = 0; i < comps.length; i++)        {	  if (pref[i] == null)	    continue;	  comps[i].setBounds(xpos, ypos, pref[i].width, pref[i].height);	  xpos += pref[i].width;        }    }    /**     * This method is called when a component is removed from the container.     *     * @param comp The component that was removed.     */    public void removeLayoutComponent(Component comp)    {      // Nothing to do here.    }    /**     * This methods calculates the minimum layout size for the container.     *     * @param parent The container.     *     * @return The minimum layout size.     */    public Dimension minimumLayoutSize(Container parent)    {      return preferredLayoutSize(parent);    }    /**     * This method returns the preferred layout size for the given container.     *     * @param parent The container.     *     * @return The preferred layout size.     */    public Dimension preferredLayoutSize(Container parent)    {      int xmax = 0;      int ymax = 0;      Component[] comps = parent.getComponents();      Dimension pref;      for (int i = 0; i < comps.length; i++)        {	  pref = comps[i].getPreferredSize();	  if (pref == null)	    continue;	  xmax += pref.width;	  ymax = Math.max(ymax, pref.height);        }      Insets insets = parent.getInsets();      return new Dimension(insets.left + insets.right + xmax,                           insets.top + insets.bottom + ymax);    }  }  /**   * This is the layout manager for the recent swatch panel.   */  static class RecentPanelLayout implements LayoutManager  {    /**     * This method is called when a component is added to the container.     *     * @param name The name of the component.     * @param comp The added component.     */    public void addLayoutComponent(String name, Component comp)    {      // Nothing needs to be done.    }    /**     * This method sets the size and position of the child components of the     * given container.     *     * @param parent The container to lay out.     */    public void layoutContainer(Container parent)    {      Component[] comps = parent.getComponents();      Dimension parentSize = parent.getSize();      Insets insets = parent.getInsets();      int currY = insets.top;      Dimension pref;      for (int i = 0; i < comps.length; i++)        {	  pref = comps[i].getPreferredSize();	  if (pref == null)	    continue;	  comps[i].setBounds(insets.left, currY, pref.width, pref.height);	  currY += pref.height;        }    }    /**     * This method calculates the minimum layout size for the given container.     *     * @param parent The container.     *     * @return The minimum layout size.     */    public Dimension minimumLayoutSize(Container parent)    {      return preferredLayoutSize(parent);    }    /**     * This method calculates the preferred layout size for the given     * container.     *     * @param parent The container.     *     * @return The preferred layout size.     */    public Dimension preferredLayoutSize(Container parent)    {      int width = 0;      int height = 0;      Insets insets = parent.getInsets();      Component[] comps = parent.getComponents();      Dimension pref;      for (int i = 0; i < comps.length; i++)        {	  pref = comps[i].getPreferredSize();	  if (pref != null)	    {	      width = Math.max(width, pref.width);	      height += pref.height;	    }        }      return new Dimension(width + insets.left + insets.right,                           height + insets.top + insets.bottom);    }    /**     * This method is called whenever a component is removed from the     * container.     *     * @param comp The removed component.     */    public void removeLayoutComponent(Component comp)    {      // Nothing needs to be done.    }  }  /**   * Creates a new DefaultSwatchChooserPanel object.   */  DefaultSwatchChooserPanel()  {    super();  }  /**   * This method updates the chooser panel with the new value from the   * JColorChooser.   */  public void updateChooser()  {    // Nothing to do here yet.  }  /**   * This method builds the chooser panel.   */  protected void buildChooser()  {    // The structure of the swatch panel is:    // One large panel (minus the insets).    // Inside that panel, there are two panels, one holds the palette.    // The other holds the label and the recent colors palette.    // The two palettes are two custom swatch panels.    setLayout(new MainPanelLayout());    JPanel mainPaletteHolder = new JPanel();    JPanel recentPaletteHolder = new JPanel();    mainPalette = new MainSwatchPanel();    recentPalette = new RecentSwatchPanel();    JLabel label = new JLabel("Recent:");    mouseHandler = new MouseHandler();    mainPalette.addMouseListener(mouseHandler);    recentPalette.addMouseListener(mouseHandler);    mainPaletteHolder.setLayout(new BorderLayout());    mainPaletteHolder.add(mainPalette, BorderLayout.CENTER);    recentPaletteHolder.setLayout(new RecentPanelLayout());    recentPaletteHolder.add(label);    recentPaletteHolder.add(recentPalette);    JPanel main = new JPanel();    main.add(mainPaletteHolder);    main.add(recentPaletteHolder);    this.add(main);  }  /**   * This method removes the chooser panel from the JColorChooser.   *   * @param chooser The JColorChooser this panel is being removed from.   */  public void uninstallChooserPanel(JColorChooser chooser)  {    recentPalette = null;    mainPalette = null;    removeAll();    super.uninstallChooserPanel(chooser);  }  /**   * This method returns the JTabbedPane displayed name.   *   * @return The name displayed in the JTabbedPane.   */  public String getDisplayName()  {    return "Swatches";  }  /**   * This method returns the small display icon.   *   * @return The small display icon.   */  public Icon getSmallDisplayIcon()  {    return null;  }  /**   * This method returns the large display icon.   *   * @return The large display icon.   */  public Icon getLargeDisplayIcon()  {    return null;  }  /**   * This method paints the chooser panel with the given Graphics object.   *   * @param g The Graphics object to paint with.   */  public void paint(Graphics g)  {    super.paint(g);  }  /**   * This method returns the tooltip text for the given MouseEvent.   *   * @param e The MouseEvent.   *   * @return The tooltip text.   */  public String getToolTipText(MouseEvent e)  {    return null;  }}

⌨️ 快捷键说明

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