Reading Image to Panel
// Reading the image at startup:
try{
BufferedImage image = ImageIO.read(new java.io.File((String)DataHandler.read("Image", "./res/familyMember_" + localIndex + ".profile")));
image = ImageUtils.resizeImage(image, imagePanel.getWidth() - 4, imagePanel.getHeight() - 10);
ImageIcon icon = new ImageIcon(image);
imageLabel = new JLabel(icon);
} catch (Exception e) {
imageLabel = new JLabel("None");
}
// Generation of the image panel:
JPanel imagePanel = new JPanel();
imagePanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
imagePanel.setBounds(0, 0, 100, 100);
add(imagePanel);
JPanel box = this;
// JButton for opening/adding the image
JButton open = new JButton("Open Image");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(box);
java.io.File file = chooser.getSelectedFile();
imagePanel.removeAll();
imagePanel.revalidate();
try {
BufferedImage image = ImageIO.read(file);
image = ImageUtils.resizeImage(image, imagePanel.getWidth() - 4, imagePanel.getHeight() - 10);
ImageIcon icon = new ImageIcon(image);
imageLabel = new JLabel(icon);
imagePanel.add(imageLabel);
imagePanel.revalidate();
imagePanel.repaint();
DataHandler.save("Image", "./res/profile.profile", file.getAbsolutePath());
} catch (IOException e1) {
e1.printStackTrace();
}
imagePanel.revalidate();
imagePanel.repaint();
}
});