GraphemeToColorSynesthesia.java
1    package synesthesia;
2    
3    import painter.SPainter;
4    
5    import javax.swing.*;
6    import java.awt.*;
7    
8    public class GraphemeToColorSynesthesia {
9        private static final int fontsize = 30;
10       private static final String theLetters = "ABCDEFGHIJKLMNOPQRDTUVWXYZ";
11       private static String[] letters;
12       private static Color[] colors;
13   
14       private void paintingCode() {
15           //INITIALIZATION
16           SPainter miro = new SPainter("miro",1400,220);
17           miro.setScreenLocation(30,30);
18           miro.setFontSize(fontsize);
19           initializeColorMap(theLetters);
20           //INTERPRETATION
21           while (true) {
22               String input = JOptionPane.showInputDialog(null,"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   
37       private static void showLetters(SPainter miro, String input) {
38   
39           //READY
40           eraseWhiteBoard(miro);
41           //SET
42           miro.moveTo(new Point.Double(100,100));
43           //GO
44           for(int i = 0; i<input.length();i = i + 1) {
45               String letter = input.substring(i,i+1);
46               Color color = getLetterColor(letter);
47               miro.setColor(color);
48               miro.draw(letter);
49               miro.mrt(fontsize);
50           }
51       }
52   
53       private static void initializeColorMap(String specialLetters) {
54           letters = new String[specialLetters.length()];
55           colors = new Color[specialLetters.length()];
56           for(int i = 0;i < specialLetters.length();i = i + 1) {
57               letters[i] = specialLetters.substring(i,i+1);
58               colors[i] = randomColor();
59           }
60       }
61   
62       private static Color getLetterColor(String letter) {
63           for(int i = 0; i < letters.length; i = i + 1) {
64               if(letter.equalsIgnoreCase(letters[i])) {
65                   return colors[i];
66               }
67           }
68           return Color.BLACK;
69       }
70   
71       private static Color randomColor() {
72           int rv = (int) (Math.random()*256);
73           int gv = (int) (Math.random()*256);
74           int bv = (int) (Math.random()*256);
75           return new Color(rv,gv,bv);
76       }
77   
78       private static void eraseWhiteBoard(SPainter miro) {
79           miro.setColor(Color.WHITE);
80           miro.wash();
81           miro.paintFrame(Color.black,5);
82       }
83       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
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   }