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    
10   import painter.SPainter;
11   
12   import javax.swing.*;
13   import java.awt.*;
14   import java.util.Locale;
15   
16   public class GraphemeToColorSynesthesia {
17       private static final int fontSize = 30;
18       private static final String theLetters = "AEIOU";
19       private static String[] letters;
20       private static Color[] colors;
21   
22       private void paintingCode() {
23   
24           //INITIALIZATION
25           SPainter miro = new SPainter(1200,220);
26           miro.setScreenLocation(30,30);
27           miro.setFontSize(fontSize);
28           initializeColorMap(theLetters);
29   
30           //INTERPRETATION
31           while (true) {
32               String input = JOptionPane.showInputDialog(null,
33                       "Please enter a word, or a few words ...");
34               if (input == null) { input = "EXIT"; }
35               input = input.toUpperCase();
36               if (input.equals("EXIT") ) {
37                   break;
38               } else if (input.equals("REMAP")) {
39                   initializeColorMap(theLetters);
40                   showLetters(miro, theLetters);
41               } else {
42                   showLetters(miro, input.toUpperCase());
43               }
44           }
45           miro.end();
46       }
47   
48       private static void showLetters(SPainter miro, String input) {
49           //READY
50           eraseWhiteBoard(miro);
51           //SET
52           miro.moveTo(new Point.Double(100,100));
53           //GO
54           for (int i = 0; i < input.length(); i++) {
55               String letter = input.substring(i, i + 1);
56               Color color = getLetterColor(letter);
57               miro.setColor(color);
58               miro.draw(letter);
59               miro.mrt(fontSize);
60           }
61       }
62   
63       private static void initializeColorMap(String specialLetters) {
64           letters = new String[specialLetters.length()];
65           colors = new Color[specialLetters.length()];
66           for (int i = 0; i < specialLetters.length(); i++) {
67               letters[i] = specialLetters.substring(i,i+1);
68               colors[i] = randomColor();
69           }
70       }
71   
72       private static Color getLetterColor(String letter) {
73           for (int i = 0; i < letters.length; i++) {
74               if (letter.equalsIgnoreCase(letters[i])) {
75                   return colors[i];
76               }
77           }
78           return Color.BLACK;
79       }
80   
81       private static Color randomColor() {
82           int rv = (int)(Math.random()*256);
83           int gv = (int)(Math.random()*256);
84           int bv = (int)(Math.random()*256);
85           return new Color(rv,gv,bv);
86       }
87   
88       private static void eraseWhiteBoard(SPainter miro) {
89           miro.setColor(Color.WHITE);
90           miro.wash();
91           miro.paintFrame(Color.BLACK, 5);
92       }
93   
94       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
95       public GraphemeToColorSynesthesia() {
96           paintingCode();
97       }
98   
99       public static void main(String[] args) {
100          SwingUtilities.invokeLater(new Runnable() {
101              public void run() {
102                  new GraphemeToColorSynesthesia();
103              }
104          });
105      }
106  }
107