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