Java/Excercise

swing ch05 EventListenerEx9

낭구리 2021. 9. 7. 17:45
package ch05;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyFrame9 extends JFrame implements KeyListener {

	BufferedImage backgroundImage;
	BufferedImage imageIcon;
	MyImagePanel imagePanel;
	
	int xPoint = 200;
	int yPoint = 200;

	public MyFrame9() {
		initData();
		setInitLayout();
		addEventListener();
	}

	private void initData() {
		setTitle("이미지 백그라운드 연습");
		setSize(500, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		try {
			backgroundImage = ImageIO.read(new File("background2.jpg"));
			imageIcon = ImageIO.read(new File("among1.jpg"));
			System.out.println("파일을 정상적으로 가지고왔습니다");
		} catch (IOException e) {

			e.printStackTrace();
		}
		imagePanel = new MyImagePanel();
		
	}

	private void setInitLayout() {
		setVisible(true);
		add(imagePanel);

	}

	private void addEventListener() {
		this.addKeyListener(this);
	}

//내부클래스
	private class MyImagePanel extends JPanel {
		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(backgroundImage, 0, 0, 500, 500, null);
			g.drawImage(imageIcon, xPoint, yPoint, 100, 100, null);
			System.out.println("Log : paintComponent 메서드 실행");
		}
	}// class

	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void keyPressed(KeyEvent e) {
	    int KeyCode = e.getKeyCode();
	    System.out.println("KeyCode : " +KeyCode);
	    if(KeyCode == KeyEvent.VK_UP ) {
	    	//삼항 연산자 변수 = (조건식) ? 식1:식2
	    	yPoint = (yPoint <0) ? 0 : yPoint -10;  
//	    	yPoint -= 10;
	    }else if (KeyCode == KeyEvent.VK_DOWN) {
	    	yPoint = (yPoint <0) ? 0 : yPoint +10;
//	    	yPoint += 10;
	    }else if(KeyCode == KeyEvent.VK_LEFT) {
	    	xPoint = (xPoint <0) ? 0 : xPoint -10;
//	    	xPoint -= 10;
	    }else if(KeyCode == KeyEvent.VK_RIGHT) {
	    	xPoint = (xPoint <0) ? 0 : xPoint +10;
//	    	xPoint += 10;
	    }
	    repaint(); //paint 다시호출
	}// 콜백

	@Override
	public void keyReleased(KeyEvent e) {}

}

public class EventListenerEx9 {

	public static void main(String[] args) {

		new MyFrame9();
	}

}

'Java > Excercise' 카테고리의 다른 글

thread ch01 ThreadTest2  (0) 2021.09.07
thread ch01 ThreadTest1  (0) 2021.09.07
swing ch05 EventListenerEx8  (0) 2021.09.07
swing ch05 EventListenerEx7  (0) 2021.09.07
swing ch05 EventListenerEx6  (0) 2021.09.07