/home/rkanin/NetBeansProjects/CS1/src/synesthesia/GraphemeToColorSynesthesia.java |
1
2
3
4
5
6
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
19
20 public class GraphemeToColorSynesthesia {
21 private static final int fontsize = 30;
22 private static final String theLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
23 private static String[] letters;
24 private static Color[] colors;
25
26 private void paintingCode() {
27
28 SPainter miro = new SPainter(1200,220);
29 miro.setScreenLocation(400,30);
30 miro.setFontSize(fontsize);
31 initializeColorMap(theLetters);
32
33
34 while ( true ) {
35 String input = JOptionPane.showInputDialog(null, "Please enter a word, or a few words...");
36 if ( input == null ) {input = "EXIT"; }
37 input = input.toUpperCase();
38 if ( input.equals("EXIT") ) {
39 break;
40 } else if ( input.equals("REMAP") ) {
41 initializeColorMap(theLetters);
42 showLetters(miro, theLetters);
43 } else {
44 showLetters(miro,input.toUpperCase());
45
46 }
47 }
48 miro.end();
49
50
51 }
52 private void showLetters(SPainter miro, String input) {
53
54 eraseWhiteBoard(miro);
55
56 miro.moveTo(new Point.Double(100,100));
57
58 for (int i = 0; i < input.length(); i = i
59 + 1) {
60 String letter = input.substring(i, i + 1);
61 Color color = getLetterColor(letter);
62 miro.setColor(color);
63 miro.draw(letter);
64 miro.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
72 = i + 1) {
73 letters[i] = specialLetters.substring(i, i + 1);
74 colors[i] = randomColor();
75 }
76 }
77
78 private static Color getLetterColor(String letter) {
79 for (int i = 0; i < letters.length; i
80 = i + 1) {
81 if (letter.equalsIgnoreCase(letters[i])) {
82 return colors[i];
83 }
84 }
85 return Color.BLACK;
86 }
87
88 private static Color randomColor() {
89 int rv = (int) (Math.random() * 256);
90 int gv = (int) (Math.random() * 256);
91 int bv = (int) (Math.random() * 256);
92 return new Color(rv, gv, bv);
93 }
94
95 private static void eraseWhiteBoard(SPainter miro) {
96
97 miro.setColor(Color.WHITE);
98 miro.wash();
99 miro.paintFrame(Color.black, 5);
100 }
101
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
116
117
118