GraphemeToColorSynesthesia.java
1    /* 
2    *Program to stimulate the phenomenon known as grapheme to color synesthesia. 
3    * -exit | terminate the program 
4    * -remap | redefine the mapping from letters to colors 
5    * - WORD OR PHRASE | simply show the word or phrase in synesthesia color 
6     */
7    
8    package synesthesia;
9    
10   import java.awt.Color;
11   import java.awt.Point;
12   import javax.swing.JOptionPane;
13   import javax.swing.SwingUtilities;
14   import painter.SPainter;
15   
16   public class GraphemeToColorSynesthesia {
17       private static final int fontsize = 30;
18       private static final String theLetters = "QWERTYUIOPASDFGHJKLZXCVBNM";
19       private static String[] letters;
20       private static Color[] colors;
21   
22       private void paintingCode() {
23   
24           //INITIALIZATION
25           SPainter miro =  new SPainter("GraphemeToColorSynnesthesia",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,"Please enter a word, or a few words...");
33               if ( input == null) {
34                   input = "EXIT";
35               }
36               input = input.toUpperCase();
37               if(input.equals("EXIT")){
38                   break;
39               } else if (input.equals("REMAP")){
40                   initializeColorMap(theLetters);
41                   showLetters(miro, theLetters);
42               } else {
43                   showLetters(miro,input.toUpperCase());
44               }
45           }
46           miro.end();
47   
48       }
49   
50       private 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 Color getLetterColor(String letter) {
75           for(int i =0;i < letters.length; i = i + 1){
76               if(letter.equalsIgnoreCase(letters[i])) {
77                   return colors[i];
78               }
79           }
80           return Color.BLACK;
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       private static void eraseWhiteBoard(SPainter miro){
89           miro.setColor(Color.WHITE);
90           miro.wash();
91           miro.paintFrame(Color.black,5);
92       }
93       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
94       public GraphemeToColorSynesthesia(){
95           paintingCode();
96       }
97   
98       public static void main(String[] args) {
99           SwingUtilities.invokeLater(new Runnable() {
100          public void run() {
101              new GraphemeToColorSynesthesia();
102          }
103          });
104      }
105  }
106