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