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