/home/kchan2/NetBeansProjects/CS1/src/synesthesia/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  * - WORD OR PHRASE | simply show the word or phrase in synesthetic color
  7  */
  8 
  9 package synesthesia;
 10 
 11 import java.awt.Color;
 12 import java.awt.Point;
 13 import javax.swing.JOptionPane;
 14 import javax.swing.SwingUtilities;
 15 import painter.SPainter;
 16 
 17 /**
 18  *
 19  * @author kchan2
 20  */
 21 public class GraphemeToColorSynesthesia {
 22 
 23     private static final int fontsize = 30;
 24     private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 25     private static String[] letters;
 26     private static Color[] colors;
 27     
 28     private void paintingCode() {
 29         
 30         // INITIALIZATION
 31         
 32         SPainter miro = new SPainter(1200,220);
 33         miro.setScreenLocation(30,30);
 34         miro.setFontSize(fontsize);
 35         initializeColorMap(theLetters);
 36         
 37         // INTERPRETATION
 38         while ( true ) {
 39             String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words ...");
 40             if ( input == null ) { input = "EXIT"; }
 41             input = input.toUpperCase();
 42             if ( input.equals("EXIT") ) {
 43                 break;
 44             } else if ( input.equals("REMAP") ) {
 45                 initializeColorMap(theLetters);
 46                 showLetters(miro,input.toUpperCase());
 47             } else {
 48                 showLetters(miro,input.toUpperCase());
 49             }
 50             
 51         }
 52         miro.end();
 53         
 54     }
 55     
 56     private void showLetters(SPainter miro, String input) {
 57         // READY
 58         eraseWhiteBoard(miro);
 59         // SET
 60         miro.moveTo(new Point.Double(100,100));
 61         // GO
 62         for ( int i = 0; i < input.length(); i = i + 1 ) {
 63             String letter = input.substring(i, i+1);
 64             Color color = getLetterColor(letter);
 65             miro.setColor(color);
 66             miro.draw(letter);
 67             miro.mrt(fontsize);
 68         }
 69     }
 70     
 71     private void initializeColorMap(String specialLetters) {
 72         letters = new String[specialLetters.length()];
 73         colors = new Color[specialLetters.length()];
 74         for ( int i = 0; i < letters.length; i = i+1 ) {
 75             letters[i] = specialLetters.substring(i, i+1);
 76             colors[i] = randomColor();
 77         }
 78     }
 79 
 80     private Color getLetterColor(String letter) {
 81         for ( int i = 0; i < letters.length; i = i+1 ) {
 82             if ( letter.equalsIgnoreCase(letters[i]) ) {
 83                 return colors[i];
 84             }
 85         }
 86         return Color.BLACK;
 87     }
 88     
 89     private 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     private void eraseWhiteBoard(SPainter miro) {
 97         miro.setColor(Color.WHITE);
 98         miro.wash();
 99         miro.paintFrame(Color.black, 5);
100     }
101     // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
102     
103     public GraphemeToColorSynesthesia() {
104         paintingCode();
105     }
106     
107     public static void main(String[] args) {
108         SwingUtilities.invokeLater(new Runnable() {
109             public void run() {
110                 new GraphemeToColorSynesthesia();
111             }
112         });
113     }
114 }
115