public class ColoursNoFX
1: import java.awt.Color;
2: import java.util.Scanner;
4: /**
5: * A program to report on the properties of colours. It explains itself.
6: *
7: * @author Mark Young (A00000000)
8: */
9: public class ColoursNoFX {
11: public static void main(String[] args) {
12: // create variables
13: Scanner kbd = new Scanner(System.in);
14: Color userColour;
15: String colourName;
17: // introduce yourself
18: printIntroduction();
20: // read colours ...
21: System.out.print("Enter a colour name: ");
22: colourName = kbd.nextLine();
24: // ... until user says to quit
25: while (!meansQuit(colourName)) {
26: // add a blank line after user's input
27: System.out.println();
29: // get the colour named, and if it's valid ...
30: if (!colourName.isEmpty()) {
31: userColour = getColourByName(colourName);
32: if (userColour != null) {
33: // ... report components of that colour
34: System.out.println("The colour " + colourName + ":");
35: report("red", userColour.getRed());
36: report("green", userColour.getGreen());
37: report("blue", userColour.getBlue());
38: } else {
39: // invalid names get an apology
40: System.out.println("Sorry, I don't recognize that colour.");
41: }
42: } else {
43: System.out.println("\nUse QUIT, STOP, DONE or EXIT to quit.\n");
44: }
46: // get another colour name
47: System.out.println();
48: System.out.print("Enter a colour name: ");
49: colourName = kbd.nextLine();
50: }
51: System.out.println();
53: }
55: /**
56: * Print an introduction to this program.
57: */
58: public static void printIntroduction() {
59: Utilities.printTitle("Properties of Colours");
60: Utilities.printParagraph(""
61: + "Colours are usually represented on the computer by three "
62: + "numbers in the range of 0 to 255. "
63: + "These three numbers represent how much red light "
64: + "the colour uses, "
65: + "how much green light, "
66: + "and how much blue light.");
67: Utilities.printParagraph(""
68: + "The bigger the numbers are, the brighter the colour is. "
69: + "White, for example, is fully 255 on each component, "
70: + "while black is fully zero on each component.");
71: Utilities.printParagraph("This program understands any name "
72: + "from the HTML set of colour names. ");
73: Utilities.printParagraph("Try it out!");
74: }
76: /**
77: * Check whether a string means QUIT.
78: *
79: * @param word the word to check
80: * @return true if the word is commonly used to indicate ending a program
81: */
82: public static boolean meansQuit(String word) {
83: // avoid problems with empty strings, which don't mean "quit" anyways!
84: if (word.isEmpty()) {
85: return false;
86: }
87: // change to lower case to avoid captialization issues
88: word = word.toLowerCase();
90: // check if it abbreviates a quitting word
91: return "quit".startsWith(word)
92: || "stop".startsWith(word)
93: || "done".startsWith(word)
94: || "exit".startsWith(word)
95: || "halt".startsWith(word);
96: }
98: /**
99: * Print a report on the amount of a component. The amount is a number from
100: * 0 to 1, and is reported as a percentage and as a ratio out of 255. It is
101: * intended as a report on colour components, which can be identified in
102: * either of those ways.
103: *
104: * @param component the name of the component ("red", "green", or "blue")
105: * @param level how much of the component to report
106: */
107: public static void report(String component, int level) {
108: double pct = 100.0 * level / 255.0;
110: System.out.println("\tuses " + String.format("%.1f", pct) + "% of the "
111: + component + " component (" + level + " out of 255)");
112: }
114: /**
115: * Look up a colour by its name. Return null if can't find the colour.
116: * <p>
117: * STUDENTS OF CSCI1226 DO NOT NEED TO UNDERSTAND THIS METHOD. It uses ideas
118: * from CSCI 1228. If you go on to 1228, you will get to see what this
119: * method does and how it does it.
120: *
121: * @param name the name of the colour
122: * @return the named colour, or null if it's not a recognized colour name.
123: */
124: private static Color getColourByName(String name) {
125: return ColourNames.get(name);
126: }
128: }