728x90
반응형
package gui;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class S14_Image {
public static void main(String[] args) {
JFrame f = new JFrame("Image...");
JLabel image1 = new JLabel();
JLabel image2 = new JLabel();
JLabel image3 = new JLabel();
JLabel image4 = new JLabel();
image1.setIcon(new ImageIcon("files/images/동석2.jpg"));
image2.setIcon(new ImageIcon("files/images/동석이.jpg"));
image3.setIcon(new ImageIcon("files/images/동석이.jpg"));
image4.setIcon(new ImageIcon("files/images/동석2.jpg"));
try {
// 이미지 로딩
BufferedImage bufferedImage = ImageIO.read(new File("files/images/동석2.jpg"));
// getScaledInstance() : 크기 조절된 이미지 인스턴스를 반환
Image scaledImage = bufferedImage.getScaledInstance(250, 250, Image.SCALE_AREA_AVERAGING);
// getSubimage() : 원본에서 원하는 일부 이미지를 잘라서 반환
bufferedImage.getSubimage(150, 150, 50, 100);
image2.setIcon(new ImageIcon(scaledImage));
} catch (IOException e) {
e.printStackTrace();
}
f.add(image1);
f.add(image2);
f.add(image3);
f.add(image4);
f.setLayout(new GridLayout(2, 2));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 500, 500);
f.setVisible(true);
}
}
728x90
반응형
'JAVA > Swing' 카테고리의 다른 글
[JAVA] FlowLayout (0) | 2023.05.02 |
---|---|
[JAVA] JPanel (0) | 2023.05.02 |
[JAVA] GridLayout (0) | 2023.05.02 |
[JAVA] JComboBox (0) | 2023.05.02 |
[JAVA] JRadioButton (0) | 2023.05.02 |