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