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