How to Create an Image Slicer In Java Netbeans
In this Java Tutorial we will see How To Slice an image into parts in java netbeans.
This program lets you upload an image and automatically splits it into a 4x4 (you can change it to more or less) grid of smaller images. Perfect for creating puzzles with image.
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package new_tutorials;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author 1BestCsharp
*/
public class ImageSlicer extends JFrame{
// Declare a panel to hold the grid of image slices
private JPanel gridPanel;
// Constants for window and image properties
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
// Target size for resizing the image
private static final int IMAGE_SIZE = 400;
// 4x4 grid (16 image slices in total)
private static final int GRID_SIZE = 5;
public ImageSlicer(){
// Set up the main window properties
setTitle("Image Splitter");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
// Create the "Upload and Split Image" button
JButton uploadButton = new JButton("Upload and Split Image");
uploadButton.setFocusPainted(false);
uploadButton.setBackground(new Color(33,150,243));
uploadButton.setForeground(Color.WHITE);
uploadButton.setBorder( BorderFactory.createEmptyBorder(10,20,10,20));
uploadButton.setFont(new Font("Arial", Font.PLAIN, 17));
uploadButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Create a panel to hold the grid of image slices (initially empty)
// Use GridLayout to divide the panel into a 4x4 grid, no gaps between cells
gridPanel = new JPanel(new GridLayout(GRID_SIZE, GRID_SIZE, 0, 0));
gridPanel.setBackground(Color.WHITE);
// Add the grid panel inside a scroll pane in case the content exceeds the window size
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
// Add an action listener to the button (what happens when it's clicked)
uploadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Images Files", ImageIO.getReaderFileSuffixes()));
int returnValue = fileChooser.showOpenDialog(null);
if(returnValue == JFileChooser.APPROVE_OPTION){
try {
File selectedFile = fileChooser.getSelectedFile();
BufferedImage image = ImageIO.read(selectedFile);
// Resize the image to the specified size (400x400 pixels)
BufferedImage reseizedImage = resizeImage(image, IMAGE_SIZE, IMAGE_SIZE);
// Split the resized image into 16 (4x4) slices and display them
splitAndDisplay(reseizedImage);
} catch (IOException ex) {
Logger.getLogger(ImageSlicer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.add(uploadButton);
add(panel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
// Function to resize the image to the target width and height
private BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight){
Image resizedImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
BufferedImage bufferedResizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedResizedImage.createGraphics();
g2d.drawImage(resizedImage, 0, 0, null);
g2d.dispose();
return bufferedResizedImage;
}
// Function to split the image into slices and display them in the grid
private void splitAndDisplay(BufferedImage image){
int sliceWidth = image.getWidth() / GRID_SIZE;
int sliceHeight = image.getHeight() / GRID_SIZE;
gridPanel.removeAll();
for(int y = 0; y < GRID_SIZE; y++){
for(int x = 0; x < GRID_SIZE; x++){
BufferedImage slice = image.getSubimage(x*sliceWidth, y*sliceHeight, sliceWidth, sliceHeight);
ImageIcon imageIcon = new ImageIcon(slice);
JLabel sliceLabel = new JLabel(imageIcon);
sliceLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
gridPanel.add(sliceLabel);
}
}
gridPanel.revalidate();
gridPanel.repaint();
}
public static void main(String[] args) {
new ImageSlicer().setVisible(true);
}
}
The Final Result:
More Java Projects:
Download Projects Source Code