GraphmeToColorSynesthesia.java
1    /* 
2     * Program to simulate the phenomenon known as graphme 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    package synesthesia;
10   
11   import java.awt.Color;
12   import java.awt.Point;
13   import java.util.Locale;
14   import javax.swing.JOptionPane;
15   import javax.swing.SwingUtilities;
16   import painter.SPainter;
17   
18   public class GraphmeToColorSynesthesia {
19   
20       private static final int fontsize = 30;
21       private static final String theLetters = "AEIOU";
22       private static String[] letters;
23       private static Color[] colors;
24   
25       private void paintingCode() {
26   
27           SPainter miro = new SPainter(1200,220);
28           miro.setScreenLocation(30, 30);
29           miro.setFontSize(fontsize);
30           initializeColorMap(theLetters);
31   
32           while ( true ) {
33               String input = JOptionPane.showInputDialog(null,
34                       "Please enter a word, or a few words ...");
35               if (input == null) { input = "EXIT"; }
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       private static void showLetters(SPainter miro, String input) {
50           eraseWhiteBoard(miro);
51           miro.moveTo(new Point.Double(100,100));
52           for ( int i = 0; i < input.length(); i = i + 1 ) {
53               String letter = input.substring(i, i+1);
54               Color color = getLetterColor(letter);
55               miro.setColor(color);
56               miro.draw(letter);
57               miro.mrt(fontsize);
58           }
59       }
60   
61       private static void initializeColorMap(String specialLetters) {
62           letters = new String[specialLetters.length()];
63           colors = new Color[specialLetters.length()];
64           for ( int i = 0; i < specialLetters.length(); i = i +1) {
65               letters[i] = specialLetters.substring(i,i+1);
66               colors[i] = randomColor();
67           }
68       }
69   
70       private static Color getLetterColor(String letter) {
71           for (int i=0; i< letters.length; i=i+1) {
72               if (letter.equalsIgnoreCase(letters[i])) {
73                   return colors[i];
74               }
75           }
76           return randomColor();
77       }
78   
79       private static Color randomColor() {
80           int rv = (int)(Math.random()*256);
81           int gv = (int)(Math.random()*256);
82           int bv = (int)(Math.random()*256);
83           return new Color(rv,gv,bv);
84       }
85   
86       private static void eraseWhiteBoard(SPainter miro) {
87           miro.setColor(Color.WHITE);
88           miro.wash();
89           miro.paintFrame(Color.black, 5);
90       }
91   
92       public GraphmeToColorSynesthesia() {
93           paintingCode();
94       }
95   
96       public static void main(String[] args) {
97           SwingUtilities.invokeLater(new Runnable() {
98               public void run() {
99                   new GraphmeToColorSynesthesia();
100              }
101          });
102      }
103  }
104