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