In my Java code, I am trying to browse an Image from my directory and then am trying to access to that selected image and capture an area (sub Image) from my image. I have some problems that once I select the image, I do not have access to the image (ImageIcon or BufferedImage). Because, I need its information to get a subimage out of it.
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.*; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class selectWindow extends JFrame{ // In this GUI project we need just one JPanel inside the JFrame private JPanel imagePanel; private JPanel buttonPanel; private JButton selectImage; private JLabel imageLabel; File targetFile; private ImageIcon image; static BufferedImage targetImg; private static final String basePath = "C:\\Users\\mroozbahani3\\Desktop"; private static final int baseSizeX = 900-110; private static final int baseSizeY = 660; Rectangle captureRect; File file; // Now let's make the constructor public selectWindow(){ // The below code will close the window(JFrame), once we have click on window exit setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //lets define the default size of our window page(main JFrame), where the first value is the x and second one is y; setSize(900,660); selectImage = new JButton("Select Image"); selectImage.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ image = selectButtonActionPerformed(e); } }); imagePanel = new JPanel(); imageLabel = new JLabel(image); imagePanel.add(imageLabel); // The below part is for JPanel related to the buttons buttonPanel = new JPanel(new GridBagLayout()); GridBagConstraints grid = new GridBagConstraints(); grid.insets = new Insets(10,10,10,10); // Select Buttom grid.gridx = 0; grid.gridy = 1; buttonPanel.add(selectImage,grid); // We can determine that how much of the space is going to be belonged to adjustPanel buttonPanel.setPreferredSize(new Dimension(110,660)); setLayout(new BorderLayout()); add(buttonPanel,BorderLayout.WEST); add(imagePanel,BorderLayout.EAST); setResizable( false ); } private ImageIcon selectButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = new JFileChooser(basePath); fc.setFileFilter(new ImageFileFilter()); int res = fc.showOpenDialog(null); // We have an image! try { if (res == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); targetImg = rescale(ImageIO.read(file)); return new ImageIcon(targetImg); } // Oops! else { JOptionPane.showMessageDialog(null, "You must select one image to be the reference.", "Aborting...", JOptionPane.WARNING_MESSAGE); } } catch (Exception iOException) { } return new ImageIcon(targetImg); } public BufferedImage rescale(BufferedImage originalImage) { BufferedImage resizedImage = new BufferedImage(baseSizeX, baseSizeY, BufferedImage.TYPE_INT_RGB); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, baseSizeX, baseSizeY, null); g.dispose(); return resizedImage; } public static void main(String[] args) throws AWTException{ JFrame gui = new selectWindow(); gui.setVisible(true); } }