JAVA

코드 리팩토링 lombok 활용하기

jiyoon12 2025. 5. 12. 17:33
  • lombok 검색하기

 

  • lombok 다운로드

  • 인텔리제이 project structure → project settings  → Libraries → + 클릭 후 

ㅇㅣㄴㅌ

  • lombok추가하기

 

 

2. 버블버블 게임 코드에 적용하기

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"));
        // 루트 패널에 JLabel 넣어보기
        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 {

    // mContext -> 별 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) {
        // mContext --> 문백(환경 정보)
        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() {
        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));

    }
}

 

  • lombook 이용해서 getter,setter 적용하기
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();
        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++;
            // 좌표 오른쪽으로 1 움직였는데 오른쪽 벽인지 아닌지 매번 확인
            setLocation(x, y);
            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);
        }
    }
}

'JAVA' 카테고리의 다른 글

JDBC 구성 요소  (0) 2025.05.14
java.time 패키지  (0) 2025.05.14
JDBC 알아보기  (0) 2025.05.12
버블버블게임 7단계 (물방울의 벽, 천장 감지)  (1) 2025.05.08
제네릭 <T extends 클래스> 사용 및 실습하기  (0) 2025.05.08