GraphemeToColorSynesthesia.java
1    /* 
2     * Program to simulate the phenomenon known as grapheme to color synesthesia. This Program is written as an 
3     * 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 synesthesia color 
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   import painter.SPainter;
16   
17   public class GraphemeToColorSynesthesia {
18   
19       private static final int fontsize = 30;
20       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
21       private static String[] letters;
22       private static Color[] colors;
23   
24       private void paintingCode() {
25   
26           // INITIALIZATION
27           SPainter miro = new SPainter("",1200,220);
28           miro.setScreenLocation(30,30);
29           miro.setFontSize(fontsize);
30           initializeColorMap(theLetters);
31   
32           // INTERPRETATION
33           while ( true ) {
34               String input = JOptionPane.showInputDialog(null, "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   
50       private static void showLetters(SPainter miro, String input) {
51   
52           // READY
53           eraseWhiteBoard(miro);
54   
55           // SET
56           miro.moveTo(new Point.Double(100,100));
57   
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 = 1; i < specialLetters.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           return Color.BLACK;
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       private static void eraseWhiteBoard(SPainter miro) {
94           miro.setColor(Color.WHITE);
95           miro.wash();
96           miro.paintFrame(Color.BLACK, 5);
97       }
98   
99       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
100      public GraphemeToColorSynesthesia() {
101          paintingCode();
102      }
103  
104      public static void main(String[] args) {
105          SwingUtilities.invokeLater(new Runnable() {
106              @Override
107              public void run() {
108                  new GraphemeToColorSynesthesia();
109              }
110          });
111      }
112  }
113