This repository has been archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoroscopeApplet.java
134 lines (110 loc) · 3.41 KB
/
HoroscopeApplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//VS4E -- DO NOT REMOVE THIS LINE!
public class HoroscopeApplet extends JApplet implements ActionListener{
private JTextField inputMonth;
private JTextField inputDay;
private JTextArea jtaHoroscope;
private static final long serialVersionUID = 1L;
public void init() {
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
setSize(320, 240);
JPanel container = new JPanel();
GridLayout gLayout = new GridLayout(3,2);
container.setLayout(gLayout);
container.setSize(320, 50);
JLabel lblMonth = new JLabel("Input month of birth");
inputMonth = new JTextField();
inputMonth.setSize(100, 10);
JLabel lblDay = new JLabel("Input day of birth");
inputDay = new JTextField();
JPanel containerBtn = new JPanel();
containerBtn.setLayout(new GridLayout(1,1));
containerBtn.setSize(320, 25);
JButton btn = new JButton("Show horoscope");
btn.setSize(50, 25);
btn.addActionListener(this);
JPanel containerHoroscope = new JPanel();
containerHoroscope.setLayout(new GridLayout(1,2));
jtaHoroscope = new JTextArea();
jtaHoroscope.setLineWrap(true);
jtaHoroscope.setWrapStyleWord(true);
jtaHoroscope.setSize(320, 100);
containerHoroscope.add(jtaHoroscope);
container.add(lblMonth);
container.add(inputMonth);
container.add(lblDay);
container.add(inputDay);
container.add(btn);
getContentPane().setName("Horoscope");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(container);
getContentPane().add(containerHoroscope);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int month = Integer.valueOf(inputMonth.getText());
int day = Integer.valueOf(inputDay.getText());
Signs.initStarSigns();
String horoscope = Signs.getHoroscope(month, day);
horoscope = unescapeXML(horoscope);
jtaHoroscope.setText(horoscope);
}
public static String unescapeXML(String str) {
if (str == null || str.length() == 0)
return "";
StringBuffer buf = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (c == '&') {
int pos = str.indexOf(";", i);
if (pos == -1) { // Really evil
buf.append('&');
} else if (str.charAt(i + 1) == '#') {
int val = Integer.parseInt(str.substring(i + 2, pos), 16);
buf.append((char) val);
i = pos;
} else {
String substr = str.substring(i, pos + 1);
if (substr.equals("&"))
buf.append('&');
else if (substr.equals("<"))
buf.append('<');
else if (substr.equals(">"))
buf.append('>');
else if (substr.equals("""))
buf.append('"');
else if (substr.equals("'"))
buf.append('\'');
else
// ????
buf.append(substr);
i = pos;
}
} else {
buf.append(c);
}
}
return buf.toString();
}
}