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