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