GraphemeToColorSynesthesia.java
1    /* Program to simulate the phenomenon known as grapheme to color synesthesia. 
2     * This program is written as an interpreter that recognizes and responds to: 
3     * - exit | terminate the program 
4     * - remap | redefine the mapping from letters to colors 
5     * - WORD OR PHRASE | simply show the word or phrase in synesthetic color 
6     */
7    
8    package synesthesia;
9    
10   import java.awt.Color;
11   import java.awt.Point;
12   import javax.swing.JOptionPane;
13   import javax.swing.SwingUtilities;
14   
15   import painter.SPainter;
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           // INITIALIZATIONS
25           SPainter miro = new SPainter("Synesthesia", 1200, 220);
26           miro.setScreenLocation(30, 30);
27           miro.setFontSize(fontsize);
28           initializeColorMap(theLetters);
29           // INTERPRETATION
30           while (true) {
31               String input = JOptionPane.showInputDialog(null,
32   
33                       "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               } else if (input.equals("REMAP")) {
41                   initializeColorMap(theLetters);
42                   showLetters(miro, theLetters);
43               } else {
44                   showLetters(miro, input.toUpperCase());
45               }
46           }
47           miro.end();
48       }
49   
50       private static void showLetters(SPainter miro, String input) {
51           eraseWhiteBoard(miro);
52           miro.moveTo(new Point.Double(100, 100));
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 void initializeColorMap(String specialLetters) {
63           letters = new String[specialLetters.length()];
64           colors = new Color[specialLetters.length()];
65           for (int i = 0; i < specialLetters.length(); i = i + 1) {
66               letters[i] = specialLetters.substring(i, i + 1);
67               colors[i] = randomColor();
68           }
69       }
70   
71       private static Color getLetterColor(String letter) {
72           for (int i = 0; i < letters.length; i = i + 1) {
73               if (letter.equalsIgnoreCase(letters[i])) {
74                   return colors[i];
75               }
76           }
77           return Color.BLACK;
78       }
79   
80       private static Color randomColor() {
81           int rv = (int) (Math.random() * 256);
82           int gv = (int) (Math.random() * 256);
83           int bv = (int) (Math.random() * 256);
84           return new Color(rv, gv, bv);
85       }
86   
87       private static void eraseWhiteBoard(SPainter miro) {
88           miro.setColor(Color.WHITE);
89           miro.wash();
90           miro.paintFrame(Color.black, 5);
91       }
92   
93       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
94       public GraphemeToColorSynesthesia() {
95           paintingCode();
96       }
97       public static void main(String[] args) {
98           SwingUtilities.invokeLater(new Runnable() {
99               public void run() {
100                  new GraphemeToColorSynesthesia();
101              }
102          });
103      }
104  }
105  
106  
107