Language/Java

[Java] 실습 (event) 상 하 좌 우키로 글자 움직이기

arajo 2022. 8. 18. 17:36
728x90
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class RunningTextExam extends JFrame {
	JPanel 	contentPane	= new JPanel();
	JLabel	la		= new JLabel("Hello");
	final int RUNNING_UNIT	= 10;
	
	RunningTextExam() {
		setTitle("상 하 좌 우 키를 이용하여 글자를 움직이기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.addKeyListener(new MyKeyListener());
		
		la.setLocation(50, 50);
		la.setSize(30, 20);
		
		la.setOpaque(true);
		la.setBackground(Color.CYAN);
		contentPane.add(la);
		
		setSize(800, 600);
		setVisible(true);

		Insets jf = this.getInsets();	
		System.out.println(contentPane.getWidth() + ":" + contentPane.getHeight());
		System.out.println("JFrame Top   :" + jf.top);
		System.out.println("JFrame Left  :" + jf.left);
		System.out.println("JFrame Right :" + jf.right);
		System.out.println("JFrame Bottom:" + jf.bottom);
		
		// JFrame의 setVisible() 메서드가 실행된 후에 특정 컴포넌트에 포커스를 주어야 한다.
		contentPane.setFocusable(true);
		contentPane.requestFocus();
	}
	class MyKeyListener extends KeyAdapter {
		//------------------------------------------------------------------------------------------
		// 방향키를 눌렀을 경우 처리해야 하는 이벤트
		//------------------------------------------------------------------------------------------
		public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			
			// System.out.println(la.getWidth() + ":" + la.getHeight());
			// 실제로 라벨이 움직일 수 있는 공간 == Insets 부분을 뺀 공간
			int runWidth = contentPane.getWidth() - la.getWidth();
			int runHeight = contentPane.getHeight() - la.getHeight();
			
			switch(keyCode) {
				case KeyEvent.VK_UP:
					if(la.getY() > 0) {
						if(la.getY() >= RUNNING_UNIT) // RUNNING_UNIT 만큼 움직일 수 있는 공간이 있는 경우
							la.setLocation(la.getX(), la.getY() - RUNNING_UNIT);
						else
							la.setLocation(la.getX(), 0); // 짜투리 공간
					}
					getPosition();
					break;
				case KeyEvent.VK_DOWN:
					if(la.getY() < runHeight) {
						if(runHeight - la.getY() < RUNNING_UNIT) // 짜투리 공간
							la.setLocation(la.getX(), la.getY() + (runHeight - la.getY()));
						else
							la.setLocation(la.getX(), la.getY() + RUNNING_UNIT);
					}
					getPosition();
					break;
				case KeyEvent.VK_LEFT:
					if(la.getX() > 0) {
						if(la.getX() >= RUNNING_UNIT)
							la.setLocation(la.getX() - RUNNING_UNIT, la.getY());
						else
							la.setLocation(0, la.getY());
					}
					getPosition();
					break;
				case KeyEvent.VK_RIGHT:
					if(la.getX() < runWidth) {
						if(runWidth - la.getX() < RUNNING_UNIT)
							la.setLocation(la.getX() + (runWidth - la.getX()), la.getY());
						else
							la.setLocation(la.getX() + RUNNING_UNIT, la.getY());
					}
					getPosition();
					break;
			}
		}  // End - public void keyPressed(KeyEvent e)
		
		//------------------------------------------------------------------------------------------
		// 방향키를 누른 후의 좌표값을 알려주는 메서드
		//------------------------------------------------------------------------------------------
		void getPosition() {
			System.out.println(la.getX() + ", " + la.getY()); // 라벨의 현재 위치
		}
		
	} // End - class MyKeyListener extends KeyAdapter
	
	public static void main(String[] args) {
		new RunningTextExam();

	}

}

실행결과
움직일 때마다 좌표