GraphemeToColorSynesthesia.java
1    /* 
2     * Program to simulate the phenomenon known as graphemeto color synesthesia. 
3     * This program is written as an interpreter that recognises 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   
18       private static final int fontsize = 30;
19       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
20       private static String[] letters;
21       private static Color[] colors;
22   
23       private void paintingCode() {
24   
25           //INITIALIZATION
26           SPainter miro = new SPainter(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,
34                       "Please enter a word, or a few words ...");
35               if (input == null) {
36                   input = "EXIT";
37               }
38               input = input.toUpperCase();
39               if (input.equals("EXIT")) {
40                   break;
41               } else if (input.equals("REMAP")) {
42                   initializeColorMap(theLetters);
43                   showLetters(miro, theLetters);
44               } else {
45                   showLetters(miro, input.toUpperCase());
46               }
47           }
48           miro.end();
49       }
50   
51       private static void showLetters(SPainter miro, String input) {
52           //READY
53           eraseWhiteBoard(miro);
54           //SET
55           miro.moveTo(new Point.Double(100, 100));
56           //GO
57           for (int i = 0; i < input.length(); i = i + 1) {
58               String letter = input.substring(i, i + 1);
59               Color color = getLetterColor(letter);
60               miro.setColor(color);
61               miro.draw(letter);
62               miro.mrt(fontsize);
63           }
64       }
65   
66       private static void initializeColorMap(String specialLetters) {
67           letters = new String[specialLetters.length()];
68           colors = new Color[specialLetters.length()];
69           for (int i = 0; i < specialLetters.length(); i = i + 1) {
70               letters[i] = specialLetters.substring(i, i + 1);
71               colors[i] = randomColor();
72           }
73       }
74   
75       private static Color getLetterColor(String letter) {
76           for (int i = 0; i < letters.length; i = i + 1) {
77               if ( letter.equalsIgnoreCase(letters[i])) {
78                   return colors[i];
79               }
80           }
81           return Color.BLACK;
82       }
83   
84       private static Color randomColor() {
85           int rv = (int)(Math.random()*256);
86           int gv = (int)(Math.random()*256);
87           int bv = (int)(Math.random()*256);
88           return new Color(rv,gv,bv);
89       }
90   
91       private static void eraseWhiteBoard(SPainter miro) {
92           miro.setColor(Color.WHITE);
93           miro.wash();
94           miro.paintFrame(Color.black, 5);
95       }
96   
97       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
98       public GraphemeToColorSynesthesia() {
99           paintingCode();
100      }
101  
102      public static void main(String[] args) {
103          SwingUtilities.invokeLater(new Runnable() {
104              public void run() {
105                  new GraphemeToColorSynesthesia();
106              }
107          });
108      }
109  }
110  
111