package CHAPTER11;

//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
//
//public class LAB12_03_KeyArrowLab extends JFrame {
//    private JPanel drawPanel;
//    private int rectX = 150, rectY = 150;
//    private final int RECT_SIZE = 50;
//    private Color rectColor = Color.BLUE;
//
//    public LAB12_03_KeyArrowLab() {
//        setTitle("Keyboard Lab Practice");
//        setSize(400, 400);
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setLocationRelativeTo(null);
//
//        drawPanel = new JPanel() {
//            @Override
//            protected void paintComponent(Graphics g) {
//                super.paintComponent(g);
//                g.setColor(rectColor);
//                g.fillRect(rectX, rectY, RECT_SIZE, RECT_SIZE);
//            }
//        };
//        add(drawPanel);
//
//        addKeyListener(new KeyAdapter() {
//            @Override
//            public void keyPressed(KeyEvent e) {
//                switch (e.getKeyCode()) {
//                    case KeyEvent.VK_UP -> rectY = Math.max(0, rectY - 10);
//                    case KeyEvent.VK_DOWN -> rectY = Math.min(getHeight() - RECT_SIZE, rectY + 10);
//                    case KeyEvent.VK_LEFT -> rectX = Math.max(0, rectX - 10);
//                    case KeyEvent.VK_RIGHT -> rectX = Math.min(getWidth() - RECT_SIZE, rectX + 10);
//                    case KeyEvent.VK_C -> rectColor = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
//                }
//                drawPanel.repaint();
//            }
//        });
//
//        setFocusable(true);
//        requestFocusInWindow();
//        setVisible(true);
//    }
//    public static void main(String[] args) {
//        new LAB12_03_KeyArrowLab();
//    }
//}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LAB_03_KeyArrowLab extends JFrame {
	private JPanel drawPanel; // 사각형을 그릴 패널
	private int rectX = 150, rectY = 150; // 사각형의 초기 위치
	private final int RECT_SIZE = 50; // 사각형 크기
	private Color rectColor = Color.BLUE; // 사각형 초기 색상

	public LAB_03_KeyArrowLab() {
		setTitle("사각형 제어 GUI");
		setSize(400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);

		drawPanel = new JPanel() {
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				g.setColor(rectColor);
				g.fillRect(rectX, rectY, RECT_SIZE, RECT_SIZE);
			}
		};
		add(drawPanel);
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_UP -> rectY = Math.max(0, rectY - 10);
				case KeyEvent.VK_DOWN -> rectY = Math.min(getHeight() - RECT_SIZE, rectY + 10);
				case KeyEvent.VK_LEFT -> rectX = Math.max(0, rectX - 10);
				case KeyEvent.VK_RIGHT -> rectX = Math.min(getWidth() - RECT_SIZE, rectX + 10);
				case KeyEvent.VK_C -> rectColor = new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
						(int) (Math.random() * 255));
				}
				drawPanel.repaint();
			}
		});

		// 키 입력을 받을 수 있도록 포커스 설정
		setFocusable(true);
		requestFocusInWindow();
		setVisible(true);
	}

	public static void main(String[] args) {
		new LAB_03_KeyArrowLab();
	}
}