6/22

ONLYS2545·2023년 6월 23일
0

BackgroundPlayerService.java


package test.ex062;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

// 메인 쓰레드는 바쁨 - 키보드 이벤트를 처리하기 바쁨
// 백그라운드에서 관찰
public class BackgroundPlayerService implements Runnable {

    private Player player;
    private BufferedImage image;

    public BackgroundPlayerService(Player player) {
        this.player = player;
        File file = new File("image/test.png");

        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {

            // Color color = new Color(image.getRGB(player.getX(), player.getY()));
            // System.out.println("보글이의 위치의 빨간색상 :" + color.getRed());
            // System.out.println("보글이의 위치의 초록색상 :" + color.getGreen());
            // System.out.println("보글이의 위치의 블루색상 :" + color.getBlue());
            // System.out.println("보글이의 x값 : " + player.getX());

            Color color1 = new Color(image.getRGB(player.getX() + 65, player.getY()));
            Color color2 = new Color(image.getRGB(player.getX() - 25, player.getY()));
            Color color3 = new Color(image.getRGB(player.getX(), player.getY() + 5));

            if ((color1.getRed() == 255 && color1.getGreen() == 0 && color1.getBlue() == 0)
                    || (color2.getRed() == 255 && color2.getGreen() == 0 && color2.getBlue() == 0)
                    || (color3.getRed() == 0 && color3.getGreen() == 0 && color3.getBlue() == 255)) {
                System.out.println("벽에 충돌이 감지됨");
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

BubbleFrame.java


package test.ex062;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

// main 스레드를 GUI 프로그램에서는 ui 스레드라고 부른다.
public class BubbleFrame extends JFrame {

    private JLabel backgroundMap;
    private Player player;

    public BubbleFrame() {
        initObject();
        initSetting();
        initListener();
        setVisible(true); // while

    }

    private void initObject() {
        backgroundMap = new JLabel(new ImageIcon("image/test.png"));
        setContentPane(backgroundMap);
        player = new Player();
        add(player);

        new Thread(new BackgroundPlayerService(player)).start();
    }

    private void initSetting() {
        setSize(1000, 640);
        setLayout(null); // absoulte 레이아웃 (자유롭게 그림을 그릴 수 있다)
        setLocationRelativeTo(null); // JFrame 가운데 배치하기
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // x버튼으로 창을 끌 때 JVM 같이 종료하기
    }

    private void initListener() {
        addKeyListener(new KeyAdapter() {

            // 키보드 클릭 이벤트 핸들러
            @Override
            public void keyPressed(KeyEvent e) {
                // System.out.println(e.getKeyCode());

                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if (!player.isLeft()) {
                        player.left();
                    }
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if (!player.isRight()) {
                        player.right();
                    }

                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                    if (!player.isUp() && !player.isDown()) {
                        player.up();
                    }
                }

                else if (e.getKeyCode() == KeyEvent.VK_UP) {
                    if (!player.isDown()) {
                        player.down();
                    }
                }
            }

            // 키보드 해제 이벤트 핸들러
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    player.setLeft(false);

                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    player.setRight(false);
                }
            }
        });
    }

    public static void main(String[] args) {
        new BubbleFrame();
    }
}

Player.java


package test.ex062;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Player extends JLabel {
    // 위치 상태
    private int x;
    private int y;

    // 플레이어의 방향 상태
    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;

    // 플레이어 속도 상태
    private final int SPEED = 10; // x축
    private final int JUMPSPEED = 4; // y축

    private ImageIcon playerR, playerL;

    public Player() {

        initObject();
        initSetting();

    }

    public void initObject() {
        playerR = new ImageIcon("image/playerR.png");
        playerL = new ImageIcon("image/playerL.png");

    }

    private void initSetting() {
        x = 70;
        y = 540;

        setIcon(playerR);
        setSize(50, 50);
        setLocation(x, y);

        left = false;
        right = false;
        up = false;
        left = false;

    }

    public void right() {
        System.out.println("right메서드 실행됨");
        right = true;

        new Thread(() -> {
            while (right) {
                setIcon(playerR);
                x = x + SPEED;
                setLocation(x, y);

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();

    }

    public void left() {
        System.out.println("left메서드 실행됨");
        left = true;

        new Thread(() -> {
            while (left) {
                setIcon(playerL);
                x = x - SPEED;
                setLocation(x, y);

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }

    public void up() {
        System.out.println("up 메서드 실행됨");
        up = true;

        new Thread(() -> {
            for (int i = 0; i < 130 / JUMPSPEED; i++) {

                y = y - JUMPSPEED;
                setLocation(x, y);

                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            up = false;
            down();
        }).start();

    }

    public void down() {
        System.out.println("down 메서드 실행됨");
        down = true;

        new Thread(() -> {
            for (int i = 0; i < 130 / JUMPSPEED; i++) {

                y = y + JUMPSPEED;
                setLocation(x, y);

                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            down = false;
        }).start();

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isLeft() {
        return left;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public boolean isDown() {
        return down;
    }

    public void setDown(boolean down) {
        this.down = down;
    }

    public ImageIcon getPlayerR() {
        return playerR;
    }

    public void setPlayerR(ImageIcon playerR) {
        this.playerR = playerR;
    }

    public ImageIcon getPlayerL() {
        return playerL;
    }

    public void setPlayerL(ImageIcon playerL) {
        this.playerL = playerL;
    }

}
profile
백엔드 교육과정 기록 velog입니다.

0개의 댓글