import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
class Main extends JFrame{
ImageIcon body = new ImageIcon("square.png");
ImageIcon head = new ImageIcon("snake.png");
JLabel [] snakeB = new JLabel[10];
JLabel snakeH = new JLabel(head);
Move move = new Move();
Vector<Point> v = new Vector<Point>();
Container c = getContentPane();
public Main() {
setTitle("스네이크 움직이기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(null);
snakeH.setSize(32,32);
snakeH.setLocation(0,0);
c.add(snakeH);
for(int i=0;i<10;i++) {
snakeB[i]= new JLabel(body);
snakeB[i].setSize(32,32);
}
c.addKeyListener(new MyKeyListener());
c.setFocusable(true);
c.requestFocus();
setSize(500,500);
setVisible(true);
}
int x=0, y=0, t=0;
int StartC=0;
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if(KeyCode==e.VK_RIGHT) {//1
if(StartC==0) {
t=1;
move.start();
}
else t=1;
}
if(KeyCode==e.VK_LEFT) {//2
if(StartC==0) {
t=2;
move.start();
}
else t=2;
}
if(KeyCode==e.VK_DOWN) {//3
if(StartC==0) {
t=3;
move.start();
}
else t=3;
}
if(KeyCode==e.VK_UP) {//4
if(StartC==0) {
t=4;
move.start();
}
else t=4;
}
repaint();
StartC++;
}
}
class Move extends Thread{
public void run(){
while(true) {
if(t==1) {
x+=5;
}
if(t==2) {
x-=5;
}
if(t==3) {
y+=5;
}
if(t==4) {
y-=5;
}
snakeH.setLocation(getX()+x, getY()+y);
try {
Thread.sleep(35);
}catch(InterruptedException e) {
return;
}
}
}
}
public static void main(String[] args) {
new Main();
}
}



