/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
c.requestFocus();
ConsumerThread th = new ConsumerThread(bar);
th.start();
}
public static void main(String[] args) {
new Main("아무키나 빨리 눌러 바 채우기");
}
}
*/
/*
import java.util.Scanner;
class runnable implements Runnable{
@Override
public void run() {
System.out.println("스레드 실행 시작");
// TODO Auto-generated method stub
int n=0;
for(int i=0; i<10; i++) {
n++;
if(i!=9) System.out.print(n);
if(i==9) System.out.println(n);
}
System.out.println("스레드 종료");
}
}
public class Main{
public static void main(String[] args) {
Thread th = new Thread(new runnable());
System.out.print("아무 키나 입력>>");
Scanner scanner = new Scanner(System.in);
String all = scanner.next();
th.start();
}
}
*/
/*class thread extends Thread{
private JLabel label;
public thread(JLabel label) {
this.label=label;
}
public void run() {
while(true) {
try {
sleep(500);
}catch(InterruptedException e) {
return;
}
Random rd = new Random();
int x= rd.nextInt(490);
int y= rd.nextInt(490);
label.setLocation(x, y);
}
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
class thread extends Thread{
private JLabel label;
public thread(JLabel label) {
this.label=label;
}
public void run() {
while(true) {
try {
sleep(500);
}catch(InterruptedException e) {
return;
}
Random rd = new Random();
int x = rd.nextInt(490);
int y = rd.nextInt(490);
label.setLocation(x,y);
}
}
}
public class Main extends JFrame{
private JLabel label= new JLabel();
int com;
thread th1 = new thread(label);
public Main() {
setTitle("원을 0.5초 간격으로 랜덤한 위치 이동");
Container c = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(null);
c.add(label);
label.setLocation(250, 250);
label.setSize(20,20);
label.setOpaque(true);
label.setBackground(Color.blue);
MyMouseListener lis1 = new MyMouseListener();
c.addMouseListener(lis1);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Main();
}
class MyMouseListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
th1.start();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
class thread extends Thread{
private JLabel label;
public thread(JLabel label) {
this.label=label;
}
public void run() {
while(true) {
Calendar c= Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
try {
sleep(1000);
}catch(InterruptedException e) {
return;
}second++;
try {
sleep(60000);
}catch(InterruptedException e) {
return;
} min ++;
try {
sleep(360000);
}catch(InterruptedException e) {
return;
} hour ++;
String clockText = Integer.toString(hour);
clockText = clockText.concat(":");
clockText = clockText.concat(Integer.toString(min));
clockText = clockText.concat(":");
clockText = clockText.concat(Integer.toString(second));
label.setText(clockText);
}
}
}
public class Main extends JFrame{
private JLabel label = new JLabel();
public Main(){
setTitle("디지털 시계 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(label);
thread th1 = new thread(label);
th1.start();
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Main();
}
}