목차
목표
1. 천장에 닿으면 3초 뒤에 bomb 이미지로 변경
2. 0.5 초 후 이미지 제거 (메모리에서 제거는 현재 단계에서 힘듦)
package bubble.test.ex09;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Bubble extends JLabel implements Moveable {
private Player player;
private BubbleFrame mContext;
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(BubbleFrame mContext) {
this.mContext = mContext;
this.player = mContext.getPlayer();
initData();
setInitLayout();
}
// get, set
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 Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
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);
}
@Override
public void left() {
left = true;
for (int i = 0; i < 400; i++) {
x--;
setLocation(x, y);
if (backgroundBubbleService.leftWall()) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
left = false;
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();
}
}
right = false;
up();
}
@Override
public void up() {
up = true;
while (up) {
y--;
setLocation(x, y);
if (backgroundBubbleService.topWall()) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
up = false;
clearBubble();
}
// 외부 호출 안될 메서드
private void clearBubble() {
// 3초 뒤에 터짐
try {
Thread.sleep(3000);
setIcon(bomb);
// 0.5 초 뒤에 삭제
Thread.sleep(500);
// 메모리에서 해제 처리해야 함
// JFrame 안에 remove 메서드가 있다.
setIcon(null);
// 오류가 생겨서 일단 이미지만 지우는 단계에서 멈춤
// mContext.remove(this); // 컴포넌트에서 제거 --> 다시 그림을 그리지 않는다
// mContext.repaint(); // 호출하는 순간 화면의 컴포넌트들 죄다 다시 그리는 거라 좋지 않음
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
정리
1. 천장에 닿으면 3초 뒤에 bomb이미지로 변경
- 3초를 슬립하고 bomb이미지로 바꾸는 clearBubble 메서드를 작성
- up() 메소드의 반복을 빠져나왔다는 말은 천장에 닿았다는 말이기 때문에
- 반복문을 나오면 clearBubble 메서드 호출
2. 0.5 초 후 이미지 제거
- clearBubble 메소드에서 그냥 bomb 으로 바꾸고 0.5 초후에 이미지를 null로 변경
- 사실 이렇게 해도 메모리 상에 bubble 객체는 남아 있지만
- 현재 단계에서는 그거 까지 신경쓰지 않기로함
'Java > Swing 프로젝트' 카테고리의 다른 글
Bubble 적군 생성 - 10 (0) | 2024.05.07 |
---|---|
Swing 프로젝트 (0) | 2024.05.07 |
Bubble 버블 생성 동작 수정 - 8 (0) | 2024.05.07 |
Bubble 버블 벽 감지 - 7 (0) | 2024.05.07 |
Bubble 버블 동작 처리 - 6 (1) | 2024.05.03 |