/*
import java.awt.*;
import javax.swing.*;
class TimerThread extends Thread {
private JLabel tl;
public TimerThread(JLabel tl) {
this.tl = tl;
}
public void run() {
int n=0;
while(true) {
tl.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main() {
setTitle("스레드 상속받은 타이머 스레드 예제");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel tl = new JLabel();
tl.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(tl);
TimerThread th = new TimerThread(tl);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
private JLabel tl;
public TimerRunnable(JLabel tl) {
this.tl = tl;
}
public void run() {
int n=0;
while(true) {
tl.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main() {
setTitle("스레드 상속받은 타이머 스레드 예제");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel tl = new JLabel();
c.add(tl);
TimerRunnable r = new TimerRunnable(tl);
Thread th = new Thread(r);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable {
private long delay;
public FlickeringLabel(String text,long delay) {
super(text);
this.delay = delay;
setOpaque(true);
Thread th = new Thread(this);
th.start();
}
public void run() {
int n=0;
while(true) {
if(n==0)
setBackground(Color.yellow);
else
setBackground(Color.GREEN);
if(n==0) n=1;
else n=0;
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main() {
setTitle("깜박깜박깜박");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
FlickeringLabel fl = new FlickeringLabel("깜박",1);
JLabel l = new JLabel("안깜박");
FlickeringLabel fl2 = new FlickeringLabel("여기도 깜박",1);
c.add(fl);
c.add(l);
c.add(fl2);
setSize(300,150);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
*/
//import java.awt.*;
//import javax.swing.*;
//import java.util.*;
//
//class TimerRunnable implements Runnable {
// public void run() {
// int n=0;
// while(n<10) {
// n++;
// System.out.println(n+" ");
// try {
// Thread.sleep(1000);
// }
// catch (InterruptedException e) {
// return;
// }
// }
// System.out.println("스레드 종료");
// }
//}
//public class Main extends JFrame {
// public Main() {
// System.out.println("아무 키나 입력 : ");
// Scanner sc = new Scanner(System.in);
// int a = sc.nextInt();
// System.out.println("스레드 실행 시작");
// TimerRunnable r = new TimerRunnable();
// Thread th = new Thread(r);
// th.start();
//
// }
// public static void main(String[] args) {
// new Main();
// }
//}
//import java.awt.*;
//import java.util.Calendar;
//import javax.swing.*;
//
//public class Main extends JFrame {
// JLabel tl = new JLabel();
// Container c1 = getContentPane();
// class TimerThread extends Thread {
// public void run() {
//
// while (true) {
// try {
// Calendar c = Calendar.getInstance();
// int h = c.get(Calendar.HOUR_OF_DAY);
// int m = c.get(Calendar.MINUTE);
// int s = c.get(Calendar.SECOND);
// tl.setText(h + ":" + m + ":" + s);
// c1.repaint();
// c1.revalidate();
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// return;
// }
// }
// }
// }
// public Main() {
// setTitle("디지럴 클러엌 메잌잉");
// setDefaultCloseOperation(EXIT_ON_CLOSE);
// c1.setLayout(new FlowLayout());
// c1.add(tl);
// tl.setFont(new Font("Gothic",Font.ITALIC,80));
// TimerThread th = new TimerThread();
// th.start();
// setSize(400, 400);
// setVisible(true);
// }
//
// public static void main(String[] args) {
// new Main();
// }
//}
/*
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class TimerRunnable extends Thread {
private JLabel tl;
public TimerRunnable(JLabel tl) {
this.tl = tl;
}
public void run() {
int n=0;
while(true) {
tl.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
private Thread th;
public Main() {
setTitle("빼애애애애애애애애애애애애애애애에ㅔ애애애애애애ㅐ액!!!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel tl = new JLabel();
tl.setFont(new Font("Gothic",Font.ITALIC,80));
TimerRunnable r = new TimerRunnable(tl);
th = new Thread(r);
c.add(tl);
JButton b = new JButton("킬 타이멀");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
th.interrupt();
JButton b = (JButton)e.getSource();
b.setEnabled(false);
}
});
c.add(b);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RT extends Thread {
private Container c;
private boolean flag = false;
public RT(Container c) {
this.c = c;
}
public void finish() {
flag = true;
}
public void run() {
while(true) {
int x = ((int)(Math.random()*c.getWidth()));
int y = ((int)(Math.random()*c.getHeight()));
JLabel l = new JLabel("$");
l.setSize(80,30);
l.setLocation(x,y);
c.add(l);
c.repaint();
try {
Thread.sleep(1);
if(flag == true) {
c.removeAll();
l = new JLabel("finish");
l.setSize(80,30);
l.setLocation(100,100);
l.setForeground(Color.RED);
c.add(l);
c.repaint();
return;
}
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
private RT th;
public Main() {
setTitle("슈슉");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
th.finish();
}
});
setSize(2000,1100);
setVisible(true);
th = new RT(c);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Main extends JFrame {
class BT extends Thread {
private Container c;
private int x, y;
ImageIcon img = new ImageIcon("전투기.png");
JLabel l = new JLabel(img);
public BT(Container c, int x, int y) {
this.c = c;
this.x = x;
this.y = y;
}
public void run() {
c.add(l);
l.setSize(80,80);
l.setLocation(100,100);
while(true) {
x++; y--;
if(x>1000 && y<1000) {
remove(l);
}
try {
Thread.sleep(1);
l.setLocation(x,y);
}
catch(InterruptedException e) {
return;
}
}
}
}
public Main() {
setTitle("피융");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
BT th = new BT(c,e.getX(),e.getY());
th.start();
}
});
setSize(1000,1000);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
public class Main {
public static void main(String[] args) {
SharedBoard b = new SharedBoard();
Thread th1 = new StudentThread("준우",b);
Thread th2 = new StudentThread("연우",b);
th1.start();
th2.start();
}
}
class SharedBoard {
private int sum = 0;
public void add() {
int n = sum;
Thread.yield();
n += 10;
sum = n;
sum = n;
System.out.println(Thread.currentThread().getName() + " : " + sum);
}
public int getSum() {return sum;}
}
class StudentThread extends Thread {
private SharedBoard b;
public StudentThread(String name,SharedBoard b) {
super(name);
this.b = b;
}
public void run() {
for(int i=0; i<10; i++) {
b.add();
}
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyLabel extends JLabel {
private int bs = 0; // barSize
private int mbs; //maxBarSize
public MyLabel(int mbs) {
this.mbs = mbs;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
int width = (int)(((double)(this.getWidth()))/mbs*bs);
if(width == 0) return;
g.fillRect(0,0,width,this.getHeight());
}
synchronized public void fill() {
if(bs == mbs) {
try {
wait();
} catch (InterruptedException e) {
return;
}
}
bs++;
repaint();
notify();
}
synchronized public void consume() {
if(bs==0) {
try {
wait();
} catch (InterruptedException e) {
return;
}
}
bs-=5;
repaint();
notify();
}
}
class CST extends Thread {
private MyLabel bar;
public CST(MyLabel bar) {
this.bar = bar;
}
public void run() {
while(true) {
try {
sleep(200);
bar.consume();
} catch (InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
private MyLabel bar = new MyLabel(100);
public Main(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
bar.setBackground(Color.lightGray);
bar.setOpaque(true);
bar.setLocation(20, 50);
bar.setSize(300,20);
c.add(bar);
c.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
bar.fill();
}
});
setSize(350,200);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
CST th = new CST(bar);
th.start();
}
public static void main(String[] args) {
new Main("빨리빨리!");
}
}
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Main extends JFrame {
ImageIcon img1 = new ImageIcon("다람쥐.png");
ImageIcon img2 = new ImageIcon("도토리.png");
ImageIcon img3 = new ImageIcon("새.png");
JLabel l1 = new JLabel(img1);
JLabel l2 = new JLabel(img2);
JLabel l3 = new JLabel(img3);
int score=0;
JLabel sc = new JLabel(Integer.toString(score));
class ST extends Thread { //도토리
private Container c;
private int x=200,y=380;
public ST(Container c) {
this.c = c;
c.add(l2);
l2.setLocation(x, y);
l2.setSize(24, 24);
l2.setVisible(true);
}
public void run() {
while(true) {
y--;
if(l2.getY()==1) { //도토리가 범위 밖으로 나갔을때
l2.setLocation(200,380);
System.out.println("도토리 범위 아웃");
return;
}
if(l3.getX()+100 >= 224 && l3.getX() <= 236 && y<=101){ // crash
score++;
c.add(sc);
sc.setText(Integer.toString(score));
sc.setFont(new Font("Arial",Font.PLAIN,100));
//System.out.println("crash!");
l2.setLocation(200,380);
return;
}
try {
sleep(1);
l2.setLocation(200,y);
} catch (InterruptedException e) {
return;
}
l2.repaint();
}
}
}
class BT extends Thread { //새
private Container c;
private int x=500, y=1;
public BT(Container c) {
this.c = c;
}
public void run() {
c.add(l3);
l3.setSize(100,100);
while(true) {
x--;
if(x==0) {
x=500;
}
try {
sleep(1);
l3.setLocation(x,1);
} catch (InterruptedException e) {
return;
}
l3.repaint();
}
}
}
public Main() {
setTitle("피융");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
l1.setLocation(200,380);
l1.setSize(64,64);
l1.setVisible(true);
l2.setLocation(200,380);
l3.setLocation(500,1);
c.add(l1);
c.setFocusable(true);
c.requestFocus();
c.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
//도토리 쓰레드는 동시에 한 개만 존재할 수 없도록()실행중이라면, 새로 못만들게)
if(e.getKeyCode() == KeyEvent.VK_SPACE && l2.getY()==380) {
// if(th.getState()) {
//
// }
ST th = new ST(c);
th.start();
System.out.println("도토리 객체 생성!");
}
}
});
setSize(500,500);
setVisible(true);
BT th2 = new BT(c);
th2.start();
}
public static void main(String[] args) {
new Main();
}
}
//다람쥐가 쏜 도토리를 새가 맞으면 도토리와 새를 원래 위치로 돌려 보내고, 점수를 증가시킨다.



