GraphMeToColorSynesthesia.java
1    /* 
2     * Progrma to simulate the phenomenon known as graphme 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 | simple show the word or phrase in synesthetic 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 GraphMeToColorSynesthesia {
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,200);
28           miro.setScreenLocation(30,30);
29           miro.setFontSize(fontsize);
30           initializeColorMap(theLetters);
31   
32           //interpretation
33           while(true) {
34               String input = JOptionPane.showInputDialog(null,
35                       "Please enter a word, or a few words ...");
36               if ( input == null) { 
37                   input = "EXIT"; 
38               } 
39               input = input.toUpperCase();
40               if ( input.equals("EXIT")) {
41                   break;
42               } else if (input.equals("REMAP")) {
43                   initializeColorMap(theLetters);
44                   showLetters(miro,theLetters);
45               } else {
46                   showLetters(miro,input.toUpperCase());
47               }
48           }
49           miro.end();
50       }
51   
52       private void showLetters(SPainter miro, String input) {
53           //ready 
54           eraseWhiteBoard(miro);
55           //set
56           miro.moveTo(new Point.Double(100,100));
57           //go
58           for (int i = 0; i < input.length(); i = i +1) {
59               String letter = input.substring(i,i+1);
60               Color color = getLetterColor(letter);
61               miro.setColor(color);
62               miro.draw(letter);
63               miro.mrt(fontsize);
64           }
65       }
66       
67       private void initializeColorMap(String specialLetters) {
68           letters = new String[specialLetters.length()];
69           colors = new Color[specialLetters.length()];
70           for (int i = 0; i < specialLetters.length(); i = i +1) {
71               letters[i] = specialLetters.substring(i,i+1);
72               colors[i] = randomColor(); 
73           }
74       }
75   
76       private Color getLetterColor(String letter) {
77           for (int i = 0; i < letters.length; i = i + 1) {
78               if (letter.equalsIgnoreCase(letters[i])) {
79                   return colors[i];
80               }
81           }
82           return Color.BLACK;
83       }
84   
85       private Color randomColor() {
86           int rv = (int)(Math.random()*256);
87           int gv = (int)(Math.random()*256);
88           int bv = (int)(Math.random()*256);
89           return new Color(rv,gv,bv);
90       }
91   
92       private void eraseWhiteBoard(SPainter miro) {
93           miro.setColor(Color.WHITE);
94           miro.wash();
95           miro.paintFrame(Color.BLACK,5);
96       }
97       //infrastructure or simple painting
98       public GraphMeToColorSynesthesia() {
99           paintingCode();
100      }
101  
102      public static void main(String[] args) {
103          SwingUtilities.invokeLater(new Runnable() {
104              public void run() {
105                  new GraphMeToColorSynesthesia();
106              }
107          });
108      }
109  }