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