GraphemeToColorSynesthesia.java
1    package synesthesia;
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   
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 GraphemeToColorSynesthesia {
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", 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               }
38               else if (input.equals("REMAP")){
39                   initializeColorMap(theLetters);
40                   showLetters(miro, theLetters);
41               }
42               else
43               {
44                   showLetters(miro, input.toUpperCase());
45               }
46           }
47           miro.end();
48       }
49   
50       private static void showLetters(SPainter miro, String input)
51       {
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           {
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 static void initializeColorMap(String specialLetters)
68       {
69           letters = new String[specialLetters.length()];
70           colors = new Color[specialLetters.length()];
71           for(int i = 0; i < specialLetters.length(); i = i + 1)
72           {
73               letters[i] = specialLetters.substring(i, i + 1);
74               colors[i] = randomColor();
75           }
76       }
77   
78       private static Color getLetterColor(String letter)
79       {
80           for (int i = 0; i < letters.length; i = i + 1)
81           {
82               if (letter.equalsIgnoreCase(letters[i]))
83               {
84                   return colors[i];
85               }
86           }
87           return Color.BLACK;
88       }
89   
90       private static Color randomColor()
91       {
92           int rv = (int)(Math.random()*256);
93           int gv = (int)(Math.random()*256);
94           int bv = (int)(Math.random()*256);
95           return new Color(rv,gv,bv);
96       }
97   
98       private static void eraseWhiteBoard(SPainter miro)
99       {
100          miro.setColor(Color.WHITE);
101          miro.wash();
102          miro.paintFrame(Color.BLACK, 5);
103      }
104  
105      //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
106      public GraphemeToColorSynesthesia()
107      {
108          paintingCode();
109      }
110  
111      public static void main(String[] args)
112      {
113          SwingUtilities.invokeLater(new Runnable() {
114              @Override
115              public void run() {
116                  new GraphemeToColorSynesthesia();
117              }
118          });
119      }
120  }
121