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