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