JAVA 유용한 클래스

(JAVA)코드 리팩토링(롬북 활용) - 8

mynote6676 2025. 5. 12. 19:31
package bubble.game;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class BubbleFrame extends JFrame {

    // 명시적으로
    BubbleFrame mContext = this; // 자기 자신의 주소값을 담는다.

    private JLabel backgroundMap;
    private Player player;

    public BubbleFrame() {
        initData();
        setInitLayout();
        addEventListener();

        new Thread(new BackgroundPlayerService(player)).start();
    }
    private void initData() {
        setTitle("버블버블게임");
        setSize(1000, 640);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        backgroundMap = new JLabel(new ImageIcon("img/backgroundMap.png"));
        // 루트 패널에 JLable 을 넣어보기
        setContentPane(backgroundMap);
        player = new Player(mContext);
    }
    private void setInitLayout() {
        setLayout(null); // 좌표기준 (절대 레이아웃)
        setResizable(false); // 리사이즈 조절 막기
        setLocationRelativeTo(null); // JFrame 화면 가운데 배치해줌
        add(player);
        setVisible(true);
    }

    private void addEventListener() {
        // 프레임에 키보드 이벤트 리스너 등록 처리
        this.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            // 키를 누룰때 .. 누르고 있으면 계속 이벤트 발생
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        if(player.isLeft() == false && player.isLeftWallCrash() == false) {
                            player.left();
                        }
                        break;
                    case KeyEvent.VK_RIGHT:
                       // 만약 플레이어가 오른쪽 가고 있는 상태가 아니라면 메서드를 수행해!
                       // 만약 플레이어가 오른쪽 가고 있는 상태라면 right() 수행 하지마
                        if(player.isRight() == false && player.isRightWallCrash() == false) {
                            player.right();
                        }
                        break;
                    case KeyEvent.VK_UP:
                        if(player.isUp() == false && player.isDown() == false) {
                            player.up();
                        }
                        break;
                }
            }


            @Override
            public void keyReleased(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        // 왼쪽으로 가고 있다면 멈춰(while 종료) -- 스레드 종료 됨
                        player.setLeft(false);
                        break;
                    case KeyEvent.VK_RIGHT:
                        // 움직이다가 멈춰 !!!
                        player.setRight(false);
                        break;
                    case KeyEvent.VK_UP:
                        break;
                    case KeyEvent.VK_SPACE:
                        player.attack();

                        // add(new Bubble(player));
                        break;
                }
            }
        });
    }

    // 테스트 코드
    public static void main(String[] args) {
        // BubbleFrame (이 하위에 생성되는 모든 객체들에 주소값에 접근 할 수 있다)
        // main 함수를 가질 수 있는 클래스는 Context 라는 개념이 생길 수 있다.
        new BubbleFrame();
    }

}

 

 

package bubble.game;

import lombok.Getter;
import lombok.Setter;

import javax.swing.*;

@Getter
@Setter
public class Player extends JLabel implements Moveable {
    // Context -> 별 5개 짜리
    private BubbleFrame mContext;

    private int x;
    private int y;

    private ImageIcon playerR;
    private ImageIcon playerL;

    // 플레이어의 속도 상태
    private final int SPEED = 4;
    private final int JUMP_SPEED = 2;

    // 플레이어의 움직인 상태
    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;

    // 벽에 충돌한 상태
    private boolean leftWallCrash;
    private boolean rightWallCrash;

    // 플레이어 방향 상태 (enum 타입 사용 법 1 - 선언 )
    private PlayerWay playerWay;


    // 나를 생성 시켜주는 BubbleFrame 의 주소값을 전달 받을 수 있도록 설계하자.
    public Player(BubbleFrame mContext) {
        // Context --> 문맥(환경정보)
        this.mContext = mContext;

        initData();
        setInitLayout();
    }

    private void initData() {
        playerR = new ImageIcon("img/playerR.png");
        playerL = new ImageIcon("img/playerL.png");
        // 플레이어 초기 상태 설정
        x = 55;
        y = 535;

        left = false;
        right = false;
        up = false;
        down = false;
    }

    private void setInitLayout() {
        setSize(50, 50);
        setIcon(playerR);
        setLocation(x, y);
    }


    @Override
    public void left() {
        // 클래스 이름으로 접근한다.
        playerWay = PlayerWay.LEFT;
        left = true;
        setIcon(playerL);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (left) {
                    x = x - SPEED;
                    setLocation(x, y);

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } // end of while
            } // end of run()
        }).start();
    }

    @Override
    public void right() {
        playerWay = PlayerWay.RIGHT;
        right = true; // 움직임 상태값 변경
        setIcon(playerR);
        // 익명 클래스 - thread.start() ---> run() 메서안에 구문 동작된다.
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (right) {
                    x = x + SPEED;
                    setLocation(x, y);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }

    @Override
    public void up() {
        System.out.println("점프!");
        up = true;
       new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 130 / JUMP_SPEED; i++) {
                    y = y - JUMP_SPEED;
                    setLocation(x, y);
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } // end of for
                up = false; // 상태값을 잘 다루어야 버그가 없다.
                down();
            }
        }).start();


    }

    @Override
    public void down() {
        down = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (down) {
                    y = y + JUMP_SPEED;
                    setLocation(x, y);
                    try {
                        Thread.sleep(3);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } // end of while
                down = false; // 상태값을 확실하게 처리하자.
            }
        }).start();

    }

    public void attack() {
        System.out.println("물발울 객체 생성");
         // JLabel에 부모 .... add() 호출함
        // add(new Bubble(this));

        // 1. 콜백 메서드를 직접 설계해서 완성 시킬 수 있다.
        // 2. 자식 클래스에서 부모 클래스의 주소값을(환경)을 전달 받아서
        //    부모.기능() 호출할 수 있다.
        // BubbleFrame --> add(new Bubble(this));
        // 부모 클래스 기능.add() 가능해 집니다.
        mContext.add(new Bubble(this));
    }
}

 

package bubble.game;

import lombok.Getter;
import lombok.Setter;

import javax.swing.*;

@Getter
@Setter
public class Bubble extends JLabel implements Moveable {

    private int x;
    private int y;

    // 물방울 움직임 상태
    private boolean left;
    private boolean right;
    private boolean up;

    private boolean isLeft; // true, false

    private ImageIcon bubble; // 기본 물방울
    private ImageIcon bomb; // 물방울이 터진 상태

    private Player player;
    private BackgroundBubbleService backgroundBubbleService;


    // 생성자를 통해서 Player 객체의 주소값을 주입 받기 -> 생성자 의존 주입
    public Bubble(Player player) {
        this.player = player;
        this.backgroundBubbleService = new BackgroundBubbleService(this);

        initData();
        setInitLayout();
        // 버블은 스레드가 하나면 된다.
        bubbleStartThread();
    }

    private void bubbleStartThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(player.getPlayerWay() == PlayerWay.LEFT) {
                    left();
                } else {
                    right();
                }
            }
        }).start();
    }


    private void initData() {
        bubble = new ImageIcon("img/bubble.png");
        bomb = new ImageIcon("img/bomb.png");
        left = false;
        right = false;
        up = false;
    }

    private void setInitLayout() {
        x = player.getX();
        y = player.getY();
        // TODO 수정 예정
        setIcon(bubble);
        // setIcon(bomb);

        setSize(50, 50);
        setLocation(x, y);
    }


    @Override
    public void left() {
        left = true;
        for(int i = 0; i < 400; i++) {
            x--;
            setLocation(x, y);
            if(backgroundBubbleService.leftWall() == true) {
                // 왼쪽 벽이다.
                break;
            }

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        up();
    }

    @Override
    public void right() {
        right = true;
        for(int i = 0; i < 400; i++) {
            x++;
            setLocation(x, y);
            // 좌표 오른쪽으로 1 움직였는데 오른쪽 벽인이 아닌지 매번 확인
            if(backgroundBubbleService.rightWall() == true) {
                break;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        up();
    }

    @Override
    public void up() {
        up = true;
        while (true) {
            y--;
            setLocation(x, y);
            if(backgroundBubbleService.topWall() == true) {
                break;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        // TODO 추후 수정 예정
        // 3초뒤에 이미지를 변경해 보세요
        try {
            Thread.sleep(3000);
            setIcon(bomb);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        try {
            Thread.sleep(500);
            setIcon(null);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}