Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.awt.Graphics2D;
  2. import java.awt.GridLayout;
  3. import java.awt.Toolkit;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.imageio.ImageIO;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.SwingUtilities;
  16.  
  17. public class Test {
  18.  
  19. private JFrame frame;
  20. private JLabel[] labels;
  21. private static String imagePath = "c:/test.jpg";
  22. private final int rows = 3; //You should decide the values for rows and cols variables
  23. private final int cols = 3;
  24. private final int chunks = rows * cols;
  25. private final int SPACING = 10;//spacing between split images
  26.  
  27. public static void main(String[] args) {
  28. SwingUtilities.invokeLater(new Runnable() {
  29.  
  30. @Override
  31. public void run() {
  32. new Test().createAndShowUI();
  33. }
  34. });
  35. }
  36.  
  37. private void createAndShowUI() {
  38. frame = new JFrame("Test");
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. initComponents();
  41. frame.setResizable(false);
  42. frame.pack();
  43. frame.setVisible(true);
  44. }
  45.  
  46. private void initComponents() {
  47.  
  48. BufferedImage[] imgs = getImages();
  49.  
  50. //set contentpane layout for grid
  51. frame.getContentPane().setLayout(new GridLayout(rows, cols, SPACING, SPACING));
  52.  
  53. labels = new JLabel[imgs.length];
  54.  
  55. //create JLabels with split images and add to frame contentPane
  56. for (int i = 0; i < imgs.length; i++) {
  57. labels[i] = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgs[i].getSource())));
  58. frame.getContentPane().add(labels[i]);
  59. }
  60. }
  61.  
  62. private BufferedImage[] getImages() {
  63. File file = new File(imagePath); // I have bear.jpg in my working directory
  64. FileInputStream fis = null;
  65. try {
  66. fis = new FileInputStream(file);
  67. } catch (FileNotFoundException ex) {
  68. Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  69. }
  70. BufferedImage image = null;
  71. try {
  72. image = ImageIO.read(fis); //reading the image file
  73. } catch (IOException ex) {
  74. Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  75. }
  76. int chunkWidth = image.getWidth() / cols; // determines the chunk width and height
  77. int chunkHeight = image.getHeight() / rows;
  78. int count = 0;
  79. BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks
  80. for (int x = 0; x < rows; x++) {
  81. for (int y = 0; y < cols; y++) {
  82. //Initialize the image array with image chunks
  83. imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
  84.  
  85. // draws the image chunk
  86. Graphics2D gr = imgs[count++].createGraphics();
  87. gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
  88. gr.dispose();
  89. }
  90. }
  91. return imgs;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement