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