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