물방울 벽 감지 ( 쓰레드 사용 안함)
package bubble.test.ex08;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BackgroundBubbleService {
private BufferedImage image;
private Bubble bubble; // 연관 관계(생성자 의존 주입)
// 생성자
public BackgroundBubbleService(Bubble bubble) {
this.bubble = bubble;
try {
image = ImageIO.read(new File("img/backgroundMapService.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
// 왼쪽 벽에
public boolean leftWall() {
Color leftColor = new Color(image.getRGB(bubble.getX() + 10, bubble.getY() + 25));
// 255 0 0 <--- 빨간색 (왼쪽벽 확인)
// 빠른 평가
if(leftColor.getRed() == 255 && leftColor.getGreen() == 0 && leftColor.getBlue() == 0) {
return true;
}
return false;
}
// 오른쪽 벽에
public boolean rightWall() {
Color rightColor = new Color(image.getRGB(bubble.getX() + 50 + 10, bubble.getY() + 25));
if(rightColor.getRed() == 255 && rightColor.getGreen() == 0 && rightColor.getBlue() == 0) {
return true;
}
return false;
}
// 천창 벽에
public boolean topWall() {
Color topColor = new Color(image.getRGB(bubble.getX() + 25, bubble.getY()));
if(topColor.getRed() == 255 && topColor.getGreen() == 0 && topColor.getBlue() == 0) {
return true;
}
return false;
}
}
package bubble.test.ex08;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Bubble extends JLabel implements Moveable {
// 의존성 컴포지션 관계
private Player player;
private BackgroundBubbleService backgroundBubbleService;
private int x;
private int y;
// 움직임 상태
private boolean left;
private boolean right;
private boolean up;
// 적군을 맞춘 상태
private int state; // 0.(기본 물방울), 1.(적을 가둔 상태 물방울)
private ImageIcon bubble; // 기본 물방울
private ImageIcon bubbled; // 적을 가둔 물방울
private ImageIcon bomb; // 물방울 팡!
// 연관관계, 의존성 컴포지션 관계, 생성자 의존 (DI)
public Bubble(Player player) {
this.player = player;
initData();
setInitLayout();
// 객체 생성시 무조건 스레드 시작
initThread();
}
//get,set
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
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 int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public ImageIcon getBubble() {
return bubble;
}
public void setBubble(ImageIcon bubble) {
this.bubble = bubble;
}
public ImageIcon getBubbled() {
return bubbled;
}
public void setBubbled(ImageIcon bubbled) {
this.bubbled = bubbled;
}
public ImageIcon getBomb() {
return bomb;
}
public void setBomb(ImageIcon bomb) {
this.bomb = bomb;
}
private void initData() {
bubble = new ImageIcon("img/bubble.png");
bubbled = new ImageIcon("img/bubbled.png");
bomb = new ImageIcon("img/bomb.png");
backgroundBubbleService = new BackgroundBubbleService(this);
left = false;
right = false;
up = false;
state = 0;
}
private void setInitLayout() {
x = player.getX();
y = player.getY();
setIcon(bubble);
setSize(50, 50);
setLocation(x, y);
}
// 공통으로 사용하는 부분을 메서드로 만들어 보자.
// 이 메서드는 내부에서만 사용할 예정
private void initThread() {
// 버블을 스레드가 하나면 된다.
// 익명 클래스,
new Thread(new Runnable() {
@Override
public void run() {
if(player.playerWay == PlayerWay.LEFT) {
left();
} else {
right();
}
}
}).start();
}
@Override
public void left() {
left = true;
for(int i = 0; i < 400; i++) {
x--;
setLocation(x, y);
// 만약 왼쪽벽 --> up()
if(backgroundBubbleService.leftWall()) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
up();
}
@Override
public void right() {
right = true;
for(int i = 0; i < 400; i++) {
x++;
setLocation(x, y);
if(backgroundBubbleService.rightWall()) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
up();
}
@Override
public void up() {
up = true;
while(true) {
y--;
setLocation(x, y);
if(backgroundBubbleService.topWall()) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
'Java > Java.Swing' 카테고리의 다른 글
Swing bubble bubble Game 10 (몬스터 만들기) (0) | 2024.05.07 |
---|---|
Swing bubble bubble Game 9 (버블 생성 동작 수정) (0) | 2024.05.07 |
Swing bubble bubble Game 7 버블 동작 처리 (0) | 2024.05.03 |
Swing bubble bubble Game 6(바닥, 층 감지 기능 추가) (0) | 2024.05.03 |
Swing bubble bubble Game 5 (물방울 생성) (0) | 2024.05.03 |