ToColor.java
1    package Synesthesia;
2    
3    import java.awt.Color;
4    import java.awt.Point;
5    import javax.swing.JOptionPane;
6    import javax.swing.SwingUtilities;
7    import painter.SPainter;
8    
9    
10   public class ToColor {
11   
12       private static final int fontsize = 30;
13       private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14       private static String[] letters;
15       private static Color[] colors;
16       private void paintingCode() {
17   
18   // INITIALIZATION
19           SPainter miro = new SPainter(1200,220);
20           miro.setScreenLocation(30, 30);
21           miro.setFontSize(fontsize);
22           initializeColorMap(theLetters);
23   
24   // INTERPRETATION
25           while ( true ) {
26               String input = JOptionPane.showInputDialog(null,
27                       "Please enter a word, or a few words ...");
28               if ( input == null ) { input = "EXIT"; }
29               input = input.toUpperCase();
30               if ( input.equals("EXIT") ) {
31                   break;
32               } else if ( input.equals("REMAP") ) {
33   
34                   initializeColorMap(theLetters);
35                   showLetters(miro,theLetters);
36               } else {
37                   showLetters(miro,input.toUpperCase());
38               }
39           }
40           miro.end();
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       private static void initializeColorMap(String specialLetters) {
57           letters = new String[specialLetters.length()];
58           colors = new Color[specialLetters.length()];
59           for ( int i = 0; i < specialLetters.length(); i = i + 1) {
60               letters[i] = specialLetters.substring(i,i+1);
61               colors[i] = randomColor();
62           }
63       }
64       private static Color getLetterColor(String letter) {
65           for ( int i = 0; i < letters.length; i =i + 1 ) {
66               if ( letter.equalsIgnoreCase(letters[i]) ) {
67                   return colors[i];
68               }
69           }
70           return Color.BLACK;
71       }
72       private static Color randomColor() {
73           int rv = (int)(Math.random()*256);
74           int gv = (int)(Math.random()*256);
75           int bv = (int)(Math.random()*256);
76           return new Color(rv,gv,bv);
77       }
78       private static void eraseWhiteBoard(SPainter miro) {
79           miro.setColor(Color.WHITE);
80           miro.wash();
81           miro.paintFrame(Color.black, 5);
82       }
83   
84       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
85       public ToColor() {
86           paintingCode();
87       }
88       public static void main(String[] args) {
89           SwingUtilities.invokeLater(new Runnable() {
90               public void run() {
91                   new ToColor();
92               }
93           });
94       }
95   }
96   
97