GraphemeToColorSynesthesia.java
1    /* 
2     * Program to simulate the phenomenon known as grapheme to color synesthesia. 
3     * This program is written as a 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 phrase in synesthesia color 
7     */
8    
9    package synesthesia;
10   
11   import painter.SPainter;
12   
13   import javax.swing.*;
14   import java.awt.*;
15   
16   public class GraphemeToColorSynesthesia {
17       private static final int fontsize = 30;
18       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
19       private static String[] letters;
20       private static Color[] colors;
21   
22       private void paintingCode() {
23   
24           // INITIALIZATION
25           SPainter miro = new SPainter ("Synesthesia",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) { input = "EXIT"; }
34               input = input.toUpperCase();
35               if ( input.equals("EXIT")){
36                   break;
37               } else if ( input.equals("REMAP")){
38                   initializeColorMap(theLetters);
39                   showLetters(miro,theLetters);
40               } else{
41                   showLetters(miro,input.toUpperCase());
42               }
43           }
44           miro.end();
45       }
46   
47       private static void showLetters(SPainter miro, String input) {
48           // READY
49           eraseWhiteBoard (miro);
50           // SET
51           miro.moveTo(new Point.Double(100,100) );
52           // GO
53           for (int i = 0; i<input.length(); i = i + 1){
54               String letter = input.substring(i, i+1);
55               Color color = getLetterColor(letter);
56               miro.setColor(color);
57               miro.draw(letter);
58               miro.mrt(fontsize);
59           }
60       }
61   
62       private static Color getLetterColor(String letter) {
63           for (int i = 0; i < letters.length; i = i + 1){
64               if (letter.equalsIgnoreCase(letters[i])){
65                   return colors[i];
66               }
67           }
68           return Color.black;
69       }
70   
71       private static void eraseWhiteBoard(SPainter miro) {
72           miro.setColor(Color.white);
73           miro.wash();
74           miro.paintFrame(Color.black,5);
75       }
76   
77       private static void initializeColorMap(String specialLetters) {
78           letters = new String[specialLetters.length()];
79           colors = new Color[specialLetters.length()];
80           for ( int i = 0; i<specialLetters.length(); i = i+1){
81               letters[i] = specialLetters.substring(i,i+1);
82               colors[i] = randomColor();
83           }
84       }
85   
86       private static Color randomColor() {
87           int rv = (int) (Math.random()*256);
88           int gv = (int) (Math.random()*256);
89           int bv = (int) (Math.random()*256);
90           return new Color (rv,gv,bv);
91       }
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