/* package whatever; // don't place package name! */
package java_hw_01;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class divide_image
extends JFrame {
public static String imagePath
= "/Users/chulgu/Documents/Admin_s_folder/background/Sculpture-Liberty-Wallpaper-HD.jpg"; public final int rows = 16; //You should decide the values for rows and cols variables
public final int cols = 16;
public final int chunks = rows * cols;
public final int SPACING = 0;//spacing between split images
public static void main
(String[] args
) {
@Override
public void run() {
new divide_image().createAndShowUI();
}
});
}
void createAndShowUI() {
frame
= new JFrame("divide_16*16_image"); frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); initComponents();
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
private void initComponents() {
//set contentpane layout for grid
frame.
getContentPane().
setLayout(new GridLayout(rows, cols, SPACING, SPACING
));
//create JButtons with split images and add to frame contentPane
for (int i = 0; i < imgs.length; i++) {
frame.getContentPane().add(button[i]);
}
}
private void image_buff() {
// TODO Auto-generated method stub
}
File file
= new File(imagePath
); // I have bear.jpg in my working directory try {
Logger.getLogger(divide_image.class.getName()).log(Level.SEVERE, null, ex);
}
try {
image = ImageIO.read(fis); //reading the image file
Logger.getLogger(divide_image.class.getName()).log(Level.SEVERE, null, ex);
}
int chunkWidth = image.getWidth() / cols; // determines the chunk width and height
int chunkHeight = image.getHeight() / rows;
int count = 0;
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
//Initialize the image array with image chunks
imgs
[count
] = new BufferedImage(chunkWidth, chunkHeight, image.
getType());
// draws the image chunk
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
gr.dispose();
}
}
return imgs;
}
}
제이프레임으로 짠 소스입니다......
일단 여기까진 이미지가 256개로 잘 분할이 됩니다.
근데 이걸 버튼을 하나 누르면 모자이크 효과가 일어나고 다시 누르면 원상 복구가 되야합니다....
문제는 액션을 어떻게 넣어야 하는지 이틀 밤을 새서 고민했는데....
구글에도 다 소스가 다르고 하니까 멘탈이 다 터지더라고요....
성님들... 정말 한번 멘탈좀 회복시켜주세요...