/* * ShearPanel.java * * Created on November 3, 2008, 3:45 PM */ package ch.ethz.ika.terrainviewer; import ch.ethz.karto.map3d.Map3DAnimation; import ch.ethz.karto.map3d.Map3DShearAnimation; import ch.ethz.karto.map3d.Map3DViewer; import java.awt.event.ItemEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.DecimalFormat; import javax.media.opengl.GLAutoDrawable; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * * @author Bernhard Jenny, Institute of Cartography, ETH Zurich. */ public class ShearPanel extends javax.swing.JPanel implements PropertyChangeListener { private Map3DViewer map3DViewer; private boolean updatingGUI = false; private static javax.swing.JDialog dialog = null; public static void showPanel(JFrame owner, Map3DViewer map3DViewer) { if (dialog == null) { dialog = new JDialog(owner, "Shearing Options", false); //frame = new javax.swing.JFrame("Display Options"); dialog.getRootPane().putClientProperty("Window.style", "small"); // setAlwaysOnTop() is not supported for applets try { dialog.setAlwaysOnTop(true); } catch (SecurityException se) { } dialog.add(new ShearPanel(map3DViewer)); dialog.pack(); dialog.setLocation(300, 30); dialog.setResizable(false); } dialog.setVisible(true); } /** Creates new form ShearPanel */ public ShearPanel(Map3DViewer map3DViewer) { this.map3DViewer = map3DViewer; initComponents(); Map3DAnimation animation = new Map3DShearAnimation((GLAutoDrawable) map3DViewer.getComponent()); map3DViewer.setAnimation(animation); map3DViewer.getComponent().addPropertyChangeListener("view", this); } /** 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() { java.awt.GridBagConstraints gridBagConstraints; javax.swing.JPanel shearPanel = new javax.swing.JPanel(); shearDirectionSlider = new javax.swing.JSlider(); shearRadiusSlider = new javax.swing.JSlider(); javax.swing.JLabel jLabel5 = new javax.swing.JLabel(); javax.swing.JLabel jLabel10 = new javax.swing.JLabel(); animateToggleButton = new javax.swing.JToggleButton(); cycleDurationSlider = new javax.swing.JSlider(); javax.swing.JLabel jLabel11 = new javax.swing.JLabel(); durationLabel = new javax.swing.JLabel(); animationTypeComboBox = new javax.swing.JComboBox(); animationAmplitudeSlider = new javax.swing.JSlider(); javax.swing.JLabel jLabel12 = new javax.swing.JLabel(); setLayout(new java.awt.BorderLayout()); shearPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 10, 5, 1)); shearPanel.setLayout(new java.awt.GridBagLayout()); shearDirectionSlider.setMajorTickSpacing(90); shearDirectionSlider.setMaximum(180); shearDirectionSlider.setMinimum(-180); shearDirectionSlider.setMinorTickSpacing(15); shearDirectionSlider.setPaintLabels(true); shearDirectionSlider.setPaintTicks(true); shearDirectionSlider.setToolTipText("Shear Direction"); shearDirectionSlider.setValue(0); shearDirectionSlider.setPreferredSize(new java.awt.Dimension(250, 54)); shearDirectionSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { sliderStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; shearPanel.add(shearDirectionSlider, gridBagConstraints); shearRadiusSlider.setMajorTickSpacing(50); shearRadiusSlider.setMaximum(200); shearRadiusSlider.setMinorTickSpacing(10); shearRadiusSlider.setPaintLabels(true); shearRadiusSlider.setPaintTicks(true); shearRadiusSlider.setToolTipText("Shear Radius"); shearRadiusSlider.setPreferredSize(new java.awt.Dimension(250, 54)); shearRadiusSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { sliderStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; shearPanel.add(shearRadiusSlider, gridBagConstraints); jLabel5.setText("Direction"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; shearPanel.add(jLabel5, gridBagConstraints); jLabel10.setText("Amplitude"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; shearPanel.add(jLabel10, gridBagConstraints); animateToggleButton.setText("Animate"); animateToggleButton.putClientProperty("JButton.buttonType", "bevel"); animateToggleButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { animateToggleButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; shearPanel.add(animateToggleButton, gridBagConstraints); cycleDurationSlider.setMaximum(50); cycleDurationSlider.setMinimum(1); cycleDurationSlider.setToolTipText("Duration of a full cycle"); cycleDurationSlider.setValue(10); cycleDurationSlider.setEnabled(false); cycleDurationSlider.setPreferredSize(new java.awt.Dimension(250, 54)); cycleDurationSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { cycleDurationSliderStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; shearPanel.add(cycleDurationSlider, gridBagConstraints); jLabel11.setText("Duration"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; shearPanel.add(jLabel11, gridBagConstraints); durationLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); durationLabel.setText("1 sec"); durationLabel.setMaximumSize(new java.awt.Dimension(45, 16)); durationLabel.setMinimumSize(new java.awt.Dimension(45, 16)); durationLabel.setPreferredSize(new java.awt.Dimension(45, 16)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 2; gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5); shearPanel.add(durationLabel, gridBagConstraints); animationTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Up - Down", "Left - Right", "Circular", "Flower" })); animationTypeComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { animationTypeComboBoxItemStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = 2; shearPanel.add(animationTypeComboBox, gridBagConstraints); animationAmplitudeSlider.setMajorTickSpacing(50); animationAmplitudeSlider.setMaximum(200); animationAmplitudeSlider.setMinorTickSpacing(10); animationAmplitudeSlider.setPaintLabels(true); animationAmplitudeSlider.setPaintTicks(true); animationAmplitudeSlider.setToolTipText("Shear Radius"); animationAmplitudeSlider.setEnabled(false); animationAmplitudeSlider.setPreferredSize(new java.awt.Dimension(250, 54)); animationAmplitudeSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { animationAmplitudeSliderStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; shearPanel.add(animationAmplitudeSlider, gridBagConstraints); jLabel12.setText("Radius"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; shearPanel.add(jLabel12, gridBagConstraints); add(shearPanel, java.awt.BorderLayout.CENTER); }// </editor-fold>//GEN-END:initComponents public void propertyChange(PropertyChangeEvent evt) { writeModelToGUI(); } private void writeModelToGUI() { if (updatingGUI) { return; } updatingGUI = true; try { this.shearDirectionSlider.setValue((int) Math.round(map3DViewer.getShearDirection())); this.shearRadiusSlider.setValue((int) Math.round(100 * map3DViewer.getShearRadius())); } finally { updatingGUI = false; } } private Map3DShearAnimation getShearAnimation() { Map3DAnimation animation = map3DViewer.getAnimation(); if (animation instanceof Map3DShearAnimation) { return (Map3DShearAnimation) animation; } else { return null; } } private void animateToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_animateToggleButtonActionPerformed final boolean animate = animateToggleButton.isSelected(); if (animate) { this.getShearAnimation().startAnimation(); } else { map3DViewer.getAnimation().stopAnimation(); } this.shearDirectionSlider.setEnabled(!animate); this.shearRadiusSlider.setEnabled(!animate); this.cycleDurationSlider.setEnabled(animate); this.animationAmplitudeSlider.setEnabled(animate); }//GEN-LAST:event_animateToggleButtonActionPerformed private void sliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderStateChanged if (updatingGUI) { return; } try { updatingGUI = true; double angle = Math.toRadians(this.shearDirectionSlider.getValue()); double radius = this.shearRadiusSlider.getValue() / 100.; map3DViewer.setShearX((float) (Math.cos(angle) * radius)); map3DViewer.setShearY((float) (Math.sin(angle) * radius)); } finally { updatingGUI = false; } }//GEN-LAST:event_sliderStateChanged private void cycleDurationSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cycleDurationSliderStateChanged float duration = this.cycleDurationSlider.getValue() / 10.f; String durationStr = new DecimalFormat("#.# sec").format(duration); durationLabel.setText(durationStr); Map3DShearAnimation animation = getShearAnimation(); if (animation != null) { ((Map3DShearAnimation) animation).setCycleDuration(duration); } }//GEN-LAST:event_cycleDurationSliderStateChanged private void animationTypeComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_animationTypeComboBoxItemStateChanged if (evt.getStateChange() != ItemEvent.SELECTED) { return; } Map3DShearAnimation animation = getShearAnimation(); if (animation != null) { int selectedItem = animationTypeComboBox.getSelectedIndex(); switch (selectedItem) { case 0: animation.setAnimationType(Map3DShearAnimation.AnimationType.updown); break; case 1: animation.setAnimationType(Map3DShearAnimation.AnimationType.leftright); break; case 2: animation.setAnimationType(Map3DShearAnimation.AnimationType.circular); break; case 3: String answer = (String)JOptionPane.showInputDialog(this, "Number of petals", "Rose Animation", JOptionPane.QUESTION_MESSAGE, null, null, "3"); int nPetals = Integer.parseInt(answer); animation.setNRosePetals(nPetals); animation.setAnimationType(Map3DShearAnimation.AnimationType.rose); break; } } }//GEN-LAST:event_animationTypeComboBoxItemStateChanged private void animationAmplitudeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_animationAmplitudeSliderStateChanged float amplitude = animationAmplitudeSlider.getValue() / 100f; getShearAnimation().setAnimationAmplitude(amplitude); }//GEN-LAST:event_animationAmplitudeSliderStateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JToggleButton animateToggleButton; private javax.swing.JSlider animationAmplitudeSlider; private javax.swing.JComboBox animationTypeComboBox; private javax.swing.JSlider cycleDurationSlider; private javax.swing.JLabel durationLabel; private javax.swing.JSlider shearDirectionSlider; private javax.swing.JSlider shearRadiusSlider; // End of variables declaration//GEN-END:variables }