/*
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame {
Container c = getContentPane();
public Main() {
setTitle("게임");
c.setLayout(null);
c.addMouseListener(new MyMouseListener());
setSize(300, 200);
setVisible(true);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
BubbleThread th = new BubbleThread(x, y);
th.start();
}
}
class BubbleThread extends Thread {
private int x, y;
public BubbleThread(int x, int y) {
this.x = x;
this.y = y;
}
public void run() {
// 라벨 만들어서 컨텐트팬에 추가
ImageIcon img = new ImageIcon("ball.png");
JLabel la = new JLabel(img);
c.add(la);
la.setSize(50, 50);
la.setLocation(x, y);
while (true) {
// 20ms마다 5씩 위로 옮기기
try {
sleep(20);
} catch (Exception e) {
// TODO: handle exception
}
int y1 = la.getY();
int x1 = la.getX();
if(y1<=0)
{
c.remove(la);
c.repaint();
return;
}
la.setLocation(x1, y1 - 5);
// 만약, 컨탠트팬에서 벗어났다면 삭제하고, 스레드 종료 return;
}
}
}
public static void main(String[] args) {
new Main();
}
}*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
// 숫자라벨 크기, 색상 설정 (Font)
// 상태 라벨 선언, 추가, 설정
public class Main extends JFrame {
Container c = getContentPane();
JLabel la[] = new JLabel[3];
JLabel lar = new JLabel("마우스를 클릭할 때 게임이 시작됨니다 ");
GamblingTread th = new GamblingTread();
public Main() {
setTitle("게임");
c.setLayout(null);
c.addMouseListener(new MyMouseListener());
for(int i=0;i<3;i++) {
la[i] = new JLabel("0");
la[i].setSize(60,50);
la[i].setOpaque(true);
la[i].setBackground(Color.MAGENTA);
la[i].setHorizontalAlignment(JLabel.CENTER);
}
c.add(la[0]);
c.add(la[1]);
c.add(la[2]);
la[0].setLocation(20, 50);
la[1].setLocation(120,50);
la[2].setLocation(220,50);
c.add(lar);
lar.setLocation(50, 120);
lar.setSize(300, 50);
th.start();
setSize(300, 200);
setVisible(true);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
th.startGamBling();
}
}
class GamblingTread extends Thread {
boolean gambling = false;
synchronized public void waitForGambling() {
if (!gambling)
try {
this.wait();
} catch (InterruptedException e) {
return;
}
}
synchronized public void startGamBling() {
gambling = true;
this.notify();
}
public void run() {
while (true) {
waitForGambling();
int a=0, b=0, c=0;
for(int i=0;i<3;i++) {
int x = (int)(Math.random()*5);
if(i==0) a=x;
else if(i==1) b=x;
else c=x;
la[i].setText(Integer.toString(x));
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(a==b&&b==c) {
lar.setText("축하합니다");
//System.out.println();
}
else {
lar.setText("아쉽군요");
//System.out.println
}
gambling = false;
}
}
}
public static void main(String[] args) {
new Main();
}
}



