GraphemeToColorSynesthesia.java
1    /* Program to simulate the phenomenon known as grapheme to color synesthesia. 
2     * 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 world or phrase in synesthetic color 
6     */
7    package synesthesia;
8    
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   
28               //INTERPRETATION
29               while (true) {
30                   String input = JOptionPane.showInputDialog(null,
31                           "Please enter a word, or phrase ...");
32                   if (input == null) {input = "EXIT";}
33                   input = input.toUpperCase();
34                   if (input.equals("EXIT")) {
35                       break;
36                   } else if (input.equals("REMAP")) {
37                       initializeColorMap(theLetters);
38                       showLetters(miro,theLetters);
39                   } else {
40                       showLetters(miro,input.toUpperCase());
41                   }
42               }
43               miro.end();
44           }
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   
61       private static void initializeColorMap(String specialLetters) {
62           letters = new String[specialLetters.length()];
63           colors = new Color[specialLetters.length()];
64           for (int i = 0; i < letters.length; i = i + 1){
65               letters[i] = specialLetters.substring(i,i+1);
66               colors[i] = randomColor();
67           }
68       }
69   
70       private static Color getLetterColor(String letter) {
71           for(int i = 0; i < letters.length; i = i + 1) {
72               if(letter.equalsIgnoreCase(letters[i])) {
73                   return colors[i];
74               }
75           }
76           return Color.BLACK;
77       }
78   
79       private static Color randomColor() {
80           int rv =(int)(Math.random()*256);
81           int gv =(int)(Math.random()*256);
82           int bv =(int)(Math.random()*256);
83           return new Color(rv,gv,bv);
84       }
85   
86       private static void eraseWhiteBoard(SPainter miro) {
87           miro.setColor(Color.WHITE);
88           miro.wash();
89           miro.paintFrame(Color.black,5);
90       }
91   
92       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
93       public GraphemeToColorSynesthesia() {
94           paintingCode();
95       }
96   
97       public static void main(String[]args) {
98           SwingUtilities.invokeLater(new Runnable() {
99               public void run() {
100                  new GraphemeToColorSynesthesia();
101              }
102          });
103      }
104  }
105