fork download
  1. /* package whatever; // don't place package name! */
  2. package java_hw_01;
  3.  
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import java.awt.Graphics2D;
  7. import java.awt.GridLayout;
  8. import java.awt.Image;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.image.BufferedImage;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javax.imageio.ImageIO;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JButton;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.border.LineBorder;
  26.  
  27.  
  28. public class divide_image extends JFrame {
  29.  
  30. public JFrame frame;
  31. public JButton[] button;
  32. public static String imagePath = "/Users/chulgu/Documents/Admin_s_folder/background/Sculpture-Liberty-Wallpaper-HD.jpg";
  33. public final int rows = 16; //You should decide the values for rows and cols variables
  34. public final int cols = 16;
  35. public final int chunks = rows * cols;
  36. public final int SPACING = 0;//spacing between split images
  37.  
  38. public static void main(String[] args) {
  39. SwingUtilities.invokeLater(new Runnable() {
  40.  
  41. @Override
  42. public void run() {
  43. new divide_image().createAndShowUI();
  44. }
  45. });
  46. }
  47.  
  48. void createAndShowUI() {
  49. frame = new JFrame("divide_16*16_image");
  50. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. initComponents();
  52. frame.setResizable(false);
  53. frame.pack();
  54. frame.setVisible(true);
  55. }
  56.  
  57. private void initComponents() {
  58. BufferedImage[] imgs = getImages();
  59.  
  60. //set contentpane layout for grid
  61. frame.getContentPane().setLayout(new GridLayout(rows, cols, SPACING, SPACING));
  62.  
  63. button = new JButton[imgs.length];
  64.  
  65. //create JButtons with split images and add to frame contentPane
  66. for (int i = 0; i < imgs.length; i++) {
  67. button[i] = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgs[i].getSource())));
  68. button[i].setBorder(new LineBorder(Color.BLACK)); // paint button's border
  69. frame.getContentPane().add(button[i]);
  70.  
  71. }
  72. }
  73.  
  74. private void image_buff() {
  75. // TODO Auto-generated method stub
  76.  
  77. }
  78.  
  79. private BufferedImage[] getImages() {
  80. File file = new File(imagePath); // I have bear.jpg in my working directory
  81. FileInputStream fis = null;
  82. try {
  83. fis = new FileInputStream(file);
  84. } catch (FileNotFoundException ex) {
  85. Logger.getLogger(divide_image.class.getName()).log(Level.SEVERE, null, ex);
  86. }
  87. BufferedImage image = null;
  88. try {
  89. image = ImageIO.read(fis); //reading the image file
  90. } catch (IOException ex) {
  91. Logger.getLogger(divide_image.class.getName()).log(Level.SEVERE, null, ex);
  92. }
  93. int chunkWidth = image.getWidth() / cols; // determines the chunk width and height
  94. int chunkHeight = image.getHeight() / rows;
  95. int count = 0;
  96. BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks
  97. for (int x = 0; x < rows; x++) {
  98. for (int y = 0; y < cols; y++) {
  99. //Initialize the image array with image chunks
  100. imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
  101.  
  102. // draws the image chunk
  103. Graphics2D gr = imgs[count++].createGraphics();
  104. gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
  105. gr.dispose();
  106. }
  107. }
  108. return imgs;
  109. }
  110. }
  111.  
  112.  
  113. 제이프레임으로 짠 소스입니다......
  114.  
  115. 일단 여기까진 이미지가 256개로 잘 분할이 됩니다.
  116.  
  117. 근데 이걸 버튼을 하나 누르면 모자이크 효과가 일어나고 다시 누르면 원상 복구가 되야합니다....
  118.  
  119. 문제는 액션을 어떻게 넣어야 하는지 이틀 밤을 새서 고민했는데....
  120.  
  121. 구글에도 다 소스가 다르고 하니까 멘탈이 다 터지더라고요....
  122.  
  123. 성님들... 정말 한번 멘탈좀 회복시켜주세요...
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:113: error: class, interface, or enum expected
??????? ? ?????......
^
1 error
stdout
Standard output is empty