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= "ABCEDFGHIJKLMNOPQRSTUVWXYZ";
11       private static String[] letters;
12       private static Color[] colors;
13   
14       private void paintingCode(){
15           // INITIALIZATION
16           SPainter miro= new SPainter("Colorful", 1200, 200);
17           miro.setScreenLocation(30,30);
18           miro.setFontSize(fontsize);
19           initializeColorMap(theLetters);
20   
21           //INTERPRETATION
22           while (true) {
23               String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words...");
24               if (input == null) { input = "EXIT"; }
25               input = input.toUpperCase();
26               if (input.equals("EXIT")) {
27                   break;
28               } else if (input.equals("REMAP")) {
29                   initializeColorMap(theLetters);
30                   showLetters(miro,theLetters);
31               } else {
32                   showLetters(miro, input.toUpperCase());
33               }
34           }
35           miro.end();
36       }
37       private static void showLetters(SPainter miro, String input) {
38           //READY
39           eraseWhiteBoard(miro);
40           //SET
41           miro.moveTo(new Point.Double(100,100));
42           //GO
43           for (int i= 0; i< input.length(); i= i+1 ) {
44               String letter= input.substring(i, i+1);
45               Color color= getLetterColor(letter);
46               miro.setColor(color);
47               miro.draw(letter);
48               miro.mrt(fontsize);
49           }
50       }
51       private static void eraseWhiteBoard(SPainter miro) {
52           miro.setColor(Color.WHITE);
53           miro.wash();
54           miro.paintFrame(Color.black, 5);
55       }
56   
57       private static Color getLetterColor(String letter) {
58           for (int i= 0; i<letters.length; i= i+1) {
59               if (letter.equalsIgnoreCase(letters[i])) {
60                   return colors[i];
61   
62               }
63           }
64           return Color.BLACK;
65       }
66   
67       private static void initializeColorMap(String specialLetters) {
68           letters= new String[specialLetters.length()];
69           colors= new Color[specialLetters.length()];
70   
71           for (int i= 0; i<specialLetters.length(); i=i+1){
72               letters[i]=specialLetters.substring(i, i+1);
73               colors[i]= randomColor();
74   
75           }
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       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
86       public GraphemeToColorSynesthesia(){
87           paintingCode();
88       }
89   
90       public static void main(String[] args) {
91           SwingUtilities.invokeLater(new Runnable() {
92               public void run() {
93                   new GraphemeToColorSynesthesia();
94   
95               }
96           });
97       }
98   }