Java tutorial
import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.JFrame; import javax.swing.SpinnerNumberModel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.util.Log; /* * 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. */ /** * * @author Slaush */ public class ResultScreen extends javax.swing.JFrame { private ModulateSignal modulate; private MouseWheelListener moduladoraMouseWheel = new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent e) { //To change body of generated methods, choose Tools | Templates. if (e.getUnitsToScroll() < 0) dHSpinner.setValue(dHSpinner.getNextValue()); else { if (dHSpinner.getPreviousValue() != null) dHSpinner.setValue(dHSpinner.getPreviousValue()); } } }; private ChangeListener changeDemoduladora = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. showGraphics2(); } }; private ChangeListener changeModuladora = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. showGraphics(); } }; /** * Creates new form ResultScreen * @param modulate seal modulada! */ public ResultScreen(ModulateSignal modulate) { this.modulate = modulate; initComponents(); initBoxes(); initSpinners(); initCharts(); initPanels(); initButtons(); showGraphics(); showGraphics2(); showResultString(); setIconImage(ChartTest.createImage().getImage()); setTitle("Modulacin Angular: Resultado"); } /** * 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() { moduladaPanel = new javax.swing.JPanel(); demoduladaPanel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); resultTextArea = new javax.swing.JTextArea(); okButton = new javax.swing.JButton(); besselButton = new javax.swing.JButton(); jLabel13 = new javax.swing.JLabel(); dDSpinner = new javax.swing.JSpinner(); jLabel14 = new javax.swing.JLabel(); dHSpinner = new javax.swing.JSpinner(); dTiimeUnidadSpinner = new javax.swing.JComboBox<String>(); updateButton = new javax.swing.JButton(); updateButton1 = new javax.swing.JButton(); mTimeUnidadSpinner = new javax.swing.JComboBox<String>(); pHSpinner1 = new javax.swing.JSpinner(); jLabel15 = new javax.swing.JLabel(); pDSpinner1 = new javax.swing.JSpinner(); jLabel16 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); calculosMenu = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setMinimumSize(new java.awt.Dimension(0, 648)); moduladaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Grafica Seal Modulada", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N javax.swing.GroupLayout moduladaPanelLayout = new javax.swing.GroupLayout(moduladaPanel); moduladaPanel.setLayout(moduladaPanelLayout); moduladaPanelLayout.setHorizontalGroup(moduladaPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); moduladaPanelLayout.setVerticalGroup(moduladaPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 146, Short.MAX_VALUE)); demoduladaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Grafica Seal Demodulada", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N javax.swing.GroupLayout demoduladaPanelLayout = new javax.swing.GroupLayout(demoduladaPanel); demoduladaPanel.setLayout(demoduladaPanelLayout); demoduladaPanelLayout.setHorizontalGroup(demoduladaPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); demoduladaPanelLayout.setVerticalGroup(demoduladaPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 146, Short.MAX_VALUE)); jScrollPane1.setBorder(null); resultTextArea.setColumns(20); resultTextArea.setRows(5); resultTextArea.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Valores Importantes", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 14))); // NOI18N jScrollPane1.setViewportView(resultTextArea); okButton.setText("OK"); besselButton.setText("Ver Espectros"); jLabel13.setText("De:"); jLabel14.setText("Hasta:"); dTiimeUnidadSpinner.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); updateButton.setText("Actualizar"); updateButton1.setText("Actualizar"); mTimeUnidadSpinner.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jLabel15.setText("Hasta:"); jLabel16.setText("De:"); jLabel1.setText("Control Seal Modulada"); jLabel2.setText("Control Seal Demodulada"); calculosMenu.setText("Calculos Relevantes"); jMenuBar1.add(calculosMenu); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().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().addGap(113, 113, 113) .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(besselButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout .createSequentialGroup() .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(demoduladaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(moduladaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1)) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel13).addGap(4, 4, 4) .addComponent(dDSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10).addComponent(jLabel14).addGap(4, 4, 4) .addComponent(dHSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( dTiimeUnidadSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(updateButton)) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel16).addGap(4, 4, 4) .addComponent(pDSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10).addComponent(jLabel15).addGap(4, 4, 4) .addComponent(pHSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(mTimeUnidadSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(updateButton1))) .addGap(91, 91, 91))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(moduladaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(demoduladaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(7, 7, 7) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pDSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(pHSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(mTimeUnidadSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(updateButton1)) .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel16).addComponent(jLabel1)) .addComponent(jLabel15)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dDSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dHSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(dTiimeUnidadSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(updateButton)) .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel13).addComponent(jLabel2)) .addComponent(jLabel14)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(okButton).addComponent(besselButton)) .addContainerGap(23, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private ChartPanel moduladaChart; private ChartPanel demoduladaChart; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton besselButton; private javax.swing.JMenu calculosMenu; private javax.swing.JSpinner dDSpinner; private javax.swing.JSpinner dHSpinner; private javax.swing.JComboBox<String> dTiimeUnidadSpinner; private javax.swing.JPanel demoduladaPanel; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel16; private javax.swing.JLabel jLabel2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JComboBox<String> mTimeUnidadSpinner; private javax.swing.JPanel moduladaPanel; private javax.swing.JButton okButton; private javax.swing.JSpinner pDSpinner1; private javax.swing.JSpinner pHSpinner1; private javax.swing.JTextArea resultTextArea; private javax.swing.JButton updateButton; private javax.swing.JButton updateButton1; // End of variables declaration//GEN-END:variables private void initPanels() { GridLayout gL = new GridLayout(); gL.setColumns(1); gL.setRows(1); demoduladaPanel.setLayout(gL); demoduladaPanel.setPreferredSize(new Dimension(150, 200)); demoduladaPanel.add(demoduladaChart); moduladaPanel.setLayout(gL); moduladaPanel.setPreferredSize(new Dimension(150, 200)); moduladaPanel.add(moduladaChart); } private void initBoxes() { ComboBoxManager.getInstance().initTimeBox(dTiimeUnidadSpinner); ComboBoxManager.getInstance().initTimeBox(this.mTimeUnidadSpinner); dTiimeUnidadSpinner.setSelectedIndex(getSelectedIndexByFreq()); mTimeUnidadSpinner.setSelectedIndex(getSelectedIndexByFreq()); } private void initCharts() { moduladaChart = new ChartPanel(null); demoduladaChart = new ChartPanel(null); } private void initSpinners() { dDSpinner.setModel(new SpinnerNumberModel(0, 0, 100000, 1)); dHSpinner.setModel(new SpinnerNumberModel(500, 0, 100000, 1)); pDSpinner1.setModel(new SpinnerNumberModel(0, 0, 100000, 1)); pHSpinner1.setModel(new SpinnerNumberModel(500, 0, 100000, 1)); dDSpinner.addChangeListener(changeDemoduladora); dHSpinner.addChangeListener(changeDemoduladora); pDSpinner1.addChangeListener(changeModuladora); pHSpinner1.addChangeListener(changeModuladora); } private void initButtons() { updateButton1.addActionListener((ActionEvent e) -> { showGraphics(); }); updateButton.addActionListener((ActionEvent e) -> { showGraphics2(); }); besselButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { EspectroForm spectro = new EspectroForm(modulate.getSpectro()); spectro.setVisible(true); } }); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); } }); calculosMenu.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. CalculosForm c = new CalculosForm(modulate); c.setVisible(true); } @Override public void mouseReleased(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void mouseEntered(MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void mouseExited(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }); } private void showGraphics() { int desdeM = (int) pDSpinner1.getValue(); int hastaM = (int) pHSpinner1.getValue(); if (desdeM < hastaM) { String unidadTiempo = (String) this.mTimeUnidadSpinner.getSelectedItem(); JFreeChart modulada = GeneradorDeGraphicas.getInstance().drawSignal(modulate, desdeM, hastaM, ConversorDeUnidades.getInstance().retornarMultiploUnidad(unidadTiempo)); moduladaChart.setChart(modulada); } } private void showGraphics2() { int desdeD = (int) dDSpinner.getValue(); int hastaD = (int) dHSpinner.getValue(); if (desdeD < hastaD) { String unidadTiempo = (String) this.dTiimeUnidadSpinner.getSelectedItem(); JFreeChart demodulada = GeneradorDeGraphicas.getInstance().drawSignal(modulate.demodulada(), desdeD, hastaD, ConversorDeUnidades.getInstance().retornarMultiploUnidad(unidadTiempo)); demoduladaChart.setChart(demodulada); } } private void showResultString() { resultTextArea.setEditable(false); resultTextArea.setText(modulate.toString()); } private int getSelectedIndexByFreq() { String[] unidades = ConversorDeUnidades.unidades; String u = this.modulate.portadora.getUnidadOptimaStr(); for (int i = 0; i < unidades.length; i++) { if (u.equals(unidades[i])) return i; } return 0; } }