import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
//class MyRect
//속성
//한 점을 저장
//Width, height
//색깔 정보
//메소드
//생성자( default, 한점과 너비,높이정보를 매개변수로)
//색깔에 대한 getter, setter
class MyRect {
//한 점을 저장
int x, y;
//Width, height
int width, height;
//색깔 정보
Color color;
public MyRect() {
}
public MyRect(int x, int y, int width, int height, Color color) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(x, y, width, height);
}
}
class MyPanel extends JPanel implements KeyListener {
MyRect[][] rects;
// 지렁이 머리의 위치를 위한 필드
int head_x;
int head_y;
final static int MAX_X = 20;
final static int MAX_Y = 20;
final static int BASE_X = 100;
final static int BASE_Y = 100;
final static int WIDTH = 40;
final static int HEIGHT = 40;
final static int INTERVAL_X = 1;
final static int INTERVAL_Y = 1;
public MyPanel() {
rects = new MyRect[MAX_X][MAX_Y];
// 지렁이 머리의 위치를 지정( X행, Y열의 위치)
head_x = MAX_X/2;
head_y = MAX_Y/2;
for(int i=0; i< MAX_X; i++) {
for(int j=0; j< MAX_Y; j++) {
// 테두리에 속하면 빨강, 그렇지 않으면 검정
if( i == 0 || i == MAX_X-1 || j ==0 || j == MAX_Y -1) {
rects[i][j] = new MyRect(BASE_X+ (WIDTH+INTERVAL_X)*j,
BASE_Y+ (HEIGHT+INTERVAL_Y)*i,WIDTH,HEIGHT,
new Color(255,0,0));
} else {
rects[i][j] = new MyRect(BASE_X+ (WIDTH+INTERVAL_X)*j,
BASE_Y+ (HEIGHT+INTERVAL_Y)*i,WIDTH,HEIGHT,
new Color(0,0,0));
}
}
}
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(new Font("굴림", Font.BOLD, 50));
g.drawString("영진전문대학교 컴퓨터정보계열", 150, 50);
g.setColor(Color.MAGENTA);
g.drawString("AI빅데이터응용소프트웨어과", 180, 100);
g.setColor(Color.BLACK);
g.setFont(new Font("굴림", Font.BOLD, 80));
g.drawString("홍", (MAX_X+1) * WIDTH + BASE_X , (MAX_Y-4)/2*HEIGHT + BASE_Y);
g.drawString("길", (MAX_X+1) * WIDTH + BASE_X , MAX_Y/2*HEIGHT + BASE_Y);
g.drawString("동", (MAX_X+1) * WIDTH + BASE_X , (MAX_Y+4)/2*HEIGHT + BASE_Y);
g.drawString("2", 30, (MAX_Y/2-6)*HEIGHT + BASE_Y);
g.drawString("2", 30, (MAX_Y/2-4)*HEIGHT + BASE_Y);
g.drawString("0", 30, (MAX_Y/2-2)*HEIGHT + BASE_Y);
g.drawString("1", 30, MAX_Y/2*HEIGHT + BASE_Y);
g.drawString("2", 30, (MAX_Y/2+2)*HEIGHT + BASE_Y);
g.drawString("3", 30, (MAX_Y/2+4)*HEIGHT + BASE_Y);
g.drawString("4", 30, (MAX_Y/2+6)*HEIGHT + BASE_Y);
for(int i=0; i< rects.length; i++) {
for(int j=0; j< rects[0].length; j++) {
rects[i][j].draw(g);
}
}
// 지렁이의 머리를 칠하자.
rects[head_x][head_y].setColor(Color.yellow);
rects[head_x][head_y].draw(g);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("홍길동" + e.getKeyCode() + ":" + e.getKeyChar());
caterpillarClear();
switch (e.getKeyCode()) { // == 37 ) {
case 37: moveLeft(); break; // 왼쪽 화살표키
case 39: moveRight(); break; // 오른쪽
case 38: moveUp(); break; // 위쪽
case 40: moveDown(); break; // 아래쪽
}
rects[head_x][head_y].setColor(Color.YELLOW);
repaint();
}
private void caterpillarClear() {
rects[head_x][head_y].setColor(Color.BLACK);
}
private void moveDown() {
head_x = head_x +1;
}
private void moveUp() {
head_x = head_x -1;
}
private void moveRight() {
head_y = head_y +1;
}
private void moveLeft() {
head_y = head_y -1;
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class MyFrame extends JFrame {
// 생성자 - 패널 추가
public MyFrame() {
setSize(600, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 패널 객체 생성
MyPanel myPanel = new MyPanel();
this.add(myPanel);
myPanel.addKeyListener(myPanel);
myPanel.setFocusable(true);
setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}