Package view

Source Code of view.ColorWindow$ColorIcon

package view;

//----- JDK Imports ------------------------------------------------------------
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

//----- Phoenix Imports --------------------------------------------------------
import controller.PhoenixController;

/**
* Video Phoenix
* Version 0.2.0
* Copyright (c) 2007 Lunderskov, Ian; Pan, Jiabei; Rebelsky, Samuel;
* Whisenhunt, Heather; Young, Ian; Zuleta Benavides, Luis.
* All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* @author  Pan, Jiabei; Rebelsky, Samuel; Whisenhunt, Heather
* @author  Glimmer Labs
* @version 0.2.0
*/

@SuppressWarnings("serial")
public class ColorWindow extends JInternalFrame
{
  /*--------*-------------------------------------------------------------------
   * Fields *
   *--------*/

  // main display window
  PhoenixWindow view;


  /*--------------*-------------------------------------------------------------
   * Constructors *
   *--------------*/

  /**
   * Create a new ColorWindow
   *
   * @param controller  PhoenixController for this ColorWindow
   */
  public ColorWindow(PhoenixController controller)
  {
    // set resizable, closable, maximizable, iconifiable
    super("Color Viewer", true, true, true, true);

    this.view = controller.view;
    this.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
    this.setPreferredSize(new Dimension(530, 300));

    // create JPanel in a JScrollPane, using a gridlayout
    JPanel contents = new JPanel();
    GridLayout grid = new GridLayout(0, 3);
    contents.setLayout(grid);

    // get contents of ColorDefs file
    String str;
    try
    {
      BufferedReader in = new BufferedReader(new FileReader(
        PhoenixController.getFile("ColorDefs")));
      while ((str = in.readLine()) != null)
      {
        // don't try to parse comments
        if (!str.substring(0, 1).equals(";"))
        {
          // only consider Scheme "define" statements
          if (str.substring(0, 8).equalsIgnoreCase("(define "))
          {
            int secondSpace = str.indexOf(" ", 8);
            String varName = str.substring(8, secondSpace);

            // ignore color lists
            if (!varName.equalsIgnoreCase("x11-colors")
                && !varName.equalsIgnoreCase("colors"))
            {
              // parse list of color values
              int closeParen = str.indexOf(")");
              String values = str.substring(secondSpace + 3, closeParen);
              int space1 = values.indexOf(" ");
              int space2 = values.indexOf(" ", space1 + 1);
              int red = Integer.parseInt(values.substring(0, space1));
              int green = Integer.parseInt(values.substring(space1 + 1,
                space2));
              int blue = Integer.parseInt(values.substring(space2 + 1));

              // display only colors with normal RGB values
              if (red >= 0 && red < 256 && green >= 0 && green < 256
                  && blue >= 0 && blue < 256)
              {
                Color color = new Color(red, green, blue);

                // create a JLabel with the color and add it to the content pane
                ColorSwatchLabel swatch = new ColorSwatchLabel(color, varName);
                contents.add(swatch);
              } // if (red >= 0 && red < 256 && green >= 0 && green < 256 ...
            } // if (!varName.equalsIgnoreCase("x11-colors") ...
          } // if (temp.substring(0, 8).equalsIgnoreCase("(define "))
        } // if (!temp.substring(0, 1).equals(";"))
      } // while ((str == in.readLine()) != null)
      in.close();
    } // try
    catch (Exception e)
    {
      e.printStackTrace();
    } // catch (Exception)

    contents.validate();
    JScrollPane scroll = new JScrollPane();
    this.setContentPane(scroll);
    this.validate();
    scroll.validate();
    scroll.setVisible(true);
    contents.setVisible(true);
    scroll.setViewportView(contents);
    this.pack();
    this.view.add(this);
    this.setVisible(true);
  } // ColorWindow(PhoenixWindow)

  /* class to create icons that are just a colored rectangle */
  private class ColorIcon implements Icon
  {
    Color color;

    /* create a new ColorIcon with color */
    private ColorIcon(Color color)
    {
      this.color = color; 
    } // ColorIcon(Color)

    /* get the height of the ColorIcon */
    public int getIconHeight()
    {
      return 25;
    } // getIconHeight()

    /* get the width of the ColorIcon */
    public int getIconWidth()
    {
      return 150;
    } // getIconWidth()

    /* paint an icon based on the color */
    public void paintIcon(Component comp, Graphics graph, int x, int y)
    {
      graph.setColor(this.color);
      graph.translate(x, y);
      graph.fillRect(0, 0, 150, 25);
      graph.translate(-x, -y);
    } // paintIcon(Component, Graphics, int, int)
  } // class ColorIcon

  /* class to turn a ColorIcon into a labeled icon */
  private class ColorSwatchLabel extends JLabel
  {
    private ColorSwatchLabel(Color color, String label)
    {
      this.setIcon(new ColorIcon(color));
      this.setBorder(BorderFactory.createTitledBorder(label.toLowerCase()));
    } // ColorSwatchLabel(ColorSwatch)
  } // class ColorSwatchLabel
} // class ColorWindow
TOP

Related Classes of view.ColorWindow$ColorIcon

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.