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