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    
10   public class GraphemeToColorSynesthesia {
11   
12       private static final int fontsize = 30;
13       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14       private static String[] letters;
15       private static Color[] colors;
16   
17       private void paintingCode() {
18   
19           //INITIALIZATION
20           SPainter miro = new SPainter("SYNESTHESIA", 1200, 220);
21           miro.setScreenLocation(30, 30);
22           ;
23           miro.setFontSize(fontsize);
24           initializeColorMap(theLetters);
25   
26           //INTERPRETATION
27           while (true) {
28               String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words... ");
29               if (input == null) {
30                   input = "EXIT";
31               }
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   
46       private static void showLetters(SPainter miro, String input) {
47           //READY
48           eraseWhiteBoard(miro);
49           //SET
50           miro.moveTo(new Point.Double(100, 100));
51           //GO
52           for (int i = 0; i < input.length(); i = i + 1) {
53               String letter = input.substring(i, i + 1);
54               Color color = getLetterColor(letter);
55               miro.setColor(color);
56               miro.draw(letter);
57               miro.mrt(fontsize);
58           }
59       }
60   
61       private static void initializeColorMap(String specialLetters) {
62           letters = new String[specialLetters.length()];
63           colors = new Color[specialLetters.length()];
64           for (int i = 0; i < specialLetters.length(); i = i + 1) {
65               letters[i] = specialLetters.substring(i, i + 1);
66               colors[i] = randomColor();
67           }
68       }
69   
70       private static Color getLetterColor(String letter) {
71           for (int i = 0; i < letters.length; i = i + 1) {
72               if (letter.equalsIgnoreCase(letters[i])) {
73                   return colors[i];
74               }
75           }
76           return Color.BLACK;
77       }
78   
79       private static Color randomColor() {
80           int rv = (int) (Math.random() * 256);
81           int gv = (int) (Math.random() * 256);
82           int bv = (int) (Math.random() * 256);
83           return new Color(rv, gv, bv);
84   
85       }
86   
87       private static void eraseWhiteBoard(SPainter miro) {
88           miro.setColor(Color.WHITE);
89           miro.wash();
90           miro.paintFrame(Color.black, 5);
91       }
92   
93       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
94   
95       public GraphemeToColorSynesthesia() {
96           paintingCode();
97       }
98   
99       public static void main(String[] args) {
100          SwingUtilities.invokeLater(new Runnable() {
101              public void run() {
102                  new GraphemeToColorSynesthesia();
103  
104              }
105          });
106      }
107  }