GraphemeToColorSynesthesia.java
1    /* 
2    * Program to simulate the phenomenon known as grapheme to color synesthesia. 
3    * This program is written as an interpreter that recognizes and responds to: 
4    * -exit|terminate the program 
5    * -remap| redefine the mapping from letters to colors 
6    * - WORDS OR PHRASE| simply show the word or phrase in synethetic color 
7     */
8    
9    
10   package Synesthesia;
11   
12   import jdk.swing.interop.SwingInterOpUtils;
13   import painter.SPainter;
14   
15   import javax.swing.*;
16   import java.awt.*;
17   import java.util.Random;
18   
19   public class GraphemeToColorSynesthesia {
20       private static final int fontsize= 30;
21       private static final String theLetters= "ABCEDFGHIJKLMNOPQRSTUVWXYZ";
22       private static String[] letters;
23       private static Color[] colors;
24   
25       private void paintingCode(){
26           // INITIALIZATION
27           SPainter miro= new SPainter("Colorful", 1200, 200);
28           miro.setScreenLocation(30,30);
29           miro.setFontSize(fontsize);
30           initializeColorMap(theLetters);
31   
32           //INTERPRETATION
33           while (true) {
34               String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words...");
35               if (input == null) { input = "EXIT"; }
36               input = input.toUpperCase();
37               if (input.equals("EXIT")) {
38                   break;
39               } else if (input.equals("REMAP")) {
40               initializeColorMap(theLetters);
41               showLetters(miro,theLetters);
42               } else {
43                   showLetters(miro, input.toUpperCase());
44               }
45           }
46           miro.end();
47       }
48       private static void showLetters(SPainter miro, String input) {
49           //READY
50           eraseWhiteBoard(miro);
51           //SET
52           miro.moveTo(new Point.Double(100,100));
53           //GO
54           for (int i= 0; i< input.length(); i= i+1 ) {
55               String letter= input.substring(i, i+1);
56               Color color= getLetterColor(letter);
57               miro.setColor(color);
58               miro.draw(letter);
59               miro.mrt(fontsize);
60           }
61       }
62       private static void eraseWhiteBoard(SPainter miro) {
63           miro.setColor(Color.WHITE);
64           miro.wash();
65           miro.paintFrame(Color.black, 5);
66       }
67   
68       private static Color getLetterColor(String letter) {
69           for (int i= 0; i<letters.length; i= i+1) {
70               if (letter.equalsIgnoreCase(letters[i])) {
71                   return colors[i];
72   
73               }
74           }
75           return Color.BLACK;
76       }
77   
78       private static void initializeColorMap(String specialLetters) {
79           letters= new String[specialLetters.length()];
80           colors= new Color[specialLetters.length()];
81   
82           for (int i= 0; i<specialLetters.length(); i=i+1){
83               letters[i]=specialLetters.substring(i, i+1);
84               colors[i]= randomColor();
85   
86               }
87           }
88   
89       private static Color randomColor() {
90           int rv = (int) (Math.random() * 256);
91           int gv = (int) (Math.random() * 256);
92           int bv = (int) (Math.random() * 256);
93           return new Color(rv, gv, bv);
94   
95       }
96       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
97       public GraphemeToColorSynesthesia(){
98           paintingCode();
99       }
100  
101      public static void main(String[] args) {
102          SwingUtilities.invokeLater(new Runnable() {
103              public void run() {
104                  new GraphemeToColorSynesthesia();
105  
106              }
107          });
108      }
109  }
110  
111