GraphmeToColorSynesthesia.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 thee word or phrase in synesthetic color 
7     */
8    
9    package synesthesia;
10   
11   import java.awt.Color;
12   import java.awt.Point;
13   import javax.swing.JOptionPane;
14   import javax.swing.SwingUtilities;
15   import painter.SPainter;
16   
17   public class GraphmeToColorSynesthesia {
18   
19       private static final int fontsize = 30;
20       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
21       private static String[] letters;
22       private static Color[] colors;
23   
24       private void paintingCode() {
25   
26           //INITIALIZATION
27           SPainter miro = new SPainter("word",1200,220);
28           miro.setScreenLocation(30,30);
29           miro.setFontSize(fontsize);
30           initializeColorMap(theLetters);
31   
32           //INTERPRETATION
33           while (true){
34               String input = JOptionPane.showInputDialog(null,
35                       "Please enter a word, or a few words ...");
36               if (input == null){input = "EXIT"; }
37               input = input.toUpperCase();
38               if (input.equals("EXIT")){
39                   break;
40               } else if (input.equals("REMAP")){
41                   initializeColorMap(theLetters);
42                   showLetters(miro, theLetters);
43               } else {
44                   showLetters(miro,input.toUpperCase());
45               }
46           }
47           miro.end();
48       }
49   
50       private static void showLetters(SPainter miro, String input){
51           //READY
52           eraseWhiteBoard(miro);
53           //SET
54           miro.moveTo(new Point.Double(100,100));
55           //GO
56           for (int i = 0; i < input.length(); i = i + 1 ) {
57               String letter = input.substring(i,i+1);
58               Color color = getLetterColor(letter);
59               miro.setColor(color);
60               miro.draw(letter);
61               miro.mrt(fontsize);
62           }
63       }
64   
65       private static void initializeColorMap(String specialLetters) {
66           letters = new String[specialLetters.length()];
67           colors = new Color[specialLetters.length()];
68           for (int i = 0; i < specialLetters.length(); i = i + 1){
69               letters[i] = specialLetters.substring(i,i+1);
70               colors[i] = randomColor();
71           }
72       }
73   
74       private static Color getLetterColor(String letter) {
75           for (int i = 0; letters.length > i; i = i + 1) {
76               if (letter.equalsIgnoreCase(letters[i])){
77                   return colors[i];
78               }
79           }return randomColor();
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 GraphmeToColorSynesthesia() {
97           paintingCode();
98       }
99   
100      public static void main(String[] args){
101          SwingUtilities.invokeLater(new Runnable() {
102              public void run() {
103                  new GraphmeToColorSynesthesia();
104              }
105          });
106      }
107  }
108