GraphemeToColor.java
1    package chromesthesia;
2    /* 
3     * Program to simulate the phenomenon known as grapheme to color synesthesia. 
4     * This program is written as an interpreter that recognizes and responds to: 
5     * - exit | terminate the program 
6     * - remap | redefine the mapping from letters to colors 
7     * - WORD OR PHRASE | simply show the word or phrase in synesthetic color 
8     */
9    import java.awt.Color;
10   import java.awt.Point;
11   import javax.swing.JOptionPane;
12   import javax.swing.SwingUtilities;
13   import painter.SPainter;
14   public class GraphemeToColor {
15       //Setting Private Variables
16       private static final int fontSize = 30; //fontsize is set to 30
17       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //letters which color is changed for
18       private static String[] letters; //establishing String Array letters[]
19       private static Color[] colors; //establishing Color Array colors[]
20   
21       private void paintingCode() {
22   
23           //Initialization...
24           SPainter leo = new SPainter(1200,220); //setting canvas name leo.
25           leo.setScreenLocation(30,30); //sets screen location.
26           leo.setFontSize(fontSize); //applies font size to leo.
27           initializeColorMap(theLetters); //code for this will be shown below, that code is told to act here.
28   
29           //Interpretation
30           while (true) {
31               String input = JOptionPane.showInputDialog(null,"Please enter a few words: ");
32               if (input == null) {//if input is null set input to EXIT
33                   input = "EXIT";
34               }
35               input = input.toUpperCase();
36               if (input.equals("EXIT")) {//if input is EXIT... exit program
37                   break;//ends here
38               } else if (input.equals("REMAP")) {//will show which letters changed colors and their colors
39                   initializeColorMap(theLetters); //theLetters are the ones which will be used in the code to change color.
40                   showLetters(leo,theLetters); //this uses painter which paints and paints basic rep
41               } else {
42                   showLetters(leo,input.toUpperCase()); //This will show words with colors.
43               }
44           }
45           leo.end(); //ends painter
46       }
47   
48       private void showLetters(SPainter leo, String input) {//set earlier in the code
49           //READY!
50           eraseWhiteBoard(leo);//erase
51           //SET~~~
52           leo.moveTo(new Point.Double(100,100));
53           //Go...
54           for (int i = 0; i < input.length(); i = i + 1) { //will do this till i is more then input.length();
55               String letter = input.substring(i,i+1); //sets String which is equal to the letter from i to i+1, so one letter. //changes as i changes.
56               Color color = getLetterColor(letter); //grabs correct color of letter, later in program
57               leo.setColor(color); //sets correct color
58               leo.draw(letter); //draws correct letter.
59               leo.mrt(fontSize); //sets letter to fontSize 30
60           }
61       }
62   
63   
64       private void initializeColorMap(String theLetters) {
65           letters = new String[theLetters.length()]; //sets letters to [theLetters.length()] which means it will space correctly I think.
66           colors = new Color[theLetters.length()]; //sets colors to Color at [theLetters.length()]
67           for (int i = 0; i < theLetters.length(); i = i + 1) { //normal for loop... I mean more common one I know of
68               letters[i] = theLetters.substring(i,i+1); //setting letters to correct letter in the word.
69               colors[i] = randomColor(); //picking a color for the part of the substring in which a certain letter lies.
70           }//goes to randomColor code
71       }
72       private Color getLetterColor(String letter) {
73           for (int i = 0; i < letters.length; i = i + 1) {
74               if (letter.equalsIgnoreCase(letters[i])) {
75                   return colors[i]; //returns color if it is one of the letters to be colored
76               }
77           }
78           return Color.BLACK; //returns black if it isn't in theLetters
79       }
80   
81       private Color randomColor() { //Grabing a random color.
82           int rv = (int)(Math.random()*256); //rolling a number
83           int gv = (int)(Math.random()*256); //rolling a number
84           int bv = (int)(Math.random()*256); //rolling a number
85           return new Color(rv,gv,bv); //putting numbers to make color code.
86       }
87   
88       private void eraseWhiteBoard(SPainter leo) {//this is how text is erased
89           leo.setColor(Color.WHITE);
90           leo.wash();
91           leo.paintFrame(Color.BLACK,5);
92       }
93       //Painting Infrastructure
94       public GraphemeToColor() {
95           paintingCode();
96       } //structure needed to run painter.
97       public static void main (String[] args) {
98           SwingUtilities.invokeLater(new Runnable() {
99               public void run() {
100                  new GraphemeToColor();
101              }
102          });
103      }
104  
105  
106  }
107