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