실습목표: 배운 GUI와 Thread에 대한 내용을 코드에 적용해보기
public class Raindrop extends JButton implements Runnable{
private Thread t= new Thread(this);
Raindrop(int x, int y){
this.setBounds(x+50, y+50, 50, 50);
//배경 테두리 지우기
this.setBorderPainted(false);
this.setContentAreaFilled(false);
this.setFocusPainted(false);
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("빗방울이 눌림");
Raindrop.this.setIcon(new ImageIcon(View.class.getResource("/img/눈.png")));
}
});
}
@Override
public void run() {
//밑으로 내려가기
while(this.getLocation().y < 600) {
this.setLocation(this.getLocation().x, this.getLocation().y + 5);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//다 내려가면 이미지 바꾸기
this.setIcon(new ImageIcon(View.class.getResource("/img/새싹.png")));
repaint(); //화면에 적용되게
System.out.println("빗방울 땅에 떨어짐");
}
}빗방울
public class View extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton cloud;
/**
* Create the frame.
*/
public View() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
cloud = new JButton("");
cloud.setIcon(new ImageIcon(View.class.getResource("/img/\uAD6C\uB984.png")));
//배경 테두리 지우기
cloud.setBorderPainted(false);
cloud.setContentAreaFilled(false);
cloud.setFocusPainted(false);
//구름 누르면 빗방울 생성
cloud.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("구름 클릭");
JButton raindrop = new Raindrop(cloud.getLocation().x, cloud.getLocation().y);
raindrop.setIcon(new ImageIcon(View.class.getResource("/img/빗방울.png")));
contentPane.add(raindrop);
contentPane.repaint(); //빗방울이 보이게
Thread t = new Thread((Runnable)raindrop);
t.start();
}
});
//구름 이동
cloud.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT)
cloud.setLocation(cloud.getLocation().x + 5, cloud.getLocation().y);
else if(keyCode == KeyEvent.VK_LEFT)
cloud.setLocation(cloud.getLocation().x - 5, cloud.getLocation().y);
}
});
cloud.setBounds(32, 23, 158, 130);
contentPane.add(cloud);
}
}View
public class Main {
public static void main(String[] args) {
View frame = new View();
frame.setVisible(true);
}
}Main
알낳기 실행영상.mp4
13.90MB
알게된 점 / 궁금한 점
화면에 무언가를 변경하는 코드를 작성한 후 실행했을 때 그 오브젝트 자체가 화면에 보이지 않는 문제를 해결하는 게 가장 어려웠다. 팀원도 같은 문제를 겪고 있어서 같이 여러 AI들에게 물어본 결과, repaint()라는 명령어를 통해서 변경사항을 화면에 반영시켜야 한다는 답변을 받았다. 실제로 repaint()를 추가하자 문제가 해결되었다. 수업시간에는 이런 내용에 대한 설명을 듣지 못했던 것 같은데, repaint() 없이도 문제해결이 가능한지 궁금하다.
개선 가능한 부분
지금은 간단한 코드라 분리하지 않았지만, Dropable/Changable/Creatable 등의 Interface로 기능을 분리하면 OCP 원칙에 좀 더 부합하는 코드를 만들 수 있다. 번개 구름을 만들어 번개가 떨어지게 만들거나 한다면 Interface가 있을 때 더 쉽게 구현이 가능할 것이다.
'JAVA' 카테고리의 다른 글
| [JAVA] DB (0) | 2025.11.16 |
|---|---|
| [JAVA] Network & Socket (0) | 2025.11.04 |
| [JAVA] 중간고사 대비 (0) | 2025.10.19 |
| [JAVA] 디자인 패턴 - 템플릿 메소드 (0) | 2025.10.17 |
| [JAVA] 디자인 패턴 - 전략 (짱구 예제) (0) | 2025.10.15 |