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