728x90
반응형
package Ex01;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CoffeeMachineEx extends JFrame{
// 커피 시뮬레이션 패널
private CoffeePanel coffeeMachinePanel = new CoffeePanel();
public CoffeeMachineEx() {
setTitle("커피 머신기");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.add(new TitlePanel(), BorderLayout.NORTH);
c.add(coffeeMachinePanel, BorderLayout.CENTER);
c.add(new ButtonPanel(), BorderLayout.SOUTH);
setSize(450, 400);
setResizable(false);
setVisible(true);
}
//제목을 담당하는 패널 - North
class TitlePanel extends JPanel{
private JLabel titleMsg = new JLabel("안녕하세요. 맥심 자판기 입니다.");
public TitlePanel() {
titleMsg.setHorizontalAlignment(JLabel.CENTER);
setBackground(Color.MAGENTA);
add(titleMsg);
}
}
//기능을 담당하는 패널 - Center
class CoffeePanel extends JPanel{
private BoxLabel[] boxes = new BoxLabel[5]; //다섯개의 재료를 표현하는 레이블 컴포넌트
private JLabel coffeeImageLabel; //커피가 나왔을 때 이를 보여주는 커피잔 이미지 레이블
//BoxLabel의 인덱스 설정
private final int CUP = 0;
private final int COFFEE = 1;
private final int WATER = 2;
private final int SUGAR = 3;
private final int CREAM = 4;
//각 재료등의 이름 문자열, 레이블로 만들어 총 밑에 출력함
private String[] boxNames = {"Cup", "Coffee", "Water", "Sugar", "Cream"};
//커피잔의 이미지 객체
private ImageIcon coffeeIcon = new ImageIcon("img/cup.jpg");
public CoffeePanel() {
setLayout(null);
for(int i=0; i<boxes.length; i++) {
boxes[i] = new BoxLabel();
boxes[i].setLocation(30+80*i, 10);
boxes[i].setSize(40, 100);
JLabel text = new JLabel(boxNames[i]);
text.setLocation(30+80*i, 120);
text.setSize(50, 30);
add(boxes[i]);
add(text);
}
coffeeImageLabel = new JLabel();
coffeeImageLabel.setLocation(180, 200);
coffeeImageLabel.setSize(coffeeIcon.getIconWidth(), coffeeIcon.getIconHeight());
add(coffeeImageLabel);
}
public void operate(String cmd) { //자판기 시뮬레이터의 핵심 처리 루틴
if(cmd.equals("블랙커피")) {
if(boxes[CUP].isEmpty() || boxes[COFFEE].isEmpty() || boxes[WATER].isEmpty()) {
error("부족한 것이 있습니다. 채워주세요.");
return;
}
else {
boxes[CUP].consume();
boxes[COFFEE].consume();
boxes[WATER].consume();
}
}else if(cmd.equals("설탕커피")) {
if(boxes[CUP].isEmpty() || boxes[COFFEE].isEmpty() || boxes[WATER].isEmpty() || boxes[SUGAR].isEmpty()) {
error("부족한 것이 있습니다. 채워주세요.");
return;
}
else {
boxes[CUP].consume();
boxes[COFFEE].consume();
boxes[WATER].consume();
boxes[SUGAR].consume();
}
}else if(cmd.equals("다방커피")) {
if(boxes[CUP].isEmpty() || boxes[COFFEE].isEmpty() || boxes[WATER].isEmpty() || boxes[SUGAR].isEmpty() || boxes[CREAM].isEmpty()) {
error("부족한 것이 있습니다. 채워주세요.");
return;
}
else {
boxes[CUP].consume();
boxes[COFFEE].consume();
boxes[WATER].consume();
boxes[SUGAR].consume();
boxes[CREAM].consume();
}
}else{ //reset
boxes[CUP].reset();
boxes[COFFEE].reset();
boxes[WATER].reset();
boxes[SUGAR].reset();
boxes[CREAM].reset();
repaint(); // boxes[]의 레이블 컴포넌트에 변화가 생겼으므로 부모에게 다시 그리도록 한다.
return;
}
repaint(); // boxes[]의 레이블 컴포넌트에 변화가 생겼으므로 부모에게 다시 그리도록 한다.
// 성공적으로 커피가 나오게 되었으므로 커피잔을 출력한다.
this.coffeeImageLabel.setIcon(coffeeIcon);
// 커피가 나왔음을 알려주는 메시지 창을 출력한다.
JOptionPane.showMessageDialog(CoffeePanel.this, "뜨거워요, 즐거운 하루~~", "커피나왔습니다.", JOptionPane.INFORMATION_MESSAGE);
// 커피잔 이미지를 지운다.
coffeeImageLabel.setIcon(null);
}
public void error(String msg) { // 경고창을 출력하는 메소드
JOptionPane.showMessageDialog(CoffeePanel.this, msg, "커피 자판기 경고", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
//실행버튼을 담당하는 패널 - South
class ButtonPanel extends JPanel{
private JButton[] coffeeButtons = new JButton[4]; //3개의 커피 선택과 reset버튼
private String[] names = {"블랙커피", "설탕커피", "다방커피", "초기화"};
public ButtonPanel() {
for(int i=0; i<coffeeButtons.length; i++) {
coffeeButtons[i] = new JButton(names[i]);
add(coffeeButtons[i]);
coffeeButtons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
coffeeMachinePanel.operate(e.getActionCommand());
}
});
}
}
}
class BoxLabel extends JLabel{
private final int MAXSIZE = 10; //통 사지느
private int currentSize; //현재 통에 들어 있는 재료의 양
public BoxLabel() {
currentSize = MAXSIZE;
}
boolean consume() {
if(currentSize > 0) {
currentSize--;
return true;
}else {
return false;
}
}
void reset() { //통에 있는 재료를 초기 상태로 돌림
currentSize = MAXSIZE;
}
boolean isEmpty() {
return currentSize == 0;
}
// 통에 남아 있는 재료의 양을 보여주기 위해 작성됨
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// LIGHT_GRAY 색으로 남아 있는 양을 보여 주기위해 그린다.
g.setColor(Color.LIGHT_GRAY);
int y = this.getHeight() - (currentSize*this.getHeight() / MAXSIZE);
g.fillRect(0, y, this.getWidth(), this.getHeight() - y);
// GRAY 색으로 통의 외곽선을 그린다.
g.setColor(Color.GRAY);
g.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
}
}
public static void main(String[] args) {
new CoffeeMachineEx();
}
}
728x90
반응형
'JAVA > 예제' 카테고리의 다른 글
[API] 영화진흥위원회 오픈API 사용 (0) | 2023.01.01 |
---|---|
[JAVA/API] JAVA에서 공공 API 요청 및 출력하기 (0) | 2023.01.01 |
[JAVA] 숫자 순서대로 클릭하기 게임 (0) | 2022.10.28 |
[JAVA] Graphics 미니그림판 (0) | 2022.10.28 |
[JAVA] Graphics 웃는 얼굴 (0) | 2022.10.28 |
댓글