/*
import java.awt.*;
import javax.swing.*;
class TimerThread extends Thread {
int n = 0;
private JLabel timerLabel;
public TimerThread(JLabel timerLabel,int n) {
this.timerLabel = timerLabel;
this.n=n;
}
public void run() {
while (true) {
timerLabel.setText(Integer.toString(n));
n+=2;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main() {
setTitle("에제13-1");
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel1 = new JLabel();
timerLabel1.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel1);
JLabel timerLabel2 = new JLabel();
timerLabel2.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel2);
TimerThread th1 = new TimerThread(timerLabel1,0);
TimerThread th2 = new TimerThread(timerLabel2,1);
setSize(300,170);
setVisible(true);
th1.start();
th2.start();
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
int n=0;
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel,int n) {
this.timerLabel=timerLabel;
this.n=n;
}
public void run() {
while(true) {
timerLabel.setText(Integer.toString(n));
n+=2;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main(){
setTitle("예제 13-2");
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel1 = new JLabel();
timerLabel1.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel1);
JLabel timerLabel2 = new JLabel();
timerLabel2.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel2);
TimerRunnable runnable1 = new TimerRunnable(timerLabel1,0);
TimerRunnable runnable2 = new TimerRunnable(timerLabel2,1);
Thread th1 = new Thread(runnable1);
Thread th2 = new Thread(runnable2);
setSize(250,150);
setVisible(true);
th1.start();
th2.start();
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
int n=0;
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel=timerLabel;
}
public void run() {
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame{
private Thread th;
JButton b = new JButton("Stop!!");
public Main() {
setTitle("예제13-5");
setSize(300,170);
setVisible(true);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel= new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
TimerRunnable runnable = new TimerRunnable(timerLabel);
th = new Thread (runnable);
c.add(timerLabel);
th.start();
c.add(b);
b.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
th.interrupt(); // 쓰레드 종료
b.setEnabled(false); //버튼 못누르게
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RandomThread extends Thread {
private Container contentPane;
private boolean flag=false;
public RandomThread(Container contentPane) {
this.contentPane = contentPane;
}
public void finish() {
flag = true;
}
public void run(){
while(true) {
int x = ((int)(Math.random()*contentPane.getWidth()));
int y = ((int)(Math.random()*contentPane.getHeight()));
JLabel label = new JLabel("coding");
label.setSize(80,30);
label.setLocation(x,y);
contentPane.add(label);
contentPane.repaint();
try {
Thread.sleep(300);
if(flag==true) {
contentPane.removeAll();
label = new JLabel("finish");
label.setSize(80,30);
int x1 = ((int)(Math.random()*contentPane.getWidth()));
int y1= ((int)(Math.random()*contentPane.getHeight()));
label.setLocation(x1,y1);
label.setForeground(Color.CYAN);
contentPane.add(label);
contentPane.repaint();
return;
}
}
catch(InterruptedException e) {return; }
}
}
}
public class Main extends JFrame {
private RandomThread th;
public Main() {
setTitle("예제13-6");
Container c = getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
th.finish();
}
});
setSize(300,200);
setVisible(true);
th = new RandomThread(c);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
public class Main {
public static void main(String [] args) {
SharedBoard board = new SharedBoard();
Thread th1 = new StudentThread("Kitae", board);
Thread th2 = new StudentThread("hyosoo", board);
th1.start();
th2.start();
}
}
class SharedBoard {
private int sum = 0;
synchronized public void add() {
int n = sum;
Thread.yield();
n += 10;
sum = n;
System.out.println(Thread.currentThread().getName()+":" + sum);
}
public int getSum() {return sum;}
}
class StudentThread extends Thread{
private SharedBoard board;
public StudentThread(String name,SharedBoard board) {
super(name);
this.board = board;
}
public void run() {
for(int i=0;i<10;i++) {
board.add();
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyLabel extends JLabel {
private int barSize = 0;
private int maxBarSize;
public MyLabel(int maxBarSize) {
this.maxBarSize = maxBarSize;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.setColor(Color.MAGENTA);
int width = (int) (((double)(this.getWidth()))/maxBarSize*barSize);
if(width==0) return;
g.fillRect(0, 0,width, this.getHeight());
}
synchronized public void fill() {
if(barSize == maxBarSize) {
try {
wait();
}catch(InterruptedException e) { return; }
}
barSize++;
repaint();
notify();
}
synchronized public void consume() {
if(barSize == 0) {
try {
wait();
}catch (InterruptedException e) {return; }
}
barSize--;
repaint();
notify();
}
}
class ConsumerThread extends Thread {
private MyLabel bar;
public ConsumerThread(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 tittle) {
super(tittle);
Container c = getContentPane();
c.setLayout(null);
bar.setBackground(Color.orange);
bar.setOpaque(true);
bar.setLocation(20,50);
bar.setSize(300,20);
c.add(bar);
c.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
bar.fill();
}
});
setSize(350,200);
setVisible(true);
c.setFocusable(true);
ConsumerThread th = new ConsumerThread(bar);
th.start();
}
public static void main(String[] args) {
new Main("아무키나 빨리 눌러 바 채우기");
}
}
인터페이스 생성 방법
1. extend Thread 스레드 클래스 상속받기
2. Runnable 인터페이스 implements
//Runnable 인터페이스로 스레드 만들기
//import java.util.*;
//
//class RunnableThread implements Runnable{
// public void run() {
// System.out.println("dkanzlskdlqfur");
// for(int i=1;i<=10;i++) {
// System.out.print(i+" ");
// }
// System.out.println("tmfpem whdfy");
// }
//}
//
//class Main{
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// String str = sc.next();
// Thread th = new Thread(new RunnableThread());
// th.start();
//
// }
//}
//Thread 클래스 상속받아서 스레드 생성하기
import java.util.*;
class RunnableThread extends Thread{
public void run() {
System.out.println("dkanzlskdlqfur");
for(int i=1;i<=10;i++) {
System.out.print(i+" ");
}
System.out.println("tmfpem whdfy");
}
}
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("아무 문자나 입력하세요>>");
String str = sc.next();
RunnableThread th = new RunnableThread();
th.start();
}
}
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() {
// 라벨 만들어서 컨텐트팬에 추가
JLabel la = new JLabel("버블");
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();
}
}
*/
//다음시간에 라벨을 이미지로 변경하기



