package gui.test;

import dataBase.DataBase;
import gui.test.ChoosePic;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import mosaik.MosaikFunctions;
import org.jdesktop.application.Action;
import util.simpleIO.Out;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Mosaik.java
 *
 * Created on 22.06.2012, 22:23:47
 */
/**
 *
 * @author Niklas
 */
public class Mosaik extends javax.swing.JFrame {

    /** Creates new form Mosaik */
    public Mosaik() {




        initComponents();
        disableAllComponents(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jPanelMain = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelAusgangsbild = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jLabelPic = new javax.swing.JLabel();
        jPanelMosaik = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabelMosaik = new javax.swing.JLabel();
        jPanelVergleich = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabelMosaikCompare = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabelPicCompare = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/test/Bundle"); // NOI18N
        setTitle(bundle.getString("Mosaik.title")); // NOI18N
        setIconImages(null);
        setMinimumSize(new java.awt.Dimension(1024, 768));
        setName("Form"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N
        jScrollPane2.setPreferredSize(new java.awt.Dimension(1024, 690));

        jPanelMain.setName("jPanelMain"); // NOI18N
        jPanelMain.setPreferredSize(new java.awt.Dimension(1024, 768));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(Mosaik.class, this);
        jButton1.setAction(actionMap.get("openPictureDialog")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(formListener);

        jButton2.setAction(actionMap.get("chooseDatabase")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton6.setAction(actionMap.get("chooseDirectoryOfPictures")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(formListener);

        jLabel4.setFont(new java.awt.Font("Tahoma 11", 1, 12));
        jLabel4.setText(bundle.getString("Mosaik.jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(bundle.getString("Mosaik.jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(bundle.getString("Mosaik.jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(bundle.getString("Mosaik.jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel9.setText(bundle.getString("Mosaik.jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(bundle.getString("Mosaik.jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma 11", 1, 12)); // NOI18N
        jLabel11.setText(bundle.getString("Mosaik.jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel12.setText(bundle.getString("Mosaik.jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setText(bundle.getString("Mosaik.jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel14.setFont(new java.awt.Font("Tahoma 11", 1, 12)); // NOI18N
        jLabel14.setText(bundle.getString("Mosaik.jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        jLabel15.setText(bundle.getString("Mosaik.jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        jLabel18.setText(bundle.getString("Mosaik.jLabel18.text")); // NOI18N
        jLabel18.setName("jLabel18"); // NOI18N

        jLabel19.setForeground(new java.awt.Color(255, 0, 0));
        jLabel19.setText(bundle.getString("Mosaik.jLabel19.text")); // NOI18N
        jLabel19.setName("jLabel19"); // NOI18N

        jLabel20.setForeground(new java.awt.Color(255, 0, 0));
        jLabel20.setText(bundle.getString("Mosaik.jLabel20.text")); // NOI18N
        jLabel20.setName("jLabel20"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel12)
                    .add(jLabel11)
                    .add(jLabel13)
                    .add(jLabel14)
                    .add(jLabel9)
                    .add(jLabel10)
                    .add(jLabel7)
                    .add(jLabel15)
                    .add(jLabel4)
                    .add(jLabel6)
                    .add(jLabel5)
                    .add(jButton2)
                    .add(jButton1)
                    .add(jLabel18)
                    .add(jLabel19)
                    .add(jLabel20)
                    .add(jButton6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel7)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1)
                .add(4, 4, 4)
                .add(jLabel11)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel10)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel9)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel14)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel13)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel15)
                .add(7, 7, 7)
                .add(jLabel18)
                .add(18, 18, 18)
                .add(jLabel19)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel20)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton6)
                .add(42, 42, 42))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel6.setName("jPanel6"); // NOI18N

        jLabel16.setFont(new java.awt.Font("Tahoma 11", 1, 12));
        jLabel16.setText(bundle.getString("Mosaik.jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        jLabel3.setText(bundle.getString("Mosaik.jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setText(bundle.getString("Mosaik.jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField2.setText(bundle.getString("Mosaik.jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(bundle.getString("Mosaik.jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel17.setText(bundle.getString("Mosaik.jLabel17.text")); // NOI18N
        jLabel17.setName("jLabel17"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel16)
                    .add(jLabel17)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(29, 29, 29)
                        .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(59, Short.MAX_VALUE))
        );

        jPanel6Layout.linkSize(new java.awt.Component[] {jTextField2, jTextField3}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel16)
                .add(3, 3, 3)
                .add(jLabel17)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(3, 3, 3)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setName("jPanel3"); // NOI18N

        jButton4.setAction(actionMap.get("resizePicture")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jLabel1.setText(bundle.getString("Mosaik.jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField1.setText(bundle.getString("Mosaik.jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma 11", 1, 12));
        jLabel8.setText(bundle.getString("Mosaik.jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLabel8)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField1))
                    .add(jButton4))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton4)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanelAusgangsbild.setAutoscrolls(true);
        jPanelAusgangsbild.setName("jPanelAusgangsbild"); // NOI18N
        jPanelAusgangsbild.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setAutoscrolls(true);
        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jLabelPic.setName("jLabelPic"); // NOI18N
        jScrollPane4.setViewportView(jLabelPic);

        jPanelAusgangsbild.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(bundle.getString("Mosaik.jPanelAusgangsbild.TabConstraints.tabTitle"), jPanelAusgangsbild); // NOI18N

        jPanelMosaik.setName("jPanelMosaik"); // NOI18N
        jPanelMosaik.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jLabelMosaik.setText(bundle.getString("Mosaik.jLabelMosaik.text")); // NOI18N
        jLabelMosaik.setName("jLabelMosaik"); // NOI18N
        jScrollPane1.setViewportView(jLabelMosaik);

        jPanelMosaik.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(bundle.getString("Mosaik.jPanelMosaik.TabConstraints.tabTitle"), jPanelMosaik); // NOI18N

        jPanelVergleich.setName("jPanelVergleich"); // NOI18N
        jPanelVergleich.setPreferredSize(new java.awt.Dimension(794, 794));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("Mosaik.jPanel5.border.title"))); // NOI18N
        jPanel5.setMinimumSize(new java.awt.Dimension(375, 400));
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setPreferredSize(new java.awt.Dimension(375, 400));

        jLabelMosaikCompare.setText(bundle.getString("Mosaik.jLabelMosaikCompare.text")); // NOI18N
        jLabelMosaikCompare.setName("jLabelMosaikCompare"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabelMosaikCompare)
                .addContainerGap(350, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jLabelMosaikCompare)
                .addContainerGap(373, Short.MAX_VALUE))
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("Mosaik.jPanel7.border.title"))); // NOI18N
        jPanel7.setMinimumSize(new java.awt.Dimension(375, 400));
        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(375, 400));

        jLabelPicCompare.setText(bundle.getString("Mosaik.jLabelPicCompare.text")); // NOI18N
        jLabelPicCompare.setName("jLabelPicCompare"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabelPicCompare)
                .addContainerGap(351, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel7Layout.createSequentialGroup()
                .add(jLabelPicCompare)
                .addContainerGap(373, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanelVergleichLayout = new org.jdesktop.layout.GroupLayout(jPanelVergleich);
        jPanelVergleich.setLayout(jPanelVergleichLayout);
        jPanelVergleichLayout.setHorizontalGroup(
            jPanelVergleichLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelVergleichLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 372, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jPanel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 373, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelVergleichLayout.setVerticalGroup(
            jPanelVergleichLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelVergleichLayout.createSequentialGroup()
                .add(11, 11, 11)
                .add(jPanelVergleichLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel7, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(397, 397, 397))
        );

        jTabbedPane1.addTab(bundle.getString("Mosaik.jPanelVergleich.TabConstraints.tabTitle"), jPanelVergleich); // NOI18N

        jButton3.setAction(actionMap.get("createMosaik")); // NOI18N
        jButton3.setText(bundle.getString("Mosaik.jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jButton5.setAction(actionMap.get("exportMosaik")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanelMainLayout = new org.jdesktop.layout.GroupLayout(jPanelMain);
        jPanelMain.setLayout(jPanelMainLayout);
        jPanelMainLayout.setHorizontalGroup(
            jPanelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelMainLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanelMainLayout.createSequentialGroup()
                        .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 123, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton5)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 761, Short.MAX_VALUE)
                .add(27, 27, 27))
        );
        jPanelMainLayout.setVerticalGroup(
            jPanelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelMainLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
                    .add(jPanelMainLayout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 415, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(11, 11, 11)
                        .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(11, 11, 11)
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(11, 11, 11)
                        .add(jPanelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton5)
                            .add(jButton3))))
                .addContainerGap(79, Short.MAX_VALUE))
        );

        jScrollPane2.setViewportView(jPanelMain);

        getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setMnemonic('f');
        fileMenu.setText(bundle.getString("Mosaik.fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        openMenuItem.setAction(actionMap.get("saveCurrentDatabase")); // NOI18N
        openMenuItem.setMnemonic('o');
        openMenuItem.setText(bundle.getString("Mosaik.openMenuItem.text")); // NOI18N
        openMenuItem.setName("openMenuItem"); // NOI18N
        fileMenu.add(openMenuItem);

        saveMenuItem.setAction(actionMap.get("exportMosaik")); // NOI18N
        saveMenuItem.setMnemonic('s');
        saveMenuItem.setText(bundle.getString("Mosaik.saveMenuItem.text")); // NOI18N
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setAction(actionMap.get("openPictureDialog")); // NOI18N
        saveAsMenuItem.setMnemonic('a');
        saveAsMenuItem.setText(bundle.getString("Mosaik.saveAsMenuItem.text")); // NOI18N
        saveAsMenuItem.setName("saveAsMenuItem"); // NOI18N
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText(bundle.getString("Mosaik.exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        exitMenuItem.addActionListener(formListener);
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setMnemonic('e');
        editMenu.setText(bundle.getString("Mosaik.editMenu.text")); // NOI18N
        editMenu.setName("editMenu"); // NOI18N

        cutMenuItem.setAction(actionMap.get("configuration")); // NOI18N
        cutMenuItem.setText(bundle.getString("Mosaik.cutMenuItem.text")); // NOI18N
        cutMenuItem.setName("cutMenuItem"); // NOI18N
        editMenu.add(cutMenuItem);

        menuBar.add(editMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText(bundle.getString("Mosaik.helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == jButton1) {
                Mosaik.this.jButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton6) {
                Mosaik.this.jButton6ActionPerformed(evt);
            }
            else if (evt.getSource() == exitMenuItem) {
                Mosaik.this.exitMenuItemActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }



        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Mosaik().setVisible(true);
            }
        });
    }

    public void disableAllComponents(boolean b) {

        jButton2.setEnabled(b);
        jButton6.setEnabled(b);
        jTextField2.setEnabled(b);
        jTextField3.setEnabled(b);
        jButton4.setEnabled(b);
        jTextField1.setEnabled(b);
        jButton3.setEnabled(b);
        jButton5.setEnabled(b);
        jTabbedPane1.setEnabled(b);


    }

    public void enableComponents1(boolean b) {

        jButton2.setEnabled(b);
        jButton6.setEnabled(b);
        jTextField2.setEnabled(b);
        jTextField3.setEnabled(b);
        jButton4.setEnabled(b);
        jTextField1.setEnabled(b);

    }

    public void enableComponents2(boolean b) {

        jButton3.setEnabled(b);
        jTabbedPane1.setEnabled(b);
    }

    @Action
    public void openPictureDialog() {

        choosePic = new ChoosePic(this, true);
        choosePic.setLocationRelativeTo(this);
        choosePic.pack();
        choosePic.setVisible(true);

    }

    @Action
    public void chooseDirectoryOfPictures() throws IOException {

        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            picturesDirectoryOnly = new File(jfc.getSelectedFile().getAbsolutePath());
            newDatabaseDirectory = new File("C:/Image_DB/defaultDB.db");
          
            Out.pl("DB made.");
            DataBase db = MosaikFunctions.makeDatabase(picturesDirectoryOnly);

            MosaikFunctions.storeDatabase(db, newDatabaseDirectory);
            Out.pl("DB stored.");
            
            currentdatabase= db;

            databaseDirectory = newDatabaseDirectory;  // sets the new Database as current Database
            enableComponents2(true);
        }
        System.out.print(picturesDirectoryOnly);
    }

    @Action
    public void chooseDatabase() {

        chooseDatabase = new ChooseDatabase(this, true, "chooseDatabase");
        chooseDatabase.setLocationRelativeTo(this);
        chooseDatabase.pack();
        chooseDatabase.setVisible(true);

    }

    @Action
    public void exportMosaik() throws IOException {

        JFileChooser jfc = new JFileChooser();
        jfc.removeChoosableFileFilter(jfc.getFileFilter());
        jfc.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                return true;
            }

            @Override
            public String getDescription() {
                return ".jpg";
            }
        });

        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            savePathofExportedFile = new File(jfc.getSelectedFile().getAbsolutePath());
        }

        BufferedImage image = new BufferedImage(jLabelMosaik.getWidth(),
                jLabelPic.getHeight(), BufferedImage.TYPE_INT_RGB);
        jLabelPic.paintAll(image.getGraphics());

        String name = savePathofExportedFile.getName();
        int i = name.lastIndexOf('.');

        if (i > 0 && i < name.length() - 1) {
            String type = name.substring(i + 1).toLowerCase();

            if (type.equals("jpg")) {
                ImageIO.write(image, "jpg", savePathofExportedFile);
            }

            System.out.print(savePathofExportedFile);
        } else {
            ImageIO.write(image, "jpg", new File(savePathofExportedFile + ".jpg"));
        }
    }

    @Action
    public void configuration() {

        configuration = new Configuration(this, true);
        configuration.setLocationRelativeTo(this);
        configuration.pack();
        configuration.setVisible(true);

    }

    @Action
    public void resizePicture() {
        int faktor = Integer.parseInt(jTextField1.getText());
        int h = originalPicture.getIconHeight();
        int w = originalPicture.getIconWidth();

        int newH = h / 100 * faktor;
        int newW = w / 100 * faktor;
        Image tmp = originalPicture.getImage().getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
        originalPicture = new ImageIcon(tmp);
        this.jLabelPic.setIcon(originalPicture);
        this.jLabelPic.validate();

    }

    public void setPictureofJLabelPic() throws IOException {
        originalPicture = new ImageIcon(ImageIO.read(picturePathofTheOrinignalFile));
          this.jLabelPic.setIcon(originalPicture);
          
         setPicture(jLabelPicCompare, ImageIO.read(picturePathofTheOrinignalFile), jPanel7);
        this.jLabelPic.validate();
    }
    
     @Action
    public void createMosaik() throws FileNotFoundException, IOException, ClassNotFoundException {
        int xparts = Integer.parseInt(jTextField2.getText());
        int yparts = Integer.parseInt(jTextField3.getText());
        float faktor = Integer.parseInt(jTextField1.getText()) / 100.0f;	// durch float dividieren, sonst nur Integer als ergebnis!

        //mosaikImage = MosaikFunctions.makeMosaik(picturePathofTheOrinignalFile, databaseDirectory, xparts, yparts, faktor);
        mosaikImage = MosaikFunctions.makeMosaik(picturePathofTheOrinignalFile, currentdatabase, xparts, yparts, faktor);
        
        ImageIcon tmp = new ImageIcon(mosaikImage);
        this.jLabelMosaik.setIcon(tmp);
        this.jLabelMosaik.validate();
        jTabbedPane1.setSelectedComponent(jPanelMosaik);
        setPicture(jLabelMosaikCompare, mosaikImage, jPanel5);

        jButton5.setEnabled(true);
    }

    public void setPicture(JLabel jLabelLoadedImage1, BufferedImage image, JPanel panel) throws IOException {

        int w = image.getWidth();
        int h = image.getHeight();
        double proW = w / panel.getWidth();
        double proH = h / panel.getHeight();
        ImageIcon newpic;
        if (proW > proH) {
            proH = h / proW;
            newpic = new ImageIcon(image.getScaledInstance(panel.getWidth(), (int) proH, Image.SCALE_FAST));

        } else {
            proW = w / proH;
            proH = jLabelLoadedImage1.getHeight();
            // proH = proH + (proH/100*20);        
            //TODO: corrected by Krisi (Exception). Correct?? Effekt? Nicht mehrkbar...
            newpic = new ImageIcon(image.getScaledInstance((int) proW, panel.getHeight(), Image.SCALE_FAST));
        }

        jLabelLoadedImage1.setIcon(newpic);
        jLabelLoadedImage1.validate();

    }

    @Action
    public void saveCurrentDatabase() throws IOException {

        storeDatabase = new ChooseNewDatabase(this, true, "storeDatabase");
        storeDatabase.setLocationRelativeTo(this);
        storeDatabase.pack();
        storeDatabase.setVisible(true);
        
    MosaikFunctions.storeDatabase(currentdatabase, databaseDirectory);
    
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelMosaik;
    private javax.swing.JLabel jLabelMosaikCompare;
    protected javax.swing.JLabel jLabelPic;
    private javax.swing.JLabel jLabelPicCompare;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    protected javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    protected javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanelAusgangsbild;
    private javax.swing.JPanel jPanelMain;
    private javax.swing.JPanel jPanelMosaik;
    private javax.swing.JPanel jPanelVergleich;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    // End of variables declaration//GEN-END:variables
    JDialog choosePic;
    JDialog exportMosaik;
    JDialog chooseDatabase;
    JDialog chooseDirectory;
    JDialog configuration;
    JDialog storeDatabase;
    File picturesDirectoryOnly;
    File databaseDirectory;
    File picturePathofTheOrinignalFile;
    File savePathofExportedFile;
    ImageIcon originalPicture;
    BufferedImage mosaikImage;
    File newDatabaseDirectory;
    DataBase currentdatabase;

   
}
