Source of ColourSwatch.java


  2: import java.awt.*;
  3: import javax.swing.*;

  5: /**
  6:  * A window (or label) that changes colour according to the user's choices.
  7:  * NOTE: this file can use FontFixer.java to enlarge its fonts.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class ColourSwatch extends JFrame {

 13:     private final JLabel fill;
 14:     private final JLabel redLabel;
 15:     private final JLabel greenLabel;
 16:     private final JLabel blueLabel;
 17:     private final JTextField decodeField;

 19:     public static final int MARGIN = 2;

 21:     /**
 22:      * Create and show the window.
 23:      *
 24:      * @param args cheerfully ignored
 25:      */
 26:     public static void main(String[] args) {
 27:         ColourSwatch win = new ColourSwatch();
 28:         win.setVisible(true);
 29:     }

 31:     /**
 32:      * Create the window.
 33:      */
 34:     public ColourSwatch() {
 35:         super("Choose the colour");
 36:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 37:         setSize(280, 320);  // Slightly over-sized for edges and margins
 38:         setResizable(false);

 40:         // create the colouring bit
 41:         fill = new JLabel("");
 42:         fill.setOpaque(true);

 44:         // make the fill area slightly bigger than needed
 45:         // (so we can add "margins")
 46:         fill.setPreferredSize(new Dimension(256 + 2*MARGIN, 256 + 2*MARGIN));

 48:         // create the RGB label bit
 49:         JPanel rgb = new JPanel(new FlowLayout());
 50:         redLabel = new JLabel("Red: ");
 51:         greenLabel = new JLabel("Green: ");
 52:         blueLabel = new JLabel("Blue: ");
 53:         rgb.add(redLabel);
 54:         rgb.add(greenLabel);
 55:         rgb.add(blueLabel);
 56:         
 57:         // create the decode control
 58:         JPanel decode = new JPanel(new FlowLayout());
 59:         decode.add(new JLabel("Color.decode(\""));
 60:         decodeField = new JTextField();
 61:         decodeField.setPreferredSize(new Dimension(200, 50));
 62:         decodeField.addActionListener(e -> 
 63:                 changeColour(this, decodeField.getText()));
 64:         decode.add(decodeField);
 65:         decode.add(new JLabel("\");"));
 66:         
 67:         // create the adjustment controls
 68:         JPanel adjust = new JPanel(new FlowLayout());
 69:         JButton brighter = new JButton("brighter()");
 70:         brighter.addActionListener(e ->
 71:                 changeColour(this, fill.getBackground().brighter()));
 72:         JButton darker = new JButton("darker();");
 73:         darker.addActionListener(e ->
 74:                 changeColour(this, fill.getBackground().darker()));
 75:         adjust.add(new JLabel("fill Colour = fillColour."));
 76:         adjust.add(brighter);
 77:         adjust.add(new JLabel("        "));
 78:         adjust.add(new JLabel("fill Colour = fillColour."));
 79:         adjust.add(darker);

 81:         // add the components
 82:         JPanel controls = new JPanel(new GridLayout(0, 1));
 83:         controls.add(rgb);
 84:         controls.add(decode);
 85:         controls.add(adjust);
 86:         add(fill);
 87:         add(controls, BorderLayout.SOUTH);
 88:         
 89:         // make larger for use in class
 90:         // FontFixer.resizeFont(this, 24);

 92:         //  make this window just the right size for its componebts
 93:         pack();
 94:     }

 96:     /**
 97:      * change colour of window/label
 98:      * 
 99:      * @param win the window to change the colour of
100:      * @param colourName the name of the colour to change it to
101:      */
102:     private static void changeColour(ColourSwatch win, String colourName) {
103:         Color newColour = getColourByName(colourName);
104:         if (newColour != null) {
105:             changeColour(win, newColour);
106:         }
107:     }
108:     
109:     /**
110:      * change colour of window/label
111:      * 
112:      * @param win the window to change the colour of
113:      * @param newColour the colour to change it to
114:      */
115:     private static void changeColour(ColourSwatch win, Color newColour) {
116:         int r = newColour.getRed();
117:         int g = newColour.getGreen();
118:         int b = newColour.getBlue();
119:         win.fill.setBackground(newColour);
120:         win.redLabel.setText("Red: " + r);
121:         win.greenLabel.setText("Green: " + g);
122:         win.blueLabel.setText("Blue: " + b);
123:         win.decodeField.setText("");
124:     }

126:     /**
127:      * Look up a colour by its name. Return null if can't find the colour.
128:      * <p>
129:      * STUDENTS OF CSCI1226 DO NOT NEED TO UNDERSTAND THIS METHOD.  It uses
130:      * ideas from CSCI 1228. If you go on to 1228, you will get to see what
131:      * this method does and how it does it.
132:      *
133:      * @param name the name of the colour
134:      * @return the named colour, or null if it's not a recognized colour name.
135:      */
136:     private static Color getColourByName(String name) {
137:         try {
138:             javafx.scene.paint.Color c = javafx.scene.paint.Color.web(name);
139:             int red = scale(c.getRed());
140:             int green = scale(c.getGreen());
141:             int blue = scale(c.getBlue());
142:             return new Color(red, green, blue);
143:         } catch (IllegalArgumentException iae) {
144:             return null;
145:         }
146:     }
147:     
148:     /**
149:      * scale a number in the range [0,1] to an int in the range [0,255]
150:      * 
151:      * @param x the number in the range [0,1] to scale
152:      * @return x scaled up to an integer in the range [0,255]
153:      */
154:     private static int scale(double x) {
155:         return (int)Math.round(x * 255);
156:     }

158: }