GraphemeToColorSynesthesia.java
1    /* 
2     * Program to simulate the phenomenon known as grapheme to color synesthesia. 
3     * This program is written as an interpreter that recognizes and responds to: 
4     * - exit | terminate the program 
5     * - remap | redefine the mapping from letters to colors 
6     *  - WORD OR PHRASE | simply show the word or phrase in synesthetic color 
7    */
8    package synesthesia;
9    import java.awt.Color;
10   import java.awt.Point;
11   import javax.swing.JOptionPane;
12   import javax.swing.SwingUtilities;
13   import painter.SPainter;
14   public class GraphemeToColorSynesthesia {
15       private static final int fontsize = 30;
16       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
17       private static String[] letters;
18       private static Color[] colors;
19       private void paintingCode() {
20           // INITIALIZATION
21           SPainter miro = new SPainter("GraphemeToColorSynesthesia", 1200, 220);
22           miro.setScreenLocation(30, 30);
23           miro.setFontSize(fontsize);
24           initializeColorMap(theLetters);
25           // INTERPRETATION
26           while (true) {
27               String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words ...");
28               if (input == null) {
29                   input = "EXIT";
30               }
31               input = input.toUpperCase();
32               if (input.equals("EXIT")) {
33                   break;
34               } else if (input.equals("REMAP")) {
35                   initializeColorMap(theLetters);
36                   showLetters(miro, theLetters);
37               } else {
38                   showLetters(miro, input.toUpperCase());
39               }
40           }
41           miro.end();
42       }
43       private static void showLetters(SPainter miro, String input) {
44           // READY
45           eraseWhiteBoard(miro);
46           // SET
47           miro.moveTo(new Point.Double(100, 100));
48           // GO
49           for (int i = 0; i < input.length(); i = i + 1) {
50               String letter = input.substring(i, i + 1);
51               Color color = getLetterColor(letter);
52               miro.setColor(color);
53               miro.draw(letter);
54               miro.mrt(fontsize);
55           }
56       }
57       private static void initializeColorMap(String specialLetters) {
58           letters = new String[specialLetters.length()];
59           colors = new Color[specialLetters.length()];
60           for (int i = 0; i < specialLetters.length(); i = i + 1) {
61               letters[i] = specialLetters.substring(i, i + 1);
62               colors[i] = randomColor();
63           }
64       }
65       private static Color getLetterColor(String letter) {
66           for (int i = 0; i < letters.length; i = i + 1) {
67               if (letter.equalsIgnoreCase(letters[i])) {
68                   return colors[i];
69               }
70           }
71           return Color.BLACK;
72       }
73       private static Color randomColor() {
74           int rv = (int) (Math.random() * 256);
75           int gv = (int) (Math.random() * 256);
76           int bv = (int) (Math.random() * 256);
77           return new Color(rv, gv, bv);
78       }
79       private static void eraseWhiteBoard(SPainter miro) {
80           miro.setColor(Color.WHITE);
81           miro.wash();
82           miro.paintFrame(Color.black, 5);
83       }
84       // INFRSTRUCTURE FOR SOME SIMPLE PAINTING
85       public GraphemeToColorSynesthesia() {
86           paintingCode();
87       }
88       public static void main(String[] args) {
89           SwingUtilities.invokeLater(new Runnable() {
90               public void run() {
91                   new GraphemeToColorSynesthesia();
92               }
93           });
94       }
95   }
96