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