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