심심해서 만든... 자바 뷰어 ㅋㅋ

2014. 5. 20. 18:05개인프로젝트

허접하지만.. 봐줄만한.. ㅋㅋㅋ


실행파일 : 

chview.exe





package com;


import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;


import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.filechooser.FileNameExtensionFilter;


class MyLabel extends JFrame implements ActionListener {

JLabel laBel = new JLabel();

JFileChooser fileOpen = new JFileChooser();

JButton btn = new JButton("openImage");

ImageIcon imgIcon = null;

Dimension dimen = Toolkit.getDefaultToolkit().getScreenSize();


public MyLabel() {

super("CHViewer");

getContentPane().setLayout(new FlowLayout());

add(btn);

add(laBel);


btn.addActionListener(this);

fileOpen.setFileFilter(new FileNameExtensionFilter("Image files",ImageIO.getReaderFileSuffixes()));

fileOpen.setMultiSelectionEnabled(false);


this.setSize(250, 100);

setLocation((dimen.width / 2) - 250, dimen.height / 2);

this.setVisible(true);

}


public void actionPerformed(ActionEvent event) {

fileOpen.showOpenDialog(this);

try {

File file = new File(fileOpen.getSelectedFile().toString());

BufferedImage bimg = ImageIO.read(file);

int type = bimg.getType() == 0? BufferedImage.TYPE_INT_ARGB : bimg.getType();

BufferedImage resizeImage = resizeImage(bimg, type,dimen.width/2,dimen.height/2);

imgIcon = new ImageIcon(resizeImage);

laBel.setIcon(imgIcon);

this.setSize(resizeImage.getWidth()+100, resizeImage.getHeight()+100);

setLocation(150, 110);

} catch (IOException e) {

e.printStackTrace();

}

}

private static BufferedImage resizeImage(BufferedImage bimg, int type, int width, int height){

BufferedImage resizedImage = new BufferedImage(width, height, type);

Graphics2D g = resizedImage.createGraphics();

g.drawImage(bimg, 0, 0, width, height, null);

g.dispose();

 

return resizedImage;

    }

 

}