Package org.bric.gui.tabs

Source Code of org.bric.gui.tabs.RotateJPanel

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.bric.gui.tabs;

import java.util.Random;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import org.bric.gui.rotate.JRotator;
import org.bric.imageEditParameters.RotateParameters;

/**
*
* @author vasilis
*/
public class RotateJPanel extends javax.swing.JPanel  implements ImageEditTab{

    private int rotationUpLimit = 360,
            rotationDownLimit = 0;
    private int randomAngle;
    private int min, max;
    private Random rand = new Random();
   
    /**
     * Creates new form RotateJPanel
     */
    public RotateJPanel() {
        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() {

        buttonGroup2 = new javax.swing.ButtonGroup();
        RotateEnableCheckBox = new javax.swing.JCheckBox();
        customRadioButton = new javax.swing.JRadioButton();
        predefinedRadioButton = new javax.swing.JRadioButton();
        jLabel11 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        angleSlider = new javax.swing.JSlider();
        randomCheckBox = new javax.swing.JCheckBox();
        differentValueCheckBox = new javax.swing.JCheckBox();
        limitCheckBox = new javax.swing.JCheckBox();
        fromSpinner = new javax.swing.JSpinner();
        jLabel12 = new javax.swing.JLabel();
        toSpinner = new javax.swing.JSpinner();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        actionsComboBox = new javax.swing.JComboBox();
        jRotator1 = new org.bric.gui.rotate.JRotator();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("lang/gui/tabs/Bundle"); // NOI18N
        RotateEnableCheckBox.setText(bundle.getString("RotateJPanel.RotateEnableCheckBox.text")); // NOI18N
        RotateEnableCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RotateEnableCheckBoxActionPerformed(evt);
            }
        });

        buttonGroup2.add(customRadioButton);
        customRadioButton.setSelected(true);
        customRadioButton.setText(bundle.getString("RotateJPanel.customRadioButton.text")); // NOI18N
        customRadioButton.setEnabled(false);
        customRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                customRadioButtonItemStateChanged(evt);
            }
        });

        buttonGroup2.add(predefinedRadioButton);
        predefinedRadioButton.setText(bundle.getString("RotateJPanel.predefinedRadioButton.text")); // NOI18N
        predefinedRadioButton.setEnabled(false);
        predefinedRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                predefinedRadioButtonItemStateChanged(evt);
            }
        });

        jLabel11.setText(bundle.getString("RotateJPanel.jLabel11.text")); // NOI18N
        jLabel11.setEnabled(false);

        jTextField4.setText(bundle.getString("RotateJPanel.jTextField4.text")); // NOI18N
        jTextField4.setEnabled(false);
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField4KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField4KeyTyped(evt);
            }
        });

        angleSlider.setMajorTickSpacing(45);
        angleSlider.setMaximum(360);
        angleSlider.setMinorTickSpacing(5);
        angleSlider.setPaintLabels(true);
        angleSlider.setPaintTicks(true);
        angleSlider.setValue(0);
        angleSlider.setEnabled(false);
        angleSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                angleSliderStateChanged(evt);
            }
        });

        randomCheckBox.setText(bundle.getString("RotateJPanel.randomCheckBox.text")); // NOI18N
        randomCheckBox.setEnabled(false);
        randomCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                randomCheckBoxItemStateChanged(evt);
            }
        });

        differentValueCheckBox.setText(bundle.getString("RotateJPanel.differentValueCheckBox.text")); // NOI18N
        differentValueCheckBox.setEnabled(false);

        limitCheckBox.setText(bundle.getString("RotateJPanel.limitCheckBox.text")); // NOI18N
        limitCheckBox.setEnabled(false);
        limitCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                limitCheckBoxItemStateChanged(evt);
            }
        });

        fromSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 360, 1));
        fromSpinner.setEnabled(false);
        fromSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fromSpinnerStateChanged(evt);
            }
        });

        jLabel12.setText(bundle.getString("RotateJPanel.jLabel12.text")); // NOI18N
        jLabel12.setEnabled(false);

        toSpinner.setModel(new javax.swing.SpinnerNumberModel(360, 0, 360, 1));
        toSpinner.setEnabled(false);
        toSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                toSpinnerStateChanged(evt);
            }
        });

        jLabel13.setText(bundle.getString("RotateJPanel.jLabel13.text")); // NOI18N
        jLabel13.setEnabled(false);

        jLabel14.setText(bundle.getString("RotateJPanel.jLabel14.text")); // NOI18N
        jLabel14.setEnabled(false);

        actionsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "180° Upside Down", "90° Counter Clockwise", "90° Clockwise", "Horizontal Flip", "Horizontal + Vertical Flip", "Vertical Flip" }));
        actionsComboBox.setEnabled(false);

        jRotator1.setEnabled(false);
        jRotator1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRotator1MouseClicked(evt);
            }
        });
        jRotator1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jRotator1MouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jRotator1Layout = new javax.swing.GroupLayout(jRotator1);
        jRotator1.setLayout(jRotator1Layout);
        jRotator1Layout.setHorizontalGroup(
            jRotator1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        jRotator1Layout.setVerticalGroup(
            jRotator1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );

        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)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(RotateEnableCheckBox)
                                .addGap(18, 18, 18)
                                .addComponent(customRadioButton)
                                .addGap(18, 18, 18)
                                .addComponent(predefinedRadioButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(randomCheckBox)
                                    .addComponent(limitCheckBox))
                                .addGap(51, 51, 51)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel12)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(fromSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel13)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(toSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(differentValueCheckBox)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addGap(18, 18, 18)
                                .addComponent(actionsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRotator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(angleSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RotateEnableCheckBox)
                    .addComponent(customRadioButton)
                    .addComponent(predefinedRadioButton))
                .addGap(2, 2, 2)
                .addComponent(jRotator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(angleSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(randomCheckBox)
                    .addComponent(differentValueCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(limitCheckBox)
                    .addComponent(jLabel13)
                    .addComponent(toSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(fromSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(actionsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void RotateEnableCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RotateEnableCheckBoxActionPerformed
        rotateTabEnable();
    }//GEN-LAST:event_RotateEnableCheckBoxActionPerformed

    private void customRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_customRadioButtonItemStateChanged
        rotateTabEnable();
    }//GEN-LAST:event_customRadioButtonItemStateChanged

    private void predefinedRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_predefinedRadioButtonItemStateChanged
        rotateTabEnable();
    }//GEN-LAST:event_predefinedRadioButtonItemStateChanged

    private void jTextField4KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyTyped
        rotateFieldAction();
    }//GEN-LAST:event_jTextField4KeyTyped

    private void jTextField4KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyReleased
        rotateFieldAction();
    }//GEN-LAST:event_jTextField4KeyReleased

    private void angleSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_angleSliderStateChanged
        jRotator1.setAngle(angleSlider.getValue());
        if (!jTextField4.hasFocus()) {
            jTextField4.setText(jRotator1.getAngle() + "°");
        }
    }//GEN-LAST:event_angleSliderStateChanged

    private void randomCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_randomCheckBoxItemStateChanged
        rotateTabEnable();
    }//GEN-LAST:event_randomCheckBoxItemStateChanged

    private void limitCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_limitCheckBoxItemStateChanged
        rotateTabEnable();
    }//GEN-LAST:event_limitCheckBoxItemStateChanged

    private void fromSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_fromSpinnerStateChanged
        setRotationLimit();
    }//GEN-LAST:event_fromSpinnerStateChanged

    private void toSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_toSpinnerStateChanged
        setRotationLimit();
    }//GEN-LAST:event_toSpinnerStateChanged

    private void jRotator1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRotator1MouseClicked
       angleSlider.setValue(jRotator1.getAngle());
       jTextField4.setText(jRotator1.getAngle() + "°");
    }//GEN-LAST:event_jRotator1MouseClicked

    private void jRotator1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRotator1MouseDragged
        angleSlider.setValue(jRotator1.getAngle());
        jTextField4.setText(jRotator1.getAngle() + "°");
    }//GEN-LAST:event_jRotator1MouseDragged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox RotateEnableCheckBox;
    private javax.swing.JComboBox actionsComboBox;
    private javax.swing.JSlider angleSlider;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JRadioButton customRadioButton;
    private javax.swing.JCheckBox differentValueCheckBox;
    private javax.swing.JSpinner fromSpinner;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private org.bric.gui.rotate.JRotator jRotator1;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JCheckBox limitCheckBox;
    private javax.swing.JRadioButton predefinedRadioButton;
    private javax.swing.JCheckBox randomCheckBox;
    private javax.swing.JSpinner toSpinner;
    // End of variables declaration//GEN-END:variables

    @Override
    public RotateParameters getImageEditParameters() {
        RotateParameters rotateParameters = new RotateParameters();
        rotateParameters.setEnabled(RotateEnableCheckBox.isSelected());
        rotateParameters.setCustom(customRadioButton.isSelected());
        rotateParameters.setPredefined(predefinedRadioButton.isSelected());
        int angle = jRotator1.getAngle();
        rotateParameters.setAngle(angle);
        rotateParameters.setRandom(randomCheckBox.isSelected());
        rotateParameters.setDifferentValues(differentValueCheckBox.isSelected());
        rotateParameters.setLimit(limitCheckBox.isSelected());
        rotateParameters.setFrom((Integer)fromSpinner.getValue());
        rotateParameters.setTo((Integer)toSpinner.getValue());
        rotateParameters.setAction(actionsComboBox.getSelectedIndex());
        rotateParameters.setRandomAngle(calculateRandomAngle());

        return rotateParameters;
    }
   
    private int calculateRandomAngle(){
        if(limitCheckBox.isSelected()){
            min = Integer.parseInt(fromSpinner.getValue().toString());
            max = Integer.parseInt(toSpinner.getValue().toString());
           
            if(min > max){
                int reminder = max;
                max = min;
                min = reminder;
            }
           
            randomAngle = rand.nextInt(max-min+1)+min;
        } else {
            randomAngle = rand.nextInt(360);
        }
        return randomAngle;
    }
   
    private void rotateTabEnable() {
        boolean enable = RotateEnableCheckBox.isSelected();
        boolean predefined = predefinedRadioButton.isSelected();
        boolean random = randomCheckBox.isSelected();
        boolean limit = limitCheckBox.isSelected();
        customRadioButton.setEnabled(enable);
        predefinedRadioButton.setEnabled(enable);
        jRotator1.setEnabled(enable & !random & !predefined);
        jLabel11.setEnabled(enable & !random & !predefined);
        jTextField4.setEnabled(enable & !random & !predefined);
        angleSlider.setEnabled(enable & !random & !predefined);
        randomCheckBox.setEnabled(enable & !predefined);
        differentValueCheckBox.setEnabled(enable & random & !predefined);
        limitCheckBox.setEnabled(enable & random & !predefined);
        jLabel12.setEnabled(enable & !predefined & limit & random);
        fromSpinner.setEnabled(enable & !predefined & limit & random);
        jLabel13.setEnabled(enable & !predefined & limit & random);
        toSpinner.setEnabled(enable & !predefined & limit & random);
        jLabel14.setEnabled(enable & predefined);
        actionsComboBox.setEnabled(enable & predefined);
    }
   
    private void rotateFieldAction() {
        int symbol = jTextField4.getText().contains("°") ? 1 : 0;
        int fieldNumber = 0;
        try {
            fieldNumber = Integer.parseInt(jTextField4.getText(0, jTextField4.getText().length() - symbol));
        } catch (Exception ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (fieldNumber <= 360 && fieldNumber >= 0 && jTextField4.getText().length() >= 1) {
            jRotator1.setAngle(fieldNumber);
            angleSlider.setValue(fieldNumber);
        }
    }
   
    private void setRotationLimit() {
        rotationDownLimit = Integer.parseInt(fromSpinner.getValue().toString());
        rotationUpLimit = Integer.parseInt(toSpinner.getValue().toString());

        if (rotationDownLimit > rotationUpLimit) {
            int tempValue = rotationDownLimit;
            rotationDownLimit = rotationUpLimit;
            rotationUpLimit = tempValue;
        }
    }
   
   
    public boolean getRotateEnableCheckBox() {
        return RotateEnableCheckBox.isSelected();
    }

    public void setRotateEnableCheckBox(boolean value) {
        this.RotateEnableCheckBox.setSelected(value);
    }

    public int getActionsComboBox() {
        return actionsComboBox.getSelectedIndex();
    }

    public void setActionsComboBox(int index) {
        this.actionsComboBox.setSelectedIndex(index);
    }

    public String getAngleSlider() {
        return String.valueOf(angleSlider.getValue());
    }

    public void setAngleSlider(int angleSlider) {
        this.angleSlider.getModel().setValue(angleSlider);
    }

    public boolean getCustomRadioButton() {
        return customRadioButton.isSelected();
    }

    public void setCustomRadioButton(boolean value) {
        this.customRadioButton.setSelected(value);
    }

    public boolean getDifferentValueCheckBox() {
        return differentValueCheckBox.isSelected();
    }

    public void setDifferentValueCheckBox(boolean value) {
        this.differentValueCheckBox.setSelected(value);
    }

    public String getFromSpinner() {
        return fromSpinner.getValue().toString();
    }

    public void setFromSpinner(int fromSpinner) {
        this.fromSpinner.getModel().setValue(fromSpinner);
    }

    public boolean getLimitCheckBox() {
        return limitCheckBox.isSelected();
    }

    public void setLimitCheckBox(boolean value) {
        this.limitCheckBox.setSelected(value);
    }

    public boolean getPredefinedRadioButton() {
        return predefinedRadioButton.isSelected();
    }

    public void setPredefinedRadioButton(boolean value) {
        this.predefinedRadioButton.setSelected(value);
    }

    public boolean getRandomCheckBox() {
        return randomCheckBox.isSelected();
    }

    public void setRandomCheckBox(boolean value) {
        this.randomCheckBox.setSelected(value);
    }

    public String getToSpinner() {
        return toSpinner.getValue().toString();
    }

    public void setToSpinner(int toSpinner) {
        this.toSpinner.getModel().setValue(toSpinner);
    }
   
}
TOP

Related Classes of org.bric.gui.tabs.RotateJPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.