mojo's Blog
명품 JAVA Programming 제 13장 실습문제 본문
2번 실습 문제 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private int operation = 0;
private MyPanel myPanel;
class MyPanel extends JPanel implements Runnable{
private int mouseOperation=0;
public void paintComponent(Graphics g) {
if(mouseOperation != 0) {
super.paintComponent(g);
int x = (int)(Math.random()*(this.getWidth()-50));
int y = (int)(Math.random()*(this.getHeight()-50));
g.setColor(Color.RED);
g.drawOval(x, y, 50, 50);
}
}
public void run() {
mouseOperation = 1;
while(true) {
repaint();
try {
Thread.sleep(400);
} catch(InterruptedException e) { return; }
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel = new MyPanel();
setContentPane(myPanel);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(operation == 0) {
operation = 1;
Thread th = new Thread(myPanel);
th.start();
}
}
});
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 3번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private TimeLabel tl;
class TimeLabel extends JLabel implements Runnable{
Calendar c;
TimeLabel(){
setFont(new Font("Gothic", Font.ITALIC, 60));
}
public void run() {
while(true) {
c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
setText(hour+":"+min+":"+second);
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 50));
tl = new TimeLabel();
c.add(tl);
Thread th = new Thread(tl);
setSize(400, 300);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 4번 - (1) code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private VibrateContentPane vcp;
class VibrateContentPane extends JFrame implements Runnable{
JFrame jf;
VibrateContentPane(JFrame jf){
this.jf = jf;
}
public void run() {
int op = 0;
while(true) {
if(op++ == 0) {
jf.setLocation(200,200);
}
else if(op++ == 1) {
jf.setLocation(220,220);
}
else if(op++ == 2) {
jf.setLocation(200,200);
}
else {
op = 0;
jf.setLocation(180,180);
}
try {
Thread.sleep(30);
}
catch(InterruptedException e) {
return;
}
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
vcp = new VibrateContentPane(this);
Thread th = new Thread(vcp);
setSize(400, 300);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 5번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
class Game extends JPanel{
private Bullet bt;
private Target tg;
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
private int bulX, bulY, tgX, tgY, mapX, start=0, bulStart=0;
private Thread tgTh, btTh;
class Bullet implements Runnable{
public void run() {
int tmpY = bulY;
while(true) {
if(bulStart == 1) {
bulY -= 5;
if(bulY < 0) {
bulStart = 0;
bulY = tmpY;
}
if(bulX >= tgX && bulX <= tgX+100 && bulY >=0 && bulY <=100) {
bulStart = 0;
bulY = tmpY;
}
}
try {
Thread.sleep(50);
} catch(InterruptedException e) { return; }
}
}
}
class Target implements Runnable{
public void run() {
int direction = -1;
while(true) {
if(direction == 1) {
if(tgX + 105 <= mapX) tgX += 5;
else {
direction = -1;
tgX -= 5;
}
}
else if(direction == -1){
if(tgX - 5 >= 0) tgX -= 5;
else {
direction = 1;
tgX += 5;
}
}
try {
Thread.sleep(100);
} catch(InterruptedException e) { return; }
}
}
}
Game(){
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyValue = e.getKeyCode();
if(keyValue == KeyEvent.VK_ENTER) {
bulStart = 1;
}
}
});
setFocusable(true);
requestFocus();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(start++ == 0) {
bulX = this.getWidth()/2 - 10; bulY = this.getHeight() - 70;
tgX = this.getWidth()/2 - 50;
mapX = this.getWidth();
tg = new Target();
tgTh = new Thread(tg);
tgTh.start();
bt = new Bullet();
btTh = new Thread(bt);
btTh.start();
}
g.setColor(Color.BLACK);
g.fillRect((this.getWidth()/2 - 25), (this.getHeight() - 50),50,50);
g.setColor(Color.RED);
g.fillRect((this.getWidth()/2 - 10), bulY, 20, 20);
g.drawImage(img, tgX, 0, 100, 100, this);
}
}
public class Practice extends JFrame{
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new Game());
setSize(400, 600);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 6번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
class BubbleLabel extends JPanel implements Runnable{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
private Vector<Point> v = new Vector<Point>();
BubbleLabel(){
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
v.add(p);
}
});
setFocusable(true);
requestFocus();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(v.size()==0) {
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
else {
for(int i=0; i<v.size(); i++) {
int x = v.get(i).x;
int y = v.get(i).y;
if(y <= -75) {
v.remove(i);
i--;
continue;
}
g.drawImage(img, x, y, 75, 75, this);
v.set(i, new Point(x, y-5));
}
}
}
synchronized public void run() {
while(true) {
try {
Thread.sleep(200);
}
catch(InterruptedException e) {
return;
}
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BubbleLabel bl = new BubbleLabel();
setContentPane(bl);
Thread th = new Thread(bl);
setSize(400, 600);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 7번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private MyPanel panel = new MyPanel();
private int direction = 0; // right : 0, down : 1, left : 2, up : 3
class MyPanel extends JPanel implements Runnable{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
private ImageIcon icon1 = new ImageIcon("C:\\\\Temp/apple.jpg");
private Image img1 = icon1.getImage();
private ImageIcon icon2 = new ImageIcon("C:\\\\Temp/kitty.png");
private Image img2 = icon2.getImage();
private Vector<Point> v = new Vector<Point>();
MyPanel(){
for(int i=0; i<10; i++) {
v.add(new Point(100+20*i,300));
}
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_UP) {
direction = 3;
}
else if(key==KeyEvent.VK_DOWN) {
direction = 1;
}
else if(key==KeyEvent.VK_RIGHT) {
direction = 0;
}
else if(key==KeyEvent.VK_LEFT) {
direction = 2;
}
}
});
setFocusable(true);
requestFocus();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
for(int i=0;i<v.size();i++) {
if(i!=v.size()-1) {
int x = v.get(i).x, y=v.get(i).y;
g.drawImage(img2, x, y, 20, 20, this);
}
else {
int x = v.get(i).x, y=v.get(i).y;
g.drawImage(img1, x, y, 20, 20, this);
}
}
}
public void run() {
while(true) {
if(direction == 0 || direction == 2) {
int x = v.get(v.size()-1).x , y = v.get(v.size()-1).y;
v.set(v.size() - 1, new Point(x + 20 * (direction==0 ? 1 : -1), y));
for(int i=v.size()-2; i>=0; i--) {
int x1 = v.get(i).x, y1 = v.get(i).y;
v.set(i, new Point(x, y));
x = x1; y = y1;
}
}
else {
int x = v.get(v.size()-1).x , y = v.get(v.size()-1).y;
v.set(v.size() - 1, new Point(x, y + 20 * (direction==1 ? 1 : -1)));
for(int i=v.size()-2; i>=0; i--) {
int x1 = v.get(i).x, y1 = v.get(i).y;
v.set(i, new Point(x, y));
x = x1; y = y1;
}
}
try {
Thread.sleep(200);
}
catch(InterruptedException e) {
return;
}
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
Thread th = new Thread(panel);
setSize(400, 400);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 8번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private MyPanel panel = new MyPanel();
private int direction = 0; // right : 0, down : 1, left : 2, up : 3
class MyPanel extends JPanel implements Runnable{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
private ImageIcon icon1 = new ImageIcon("C:\\\\Temp/kitty.png");
private Image img1 = icon1.getImage();
private Vector<Point> v = new Vector<Point>();
MyPanel(){
System.out.println(getWidth()+" "+getHeight());
for(int i=0;i<50;i++) {
int x = (int)(Math.random() * 400);
int y = (int)(Math.random() * 400);
v.add(new Point(x,y));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
for(int i=0; i<v.size(); i++) {
int x = v.get(i).x, y = v.get(i).y;
g.drawImage(img1, x, y, 20, 20, this);
}
}
public void run() {
while(true) {
for(int i=0; i<v.size(); i++) {
int x = v.get(i).x, y = v.get(i).y;
if(y + 30 > getHeight()) {
x = (int)(Math.random()*getWidth());
y = 0;
v.set(i, new Point(x,y));
}
else {
v.set(i, new Point(x, y+20));
}
}
try {
Thread.sleep(50);
}
catch(InterruptedException e) {
return;
}
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
Thread th = new Thread(panel);
setSize(400, 400);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 9번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private JLabel la[] = new JLabel[3];
private JLabel gameLa = new JLabel("마우스를 클릭할 때 마다 게임합니다.");
private GamblingRunnable gr = new GamblingRunnable();
private int op = 0;
class GamblingRunnable implements Runnable{
boolean gambling = false;
synchronized public void waitForGambling() {
if(!gambling) {
try { wait(); }
catch(InterruptedException e) { return; }
}
}
synchronized public void startGambling() {
gambling = true;
notify();
}
public void game() {
try {
int gameOp=1, before=-1;
for(int i=0; i<la.length; i++) {
int r = (int)(Math.random()*5);
if(before == r) gameOp++;
before = r;
la[i].setText(" " + r + " ");
Thread.sleep(200);
}
if(gameOp == 3) gameLa.setText("축하합니다!!");
else gameLa.setText("아쉽군요");
} catch(InterruptedException e) { return; }
}
public void run() {
while(true) {
waitForGambling();
game();
gambling = false;
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
for(int i=0; i<la.length; i++) {
la[i] = new JLabel(" 0 ");
la[i].setFont(new Font("Jokerman", Font.ITALIC, 30));
la[i].setForeground(Color.YELLOW);
la[i].setOpaque(true);
la[i].setBackground(Color.PINK);
c.add(la[i]);
}
c.add(gameLa);
Thread th = new Thread(gr);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
gr.startGambling();
}
});
setSize(300, 180);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 10번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private MyPanel panel = new MyPanel();
class MyPanel extends JPanel implements Runnable{
private boolean op = false;
private int x=0, y=0;
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
MyPanel(){
addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
x = e.getX(); y = e.getY();
draw();
}
public void mouseExited(MouseEvent e) {
op = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
x = e.getX(); y = e.getY();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(!op) g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
else {
g.setColor(Color.MAGENTA);
int width = (int)(Math.random()*(getWidth()-x));
int height = (int)(Math.random()*(getHeight()-y));
g.fillOval(x, y, width, height);
}
}
synchronized public void stop() {
if(!op) {
try {
wait();
} catch(InterruptedException e) { return; }
}
}
synchronized public void draw() {
op = true;
notify();
}
public void run() {
while(true) {
stop();
try {
Thread.sleep(300);
} catch(InterruptedException e) { return; }
}
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
Thread th = new Thread(panel);
setSize(600, 600);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
실습문제 11번 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;
public class Practice extends JFrame{
private NorthPanel np = new NorthPanel();
private SouthPanel sp = new SouthPanel();
private CenterPanel cp = new CenterPanel();
class NorthPanel extends JPanel{
private JLabel la = new JLabel("산성비 게임을 시작하려면 <Enter> 키를 누르세요");
NorthPanel(){
setLayout(new FlowLayout(FlowLayout.LEFT));
la.setFont(new Font("Jokerman", Font.BOLD, 15));
add(la);
}
public void setLabel(String s) {
la.setText(s);
}
}
class CenterPanel extends JPanel implements Runnable{
private Vector<String> v = new Vector<String>();
private boolean game = false;
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
private int x, y, r;
public void setWord() {
v.add("apple"); v.add("public"); v.add("cuttlefish");
v.add("kakao"); v.add("java"); v.add("success");
v.add("pink"); v.add("yellow"); v.add("environment");
v.add("orange"); v.add("envelopment"); v.add("smart");
v.add("sogang"); v.add("university"); v.add("game");
v.add("rain"); v.add("notebook"); v.add("coffee");
}
public void resetWord() {
r = (int)(Math.random()*v.size());
y = 0;
}
public String getWord() {
return v.get(r);
}
CenterPanel(){
setWord();
r = (int)(Math.random()*v.size());
x = 100;
y = 0;
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = (int)(e.getKeyCode());
if(keyCode == KeyEvent.VK_ENTER) {
gameStart();
}
}
});
setFocusable(true);
requestFocus();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(!game) g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
else {
y += 5;
g.setColor(Color.MAGENTA);
g.setFont(new Font("Arial",Font.ITALIC,30));
g.drawString(v.get(r), x, y);
}
}
synchronized public void gameStop() {
if(!game) {
try {
wait();
} catch(InterruptedException e) { return; }
}
}
synchronized public void gameStart() {
game = true;
notify();
}
public void run() {
while(true) {
gameStop();
if(y > getHeight()) {
resetWord();
np.setLabel("시간초과실패");
}
try {
Thread.sleep(50);
} catch(InterruptedException e) { return; }
}
}
}
class SouthPanel extends JPanel{
private JTextField tf = new JTextField("", 20);
SouthPanel(){
setBackground(Color.GRAY);
add(tf);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String word = cp.getWord();
String myWord = tf.getText();
if(myWord.equals("그만")) System.exit(0);
if(myWord.equals(word)) {
cp.resetWord();
np.setLabel("성공");
}
tf.setText("");
}
});
}
}
Practice(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(np, BorderLayout.NORTH);
c.add(sp, BorderLayout.SOUTH);
c.add(cp, BorderLayout.CENTER);
Thread th = new Thread(cp);
setSize(400, 400);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice();
}
}
'Java' 카테고리의 다른 글
팝업 다이얼로그 / 탭팬 (0) | 2021.08.18 |
---|---|
고급 스윙 컴포넌트(메뉴, 툴바, 툴팁, 다이얼로그) (0) | 2021.08.18 |
명품 JAVA Programming 제 13장 Open Challenge (0) | 2021.08.09 |
wait(), notify(), notifyAll() 을 이용하여 스레드를 동기화하기 (0) | 2021.08.09 |
Thread Synchronization (0) | 2021.08.09 |
Comments