mojo's Blog

명품 JAVA Programming 제 14장 Open Challenge 본문

Java

명품 JAVA Programming 제 14장 Open Challenge

_mojo_ 2021. 8. 18. 16:09

커피 자판기 시뮬레이터 Code 

 

package Part14;

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;

public class OpenChallenge14 extends JFrame{

	private NorthPanel np= new NorthPanel();
	private CenterPanel cp = new CenterPanel();
	private SouthPanel sp = new SouthPanel();
	
	class NorthPanel extends JPanel{
		private JLabel jl = new JLabel("Welcome, Hot Coffee!!!");
		
		NorthPanel(){
			setBackground(Color.MAGENTA);
			setLayout(new FlowLayout(FlowLayout.CENTER));
			add(jl);
		}
	}
	
	class CenterPanel extends JPanel{
		private ImageIcon icon[] = {
				new ImageIcon("C:\\\\Temp/lion.gif"),
				new ImageIcon("C:\\\\Temp/lion.gif"),
				new ImageIcon("C:\\\\Temp/lion.gif")
		};
		private Image img[] = new Image[3];
		private int []value = new int[5];
		private int []dx = {-150, 0, 150};
		
		CenterPanel(){
			for(int i=0; i<img.length; i++) {
				img[i] = icon[i].getImage();
			}
			
			for(int i=0; i<value.length; i++) {
				value[i] += 100;
			}
		}
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			
			for(int i=0; i<value.length; i++) {
				g.setColor(Color.GRAY);
				g.drawRect(60 + i*100, 25, 50, 125);
				g.setColor(Color.LIGHT_GRAY);
				g.fillRect(60 + i*100, 150 - (int)(1.25*value[i]), 50, (int)(value[i] * 1.25));
			}
			
			
			for(int i=0; i<img.length; i++) {
				int x = getWidth()/2 - 50;
				int y = getHeight()/2 + 30;
				g.drawImage(img[1], x + dx[i], y, 100, 100, this);	
			}
		}
		
		int []getValue(){
			return value;
		}
		
	}
	
	class SouthPanel extends JPanel{
		private JButton []jBtn = new JButton[4];
		private String []text = { "Black Coffee", "Sugar Coffee", 
				"Dabang Coffee", "Reset" }; 
		
		SouthPanel(){
			setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
			for(int i=0; i<4; i++) {
				jBtn[i] = new JButton(text[i]);
				jBtn[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						JButton jb = (JButton)(e.getSource());
						String s = jb.getText();
						int []value = cp.getValue();
						for(int i=0; i<value.length; i++) {
							System.out.println("value"+(i+1)+" = "+value[i]);
						}
						
						if(s.equals(text[0])) {
							if(value[0] == 0 || value[1] == 0 || value[2] == 0) {
								JOptionPane.showMessageDialog(null, "부족한 것이 있습니다. 채워주세요.",
										"커피 자판기 경고", JOptionPane.DEFAULT_OPTION);
							}
							else {
								for(int i=0; i<=2 ;i++) value[i] -= 10;
								JOptionPane.showMessageDialog(null, "뜨거워요. 즐거운 하루~",
										"커피 나왔습니다.", JOptionPane.DEFAULT_OPTION);
							}
						}
						else if(s.equals(text[1])) {
							if(value[0] == 0 || value[1] == 0 || value[2] == 0 || value[3] == 0 || value[4] == 0) {
								JOptionPane.showMessageDialog(null, "부족한 것이 있습니다. 채워주세요.",
										"커피 자판기 경고", JOptionPane.DEFAULT_OPTION);
							}
							else {
								for(int i=0; i<=4 ;i++) value[i] -= 10;
								JOptionPane.showMessageDialog(null, "뜨거워요. 즐거운 하루~",
										"커피 나왔습니다.", JOptionPane.DEFAULT_OPTION);
							}
						} 
						else if(s.equals(text[2])) {
							if(value[0] == 0 || value[1] == 0 || value[2] == 0) {
								JOptionPane.showMessageDialog(null, "부족한 것이 있습니다. 채워주세요.",
										"커피 자판기 경고", JOptionPane.DEFAULT_OPTION);
							}
							else {
								for(int i=0; i<=2 ;i++) value[i] -= 10;
								JOptionPane.showMessageDialog(null, "뜨거워요. 즐거운 하루~",
										"커피 나왔습니다.", JOptionPane.DEFAULT_OPTION);
							}
						} 
						else {
							for(int i=0; i<=4; i++) {
								value[i] = 100;
							}
						} 
					}
				});
				add(jBtn[i]);
			}
		}
	}
	
	OpenChallenge14(){
		setTitle("14장 연습하기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		c.add(np, BorderLayout.NORTH);
		c.add(cp, BorderLayout.CENTER);
		c.add(sp, BorderLayout.SOUTH);
		
		setSize(600, 400); 
		setVisible(true); 
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new OpenChallenge14();
	}

}

Comments