Adding visualization of big image. WARNING Image displayed can be bigger than screen. Need to scroll, zoom or resize to fit to screen

This commit is contained in:
Andy Chabalier 2018-09-27 19:18:07 +02:00
parent 48d8d3a411
commit ab350eaecd
3 changed files with 71 additions and 2 deletions

View File

@ -5,14 +5,19 @@ import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
public final class ImageLib {
private static HashMap<Image,File> save = new HashMap<Image,File>();
public static Image createScaledImage(File file, int iconWidth, int iconHeight) {
final BufferedImage img;
try {
if ((img = ImageIO.read(file)) != null) {
return img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT);
Image image = img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT);
save.put(image, file);
return image;
}
} catch (IOException e) {
e.printStackTrace();
@ -21,4 +26,8 @@ public final class ImageLib {
}
private ImageLib() {}
public static File getFileFromImage(Image img) {
return save.get(img);
}
}

View File

@ -0,0 +1,44 @@
package fr.uha.gabalier.view;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import fr.uha.gabalier.util.ImageLib;
public class BigView extends JPanel {
private static final long serialVersionUID = 1197679995105162808L;
private BufferedImage image;
public BigView(Image image) {
setLayout(new BorderLayout(0, 0));
JPanel imageDisplay = new JPanel();
add(imageDisplay, BorderLayout.SOUTH);
try {
this.image = ImageIO.read(ImageLib.getFileFromImage(image));
} catch (IOException e) {
e.printStackTrace();
}
JLabel imageName = new JLabel(ImageLib.getFileFromImage(image).getName());
imageName.setEnabled(false);
imageName.setHorizontalAlignment(SwingConstants.LEADING);
add(imageName, BorderLayout.NORTH);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(this.image, 0, 0, null);
}
}

View File

@ -10,6 +10,9 @@ import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import fr.uha.gabalier.util.languages.StringSources;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class Preview extends JFrame {
@ -25,6 +28,7 @@ public class Preview extends JFrame {
private List<Image> model;
private ImageLoader loader;
private ImageView imageView;
protected ImageController<?> controller;
// UI elements
private JMenuItem loadItem;
@ -33,11 +37,15 @@ public class Preview extends JFrame {
private JButton leftNavButton;
private final JTextField statusText;
public Preview() throws HeadlessException {
setIconImage(Toolkit.getDefaultToolkit().getImage(Preview.class.getResource("/fr/uha/gabalier/util/ressources/logo.jpg")));
setTitle(StringSources.getString("Preview.this.title")); //$NON-NLS-1$
model = new ArrayList<>();
this.controller = new ImageController(model);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// UI elements
@ -60,7 +68,15 @@ public class Preview extends JFrame {
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
imageView = new ImageView(model);
final ImageController controller = new ImageController(model);
imageView.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFrame bigViewFrame = new JFrame();
bigViewFrame.add(new BigView(model.get(controller.getIndex())));
bigViewFrame.repaint();
bigViewFrame.setVisible(true);
}
});
imageView.setController(controller);
controller.setView(imageView);
getContentPane().add(imageView);