Java/Excercise

swing ch04 MainTest

낭구리 2021. 9. 3. 17:08
package ch04;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

class ImagePanel extends JPanel {
	private Image image; 
	public ImagePanel() {
		image = new ImageIcon("image1.jpg").getImage(); 
	}
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
	}
}

class MyFrame extends JFrame {
	
	// 멤버 변수 ImagePanel imagePanel
	ImagePanel imagePanel;
	
	
	public MyFrame() {
		initData();
		setInitLayout();
	}
	private void initData() {
		// 기본 코드 작성 
		setTitle("이미지 패널 연습");
		setSize(500, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		imagePanel = new ImagePanel(); 
		// 500, 500
		// ImagePanel 메모리 올리기 
	}
	private void setInitLayout() {
		// 기본코드
		setVisible(true);
		add(imagePanel);
	}
}

public class MainTest1 {
	public static void main(String[] args) {
		new MyFrame(); 
	}
}

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

generic ch05 Point  (0) 2021.09.06
swing ch04 MainTest2  (0) 2021.09.03
swing ch03 MyFrame2  (0) 2021.09.03
swing ch03 Jpanel  (0) 2021.09.03
generic ch04 Material  (0) 2021.09.03