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