public class ColourByNameNoFX extends JFrame
1: import java.awt.BorderLayout;
2: import java.awt.Color;
3: import java.awt.Component;
4: import java.awt.Container;
5: import java.awt.Dimension;
6: import java.awt.Font;
7: import javax.swing.JFrame;
8: import javax.swing.JLabel;
9: import javax.swing.JPanel;
10: import javax.swing.JTextField;
12: /**
13: * A window (or label) that changes colour as you move the mouse
14: *
15: * @author Mark Young (A00000000)
16: * @version 1.0
17: */
18: public class ColourByNameNoFX extends JFrame {
20: private final JLabel swatch;
21: private final JLabel redLabel;
22: private final JLabel greenLabel;
23: private final JLabel blueLabel;
25: private final JTextField colourName;
27: public static final int MARGIN = 2;
29: /**
30: * Create and show the window.
31: *
32: * @param args cheerfully ignored
33: */
34: public static void main(String[] args) {
35: ColourByNameNoFX win = new ColourByNameNoFX();
36: win.setLocationRelativeTo(null);
37: win.setVisible(true);
38: }
40: /**
41: * Create the window.
42: */
43: public ColourByNameNoFX() {
44: super("Colours By Name");
45: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46: setSize(800, 600);
48: // create the colouring bit
49: swatch = new JLabel("");
50: swatch.setOpaque(true);
51: swatch.setBackground(new Color(0, 0, 0));
53: // create the RGB label bit
54: JPanel rgb = new JPanel();
55: redLabel = new JLabel("Red: ");
56: greenLabel = new JLabel("Green: ");
57: blueLabel = new JLabel("Blue: ");
58: rgb.add(redLabel);
59: rgb.add(greenLabel);
60: rgb.add(blueLabel);
62: // create the control area
63: JPanel control = new JPanel();
64: colourName = new JTextField();
65: colourName.setPreferredSize(new Dimension(200, 30));
66: colourName.addActionListener(e
67: -> getColourByName(colourName.getText()));
68: control.add(new JLabel("Colour name or code: "));
69: control.add(colourName);
71: // add the components
72: add(swatch);
73: add(rgb, BorderLayout.PAGE_END);
74: add(control, BorderLayout.PAGE_START);
76: // adjust the font sizes
77: resizeFont(this, 24);
78: }
80: /**
81: * Change the colour of the swatch. Caller's responsibility to ensure that
82: * the levels are in the correct range.
83: *
84: * @param r the amount of red (0..255)
85: * @param g the amount of green (0..255)
86: * @param b the amount of blue (0..255)
87: */
88: private void changeColour(int red, int green, int blue) {
89: Color c = new Color(red, green, blue);
90: redLabel.setText("Red: " + red);
91: greenLabel.setText("Green: " + green);
92: blueLabel.setText("Blue: " + blue);
93: swatch.setBackground(c);
94: }
96: /**
97: * Set the colour of the swatch and the RGB labels according to the named
98: * colour. If the colour name/description isn't valid, then set the swatch
99: * black and give the colour name text box a pink background.
100: *
101: * @param name the name of the colour
102: * @return the named colour, or null if it's not a recognized colour name.
103: */
104: private void getColourByName(String name) {
105: Color color = ColourNames.get(name);
106: if (color != null) {
107: changeColour(color.getRed(), color.getGreen(), color.getBlue());
108: colourName.setBackground(Color.white);
109: } else {
110: changeColour(0, 0, 0);
111: colourName.setBackground(Color.pink);
112: }
113: }
115: /**
116: * Change the font size to a fixed value in a container and all its
117: * components.
118: *
119: * @param c the container to change the font size in
120: * @param size the size to change it to
121: */
122: private static void resizeFont(Component c, double size) {
123: Font myFont = c.getFont();
124: if (myFont != null) {
125: c.setFont(myFont.deriveFont((float) size));
126: }
127: if (c instanceof Container) {
128: for (Component comp : ((Container) c).getComponents()) {
129: resizeFont(comp, size);
130: }
131: }
132: }
133: }