import java.util.*;
import java.awt.*;
import java.awt.event.*;
class AdvancedGame extends Frame {
Vector words = new Vector();
String[] data = { "태연", "유리", "윤아", "효연", "수영", "서현", "티파니", "써니", "제시카" };
int interval = 1 * 1200;
// 2초
TextArea ta;
TextField tf;
WordGenerator wd;
// WordGenerator wg = new WordGenerator();
AdvancedGame() {
super("단어 지우기 게임");
ta = new TextArea();
tf = new TextField();
wd = new WordGenerator();
add(ta, "Center");
add(tf, "South");
ta.setEditable(false);
tf.addActionListener(new ActionHandler());
setSize(300, 200);
setVisible(true);
tf.requestFocus();
wd.start();
this.addWindowListener(new WindowHandler());
}
public void delay(int millis) {
try {
Thread.sleep(millis);
}
catch (Exception e) {
}
}
public String display(Vector v) {
Iterator il = v.listIterator();
int count = 0;
String ret = "";
while (il.hasNext()) {
if (count < 5) {
ret += il.next() + " ";
count++;
}
else {
ret += "\n" + il.next() + " ";
count = 0;
}
}
return ret;
}
class WordGenerator extends Thread {
public void run() {
while (true) {
int max = data.length;
int val = (int) (Math.random() * max);
words.add(data[val]);
delay(interval);
// System.out.println(words.toString());
int count = 0;
ta.setText(display(words));
}
/*
* WordGenerator는 일정시간(interval) 간격으로 data배열의 한 요소를 골라서
* words(Vector인스턴스)에 저장하는 일을 수행한다.
*
* 다음의 코드를 완성하세요. 1. 문자열 배열 data의 임의의 요소를 골라서 words(Vector인스턴스)에
* 저장한다. 2. 인스턴스 변수 interval의 값만큼 시간간격을 둔다. 3. 반복문을 이용해서 1,2의 작업이
* 반복적으로 이루어지게 한다.
*/
}
// end of run()
}
// class WordGenerator
class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// System.out.println("Enter");
// System.out.println(tf);
// System.out.println(tf.getText());
if (words.contains(tf.getText())) {
System.out.println(tf.getText());
words.remove(tf.getText());
ta.setText(display(words));
tf.setText("");
tf.requestFocus();
}
tf.setText("");
tf.requestFocus();
}
}
// Action Handler
class WindowHandler implements WindowListener {
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
public static void main(String args[]) {
AdvancedGame game = new AdvancedGame();
// Vector words = game.words;
// game.wg.start();
// while(true) {
// System.out.println(words);
// String prompt = ">>";
// System.out.print(prompt);
// 화면으로부터 라인단위로 입력받는다.
// Scanner s = new Scanner(System.in);
// String input = s.nextLine().trim();
/*
* 다음의 코드를 완성하세요. 1. 사용자가 입력한 값을 words에서 찾아 제거한다.
*/
// }
}
// main
}
// TypingGameEx1