Updated copyright. Prefer HTTPS in documentation. Switched to LGPLv3 or later.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / sampleApplication / ImagePanel.java
index 749d558..13b1bc7 100755 (executable)
-package eu.svjatoslav.imagesqueeze.sampleApplication;
-
-import java.awt.BorderLayout;
-
-import java.awt.Dimension;
-import java.awt.image.BufferedImage;
-import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+/*
+ * Imagesqueeze - Image codec. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
 
-import javax.swing.JButton;
-
-import javax.swing.ImageIcon;
-import javax.swing.JPanel;
-import javax.swing.WindowConstants;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
+package eu.svjatoslav.imagesqueeze.sampleApplication;
 
 import eu.svjatoslav.imagesqueeze.codec.Image;
 
+import javax.swing.*;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.*;
 
 public class ImagePanel extends javax.swing.JPanel {
-       private JLabel imageLabel;
-
-       public BufferedImage bufferedImage;
-
-       /**
-        * Auto-generated main method to display this 
-        * JPanel inside a new JFrame.
-        */
-       public static void main(String[] args) {
-               JFrame frame = new JFrame();
-               frame.getContentPane().add(new ImagePanel());
-               frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-               frame.pack();
-               frame.setVisible(true);
-       }
-
-       public ImagePanel() {
-               super();
-               initGUI();
-       }
-
-       private void initGUI() {
-               try {
-                       BorderLayout thisLayout = new BorderLayout();
-                       this.setLayout(thisLayout);
-                       setPreferredSize(new Dimension(660, 500));
-                       {
-                               imageLabel = new JLabel();
-                               this.add(getImageLabel(), BorderLayout.CENTER);
-                       }
-               } catch (Exception e) {
-                       e.printStackTrace();
-               }
-       }
-
-       public JLabel getImageLabel() {
-               return imageLabel;
-       }
-
-       public void loadImage(File inputFile, boolean isImgSqz) throws IOException{
-               FileInputStream fileInputStream = new FileInputStream(inputFile);
-       
-               loadImage(fileInputStream, isImgSqz);           
-       }
-
-       public void loadImage(InputStream inputStream, boolean isImgSqz) throws IOException{
-               if (isImgSqz){
-                       // load ImageSqueeze file
-
-                       Image image = new Image();
-                       image.loadImage(inputStream);
-
-                       bufferedImage = image.bufferedImage;
-
-                       ImageIcon icon = new ImageIcon(bufferedImage);
-                       //ImageIcon icon = new ImageIcon("sample data/original.png");
-
-                       imageLabel.setIcon(icon);                       
-
-               } else {
-                       // load JPEG, PNG, GIF file
-                       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-                       readLoop:{
-                               for (;;){                               
-                                       int b = inputStream.read();
-                                       if (b == -1) break readLoop;
-                                       outputStream.write(b);
-                               }
-                       }
-                       
-                       ImageIcon icon = new ImageIcon(outputStream.toByteArray());
-
-                       bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
-                       bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
-
-                       ImageIcon displayIcon = new ImageIcon(bufferedImage);
-                       imageLabel.setIcon(displayIcon);                        
-               }
-       }
-
-
-       public void createEmptyImage(Dimension dimension){
-
-               bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_3BYTE_BGR);
-
-               ImageIcon icon = new ImageIcon(bufferedImage);
-
-               imageLabel.setIcon(icon);                       
-       }
-
-
-       public void saveImage(File outputFile){
-               Image image = new Image(bufferedImage);
-               try {
-                       image.saveImage(outputFile);
-               } catch (Exception e) {
-                       System.out.println("Error while saving image: " + e.toString());
-               }
-       }
+    private BufferedImage bufferedImage;
+    private JLabel imageLabel;
+
+    public ImagePanel() {
+        super();
+        initGUI();
+    }
+
+    /**
+     * Auto-generated main method to display this JPanel inside a new JFrame.
+     */
+    public static void main(final String[] args) {
+        final JFrame frame = new JFrame();
+        frame.getContentPane().add(new ImagePanel());
+        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+        frame.pack();
+        frame.setVisible(true);
+    }
+
+    public void createEmptyImage(final Dimension dimension) {
+
+        bufferedImage = new BufferedImage(dimension.width, dimension.height,
+                BufferedImage.TYPE_3BYTE_BGR);
+
+        final ImageIcon icon = new ImageIcon(bufferedImage);
+
+        imageLabel.setIcon(icon);
+    }
+
+    private JLabel getImageLabel() {
+        return imageLabel;
+    }
+
+    private void initGUI() {
+        try {
+            final BorderLayout thisLayout = new BorderLayout();
+            setLayout(thisLayout);
+            setPreferredSize(new Dimension(660, 500));
+            {
+                imageLabel = new JLabel();
+                this.add(getImageLabel(), BorderLayout.CENTER);
+            }
+        } catch (final Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void loadImage(final File inputFile, final boolean isImgSqz)
+            throws IOException {
+
+        try (final FileInputStream fileInputStream = new FileInputStream(inputFile)) {
+            loadImage(fileInputStream, isImgSqz);
+        }
+    }
+
+    public void loadImage(final InputStream inputStream, final boolean isImgSqz)
+            throws IOException {
+        if (isImgSqz) {
+            // load ImageSqueeze file
+
+            final Image image = new Image();
+            image.loadImage(inputStream);
+
+            bufferedImage = image.bufferedImage;
+
+            final ImageIcon icon = new ImageIcon(bufferedImage);
+            // ImageIcon icon = new ImageIcon("sample data/original.png");
+
+            imageLabel.setIcon(icon);
+
+        } else {
+            // load JPEG, PNG, GIF file
+            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            readLoop:
+            {
+                for (; ; ) {
+                    final int b = inputStream.read();
+                    if (b == -1)
+                        break readLoop;
+                    outputStream.write(b);
+                }
+            }
+
+            final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
+
+            bufferedImage = new BufferedImage(icon.getIconWidth(),
+                    icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
+            bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
+
+            final ImageIcon displayIcon = new ImageIcon(bufferedImage);
+            imageLabel.setIcon(displayIcon);
+        }
+    }
+
+    public void saveImage(final File outputFile) {
+        final Image image = new Image(bufferedImage);
+        try {
+            image.saveImage(outputFile);
+        } catch (final Exception e) {
+            System.out.println("Error while saving image: " + e.toString());
+        }
+    }
 
 }