/*import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
//0~4까지의 한 자릿수가 출력된다, 패널에 마우스를 누를 때마다 3개의 수를 랜덤하게 발생 각 수를 레이블에 출력, 3개의 수를 왼쪽에서부터 200ms간격으로 하나씩 출력
// 모든 자리의 숫자가 같은 숫자면 "축하합니다!!" 아니면 "아쉽군요" 출력
class thread extends Thread{
Random rd = new Random();
private Vector ve;
public int x=50;
public int y=50;
public JLabel[] label1=new JLabel[10];
public int[] xrr; // vector x좌표
public int[] yrr; // vector y좌표
public thread(Vector ve) {
this.ve = ve;
}
public void run() {
xrr = new int[10];
yrr = new int[10];
for(int i=0; i<10; i++) {
label1[i]=(JLabel)ve.get(i);
}
while(true){
try {
sleep(200);
}catch(InterruptedException e) {
return;
}
int com = rd.nextInt(4);
for(int i=0; i<10; i++) {
xrr[i]=label1[i].getX();
yrr[i]=label1[i].getY();
}
for(int i=9; i>=1; i--) {
label1[i-1].setLocation(xrr[i],yrr[i]);
}
if(com==0) {// 위
y-=15;
if(y!=0) {
label1[9].setLocation(x,y);
}
}
if(com==1) {// 아래
y+=15;
if(y!=500) {
label1[9].setLocation(x,y);
}
}
if(com==2) {// 오른쪽
x+=15;
if(x!=500) {
label1[9].setLocation(x,y);
}
}
if(com==3) {// 왼쪽
x-=15;
if(x!=0) {
label1[9].setLocation(x,y);
}
}
}
}
}
public class Main extends JFrame{
private JLabel label = new JLabel();
Vector<JLabel> v = new Vector<JLabel>();
private JLabel[] label1 = new JLabel[10];
thread th1 = new thread(v);
public Main() {
setTitle("스네이크 움직이기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
for(int i=0; i<10; i++) {
label1[i]= new JLabel();
v.add(label1[i]);
c.add(label1[i]);
label1[i].setOpaque(true);
label1[i].setSize(15,15);
}
for(int i=1; i<10; i++) {
label1[i].setBackground(Color.black);
}
label1[9].setBackground(Color.CYAN);
label=new JLabel();
label.addKeyListener(new MyKeyListener());
c.add(label);
th1.start();
label.setOpaque(true);
label.setSize(15,15);
label.setLocation(250, 250);
label.setBackground(Color.green);
label.setFocusable(true);
label.requestFocus();
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Main();
}
class MyKeyListener implements KeyListener{
int x = 250;
int y = 250;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==37) {
//왼쪽
x-=5;
label.setLocation(x,y);
}
if(e.getKeyCode()==40) {
//아래
y +=5;
label.setLocation(x,y);
}
if(e.getKeyCode()==39) {
//오른쪽
x += 5;
label.setLocation(x,y);
}
if(e.getKeyCode()==38) {
//위쪽
y -= 5;
label.setLocation(x,y);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
class thread extends Thread{
private Vector v;
private JLabel[] label = new JLabel[30];
Random rd = new Random(450);
int y=0;
public thread(Vector v) {
this.v=v;
for(int i=0; i<30; i++) {
label[i]= (JLabel)v.get(i);
}
}
public void run() {
int com = rd.nextInt()+5;
for(int i=0; i<30; i++) {
label[i].setLocation(com,0);
}
while(y!=500) {
try {
sleep(50);
}catch(InterruptedException e) {
return;
}
label[i].setLocation()
}
}
}
public class Main extends JFrame{
Vector<JLabel> v = new Vector<JLabel>();
private JLabel [] label = new JLabel[30];
public Main() {
setTitle("눈 내리는 샤갈의 마을");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
for(int i=0; i<30; i++) {
c.add(label[i]);
v.add(label[i]);
}
thread th1 = new thread(v);
th1.start();
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Main();
}
}
// 책 쓴 사람이 참고 자료 같이 넣어놓은 내용
/*//눈은 총 50개로 하고 눈의 위치는 Point객체를 이용하며 50개의 객체를 벡터에 넣어서 관리한다
Vector<Point> snowVector = new Vector<Point>();
g.setColor(Color.white);
for(int i=0; i<snowVector.size(); i++){
Point p = snowVector.get(i);
g.fillOval(p.x, p.y, 10 ,10);
}// 흰 색 눈을 그려서 연출
class snowThread extends Thread{
public void run(){
while(true){
try {
sleep(300);
}
catch(InterruptedException e){
return;
}
changeSnowPosition();
repaint();
}
}
} // 50개의 눈의 위치를 계속 갱신하는 스레드는 다음과 같이 작성, 눈이 떨어져 패널 아래로 내려가면 다시 위에서부터 나타나게 함
*/