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           SPainter miro = new SPainter(1200, 220);
18           miro.setScreenLocation(30, 30);
19           miro.setFontSize(fontsize);
20           initializeColorMap(theLetters);
21   
22           while (true) {
23               String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words ...");
24               if (input == null) {
25                   input = "EXIT";
26               }
27               input = input.toUpperCase();
28               if (input.equals("EXIT")) {
29                   break;
30               } else if (input.equals("REMAP")) {
31                   initializeColorMap(theLetters);
32                   showLetters(miro, theLetters);
33               } else {
34                   showLetters(miro, input.toUpperCase());
35               }
36           }
37           miro.end();
38   
39   
40       }
41   
42       private static void showLetters(SPainter miro, String input) {
43           eraseWhiteBoard(miro);
44           miro.moveTo(new Point.Double(100, 100));
45           for (int i = 0; i < input.length(); i = i + 1) {
46               String letter = input.substring(i, i + 1);
47               Color color = getLetterColor(letter);
48               miro.setColor(color);
49               miro.draw(letter);
50               miro.mrt(fontsize);
51           }
52   
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       }
64       private static Color getLetterColor(String letter) {
65           for ( int i = 0; i < letters.length; i =i + 1 ) {
66               if ( letter.equalsIgnoreCase(letters[i]) ) {
67                   return colors[i];
68               }
69           }
70           return Color.BLACK;
71       }
72       private static Color randomColor() {
73           int rv = (int)(Math.random()*256);
74           int gv = (int)(Math.random()*256);
75           int bv = (int)(Math.random()*256);
76           return new Color(rv,gv,bv);
77       }
78       private static void eraseWhiteBoard(SPainter miro) {
79           miro.setColor(Color.WHITE);
80           miro.wash();
81           miro.paintFrame(Color.black, 5);
82       }
83   
84       public GraphemeToColorSynesthesia() {
85           paintingCode();
86       }
87       public static void main(String[] args) {
88           SwingUtilities.invokeLater(new Runnable() {
89               public void run() {
90                   new GraphemeToColorSynesthesia();
91               }
92           });
93       }
94   
95   
96   }