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    
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 = "AEIOU";
19       private static String[] letters;
20       private static Color[] colors;
21   
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   
29           //interpretation
30           while (true) {
31               String input = JOptionPane.showInputDialog(null,
32                       "Please enter a word, or a few words");
33               if (input == null) {
34                   input = "EXIT";
35               }
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           // READY
51           eraseWhiteBoard(miro);
52   
53           // SET
54           miro.moveTo(new Point.Double(100, 100));
55           // GO
56           for (int i = 0; i < input.length(); i = i + 1) {
57               String letter = input.substring(i, i + 1);
58               Color color = getLetterColor(letter);
59               miro.setColor(color);
60               miro.draw(letter);
61               miro.mrt(fontsize);
62           }
63       }
64   
65       private static void initializeColorMap(String specialLetters) {
66           letters = new String[specialLetters.length()];
67           colors = new Color[specialLetters.length()];
68           for (int i = 0; i < specialLetters.length(); i = i + 1) {
69               letters[i] = specialLetters.substring(i, i + 1);
70               colors[i] = randomColor();
71           }
72       }
73   
74       private static Color getLetterColor(String letter) {
75           for (int i = 0; i < letters.length; i = i + 1) {
76               if (letter.equalsIgnoreCase(letters[i])) {
77                   return colors[i];
78               }
79           }
80           return randomColor();
81       }
82   
83       private static Color randomColor() {
84           int rv = (int) (Math.random() * 256);
85           int gv = (int) (Math.random() * 256);
86           int bv = (int) (Math.random() * 256);
87           return new Color(rv, gv, bv);
88       }
89   
90       private static void eraseWhiteBoard(SPainter miro) {
91           miro.setColor(Color.WHITE);
92           miro.wash();
93           miro.paintFrame(Color.black, 5);
94       }
95   
96       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
97       public GraphemeToColorSynesthesia() {
98           paintingCode();
99       }
100  
101      public static void main(String[] args) {
102          SwingUtilities.invokeLater(new Runnable() {
103              public void run() {
104                  new GraphemeToColorSynesthesia();
105              }
106          });
107      }
108  }
109  
110  
111