pl.piotrsukiennik.jbrain.gui.NewSimulationPanel.java Source code

Java tutorial

Introduction

Here is the source code for pl.piotrsukiennik.jbrain.gui.NewSimulationPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.piotrsukiennik.jbrain.gui;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pl.piotrsukiennik.jbrain.app.AppController;
import pl.piotrsukiennik.jbrain.app.Handler;
import pl.piotrsukiennik.jbrain.app.SimulationController;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 *
 * @author Piotr Sukiennik
 */
@Component
public class NewSimulationPanel extends javax.swing.JPanel {

    @Autowired
    private SimulationController simulationController;

    /**
     * Creates new form NewSimulationPanel
     */
    public NewSimulationPanel() {
        initComponents();
    }

    /**
     * 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() {

        titleInput = new javax.swing.JTextField();
        timeInput = new javax.swing.JSlider();
        routeInput = new javax.swing.JComboBox();
        imageHolder = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        simulateButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        titleInput.setText("Simulation");
        titleInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                titleInputActionPerformed(evt);
            }
        });

        timeInput.setBackground(new java.awt.Color(255, 255, 255));
        timeInput.setMaximum(10000);
        timeInput.setMinimum(100);
        timeInput.setValue(1000);
        timeInput.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                timeInputStateChanged(evt);
            }
        });

        routeInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                routeInputActionPerformed(evt);
            }
        });

        imageHolder.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        imageHolder.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imageHolderMouseClicked(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("No input image selected");

        javax.swing.GroupLayout imageHolderLayout = new javax.swing.GroupLayout(imageHolder);
        imageHolder.setLayout(imageHolderLayout);
        imageHolderLayout.setHorizontalGroup(
                imageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        imageHolderLayout.createSequentialGroup().addContainerGap()
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                                .addContainerGap()));
        imageHolderLayout
                .setVerticalGroup(imageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(imageHolderLayout.createSequentialGroup().addContainerGap()
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                                .addContainerGap()));

        simulateButton.setText("Simulate!");
        simulateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulateButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Simulation title:");

        jLabel2.setText("Simulation time (msec):");

        jLabel3.setText("Brain route:");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Input image:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(imageHolder, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addComponent(simulateButton, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                .createSequentialGroup()
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(
                                                jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 177,
                                                Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(titleInput, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(routeInput, javax.swing.GroupLayout.Alignment.TRAILING, 0,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(timeInput, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)))
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(titleInput, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addComponent(jLabel1,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(timeInput, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(routeInput, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imageHolder, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(simulateButton).addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private void simulateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulateButtonActionPerformed
        setTitle();
        setRoute();
        setSimulationTime();
        simulationController.simulate();
    }//GEN-LAST:event_simulateButtonActionPerformed

    private void titleInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_titleInputActionPerformed
        setTitle();
    }//GEN-LAST:event_titleInputActionPerformed

    private void routeInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_routeInputActionPerformed
        setRoute();
    }//GEN-LAST:event_routeInputActionPerformed

    private void timeInputStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_timeInputStateChanged
        setSimulationTime();
    }//GEN-LAST:event_timeInputStateChanged

    private void setRoute() {
        simulationController.setRoute((String) routeInput.getSelectedItem());
    }

    private void setTitle() {
        simulationController.setTitle(titleInput.getText());
    }

    private void setSimulationTime() {
        simulationController.setSimulationTime(timeInput.getValue());
    }

    private void imageHolderMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageHolderMouseClicked
        simulationController.showInputImageSelectDialog(new Handler<File>() {
            @Override
            public void handle(File file) {
                BufferedImage image = null;
                imageHolder.setLayout(new CardLayout());
                try {
                    image = ImageIO.read(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageHolder.removeAll();
                imageHolder.add(new JLabel(new ImageIcon(image)));
                imageHolder.revalidate();
                imageHolder.repaint();
            }
        });
    }//GEN-LAST:event_imageHolderMouseClicked

    public void setPossibleRoutes(ComboBoxModel comboBoxModel) {
        routeInput.setModel(comboBoxModel);
    }

    public JPanel getImageHolder() {
        return imageHolder;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel imageHolder;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox routeInput;
    private javax.swing.JButton simulateButton;
    private javax.swing.JSlider timeInput;
    private javax.swing.JTextField titleInput;
    // End of variables declaration//GEN-END:variables
}