ImageFrameTest

Programming/JAVA,JSP 2016. 2. 5. 15:20 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

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

class MyFrame extends JFrame implements ActionListener {
	Image image;
	JButton button;
	JPanel panel;

	public MyFrame() {
		setSize(500, 300);
		setTitle("Image Frame Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panel = new MyPanel();
		add(panel, BorderLayout.CENTER);

		button = new JButton("그림 선택 및 표시");
		button.addActionListener(this);
		add(button, BorderLayout.NORTH);

		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String file = getFile();
		if (file != null) {
			image = Toolkit.getDefaultToolkit().getImage(file);
			image = image.getScaledInstance(500, 300, Image.SCALE_SMOOTH);
			panel.repaint();
		}
	}

	private String getFile() {
		JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new ImageFilter());
		int result = fc.showOpenDialog(null);
		File file = null;
		if (result == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			return file.getPath();
		} else
			return null;
	}

	private class MyPanel extends JPanel {
		public void paint(Graphics g) {
			g.drawImage(image, 0, 0, this);
		}
	}

	private class ImageFilter extends javax.swing.filechooser.FileFilter {
		public boolean accept(File f) {
			if (f.isDirectory())
				return true;
			String name = f.getName();
			if (name.matches(".*((.jpg)|(.JPG)|(.gif)|(.GIF)|(.png))|(.PNG)"))
				return true;
			else
				return false;
		}

		public String getDescription() {
			return "이미지 파일 (*.jpg, *.gif, *.png)";
		}
	}
}

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

'Programming > JAVA,JSP' 카테고리의 다른 글

Activex Event use in JavaScript  (0) 2016.04.14
JDBC Realm  (1) 2016.03.09
Java 채팅 프로그램  (0) 2016.02.04
Java 채팅 소스 예제 #1  (0) 2016.02.04
Java CardLayout Test  (0) 2016.01.26