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