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