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