/* * TimeSim1.java * * Created on October 11, 2003, 10:03 PM * @author Barry barry@coilgun.info * * NOTES ON TABLE MODEL: * The "table model" contains all the values in the row and column grid. * For this application, the number of columns is sufficient to contain 1 - 10 msec, or 10 columns. * And the number of rows can hold 1 microfarad - 1 farad. * * Scrolling vertically through capacitors is handled automatically by the JScrollPane. * Scrolling horizontally through time is done with UI buttons that replace * table values and update column headings. * * To keep a reasonable in-memory table size, we pre-allocate the entire table. * This is approximately 11 cols x 98 rows = 1078 items. * * @see http://java.sun.com/products/jfc/swingdoc-api-1.1.1/javax/swing/JTable.html * @see http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JScrollPane.html * @see http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JScrollBar.html */ import javax.swing.*; import javax.swing.table.*; import javax.swing.JComponent.*; import java.awt.Dimension; public class TimeSim1 extends javax.swing.JApplet { public final String sVersion = "v1.2"; /** * getAppletInfo is a bean pattern the browser can use to get * information about this applet. It can also be used to fill * in an About box. */ public String getAppletInfo() { return "Name: TimeSim1\r\n" + "Author: Barry (barry@coilgun.info)\r\n" + "Calculates inductance required in an LC circuit,\r\n" + "to result in a desired pulse width for a given capacitor.\r\n"; } /** Initializes the applet TimeSim1 */ public void init() { initComponents(); // create UI controls jLabelVersion.setText(sVersion); timeButtonGroup.add(jrbZeroCrossing); // group these radio buttons together timeButtonGroup.add(jrbHalfPower); // (is there a way to do it in NetBeans UI?) timeButtonGroup.add(jrbFullCycle); jrbZeroCrossing.setSelected(true); // select top radio button jLabelIcon.setIcon(createImageIcon("zerocrossing.gif")); // Show image example of timing selected String p = jrbZeroCrossing.getText(); // Set title above table to exactly match the button's label jLabelTime.setText(p); cap = Capacitor.initCapacitorList(); time = initColumnTime(1E-3, 1E-3); // start with 1 msec in leftmost column, increment by 1 msec initRowNames(); // put capacitor values into leftmost column initColumnNames(); // make column headings initInductanceTable(); // fill in entire table with inductor values // fill in first answer for custom time + cap String sAnswer = computeCustom(jTextTime.getText(), jTextCapacitance.getText()); jLabelAnswer.setText(sAnswer); } /** * Adjust time scale left/right to show prev/next decade of time values * Note that columns are always a decade of time: 1-10, or 10-100, or 100-1000 */ public void scrollTime(boolean scrollDirection) { // obtain tentative new start/end times double newStartTime; if (scrollDirection == SCROLL_RIGHT) { newStartTime = time[0] * 10; } else { newStartTime = time[0] / 10; } double newEndTime = newStartTime + newStartTime*TIME_COLUMNS; // allow scrolling from 1.0 usec - 1 second if (scrollDirection == SCROLL_LEFT && newStartTime <= 0.999E-6) return; if (scrollDirection == SCROLL_RIGHT && newEndTime >= 9.999) return; // increment of time from one column to the next double incrTime = newStartTime; // scroll by adding requested amount of time time = initColumnTime(newStartTime, incrTime); initColumnNames(); initInductanceTable(); // fill in entire table with inductor values } /** * Initialize the value (seconds) for each column in table */ public double[] initColumnTime(double startTime, double increment) { double[] myTime = new double[TIME_COLUMNS]; for (int tIndex=0; tIndex>>"); jButtonScrollRight.setToolTipText("Scroll right"); jButtonScrollRight.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButtonScrollRightMouseClicked(evt); } }); getContentPane().add(jButtonScrollRight); jButtonScrollRight.setBounds(590, 50, 56, 20); jrbZeroCrossing.setFont(new java.awt.Font("Dialog", 0, 12)); jrbZeroCrossing.setMnemonic('Z'); jrbZeroCrossing.setText("Time for Zero Crossing Pulse"); jrbZeroCrossing.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { actionZeroCrossing(evt); } }); getContentPane().add(jrbZeroCrossing); jrbZeroCrossing.setBounds(20, 390, 187, 24); jrbHalfPower.setFont(new java.awt.Font("Dialog", 0, 12)); jrbHalfPower.setMnemonic('H'); jrbHalfPower.setText("Time for Half Power Pulse"); jrbHalfPower.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { actionHalfPower(evt); } }); getContentPane().add(jrbHalfPower); jrbHalfPower.setBounds(20, 370, 168, 24); jrbFullCycle.setFont(new java.awt.Font("Dialog", 0, 12)); jrbFullCycle.setMnemonic('F'); jrbFullCycle.setText("Time for One Full Cycle"); jrbFullCycle.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { actionFullCycle(evt); } }); getContentPane().add(jrbFullCycle); jrbFullCycle.setBounds(20, 410, 152, 24); jLabelTimeButtons.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelTimeButtons.setText("Select a definition of the time period:"); getContentPane().add(jLabelTimeButtons); jLabelTimeButtons.setBounds(20, 350, 198, 16); jLabelIcon.setBackground(new java.awt.Color(255, 255, 204)); jLabelIcon.setForeground(new java.awt.Color(255, 255, 204)); getContentPane().add(jLabelIcon); jLabelIcon.setBounds(230, 350, 125, 110); jLabelCustom1.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelCustom1.setText("Read the inductance in the table above, or"); getContentPane().add(jLabelCustom1); jLabelCustom1.setBounds(420, 350, 240, 16); jLabelCustom2.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelCustom2.setText("or enter your custom values here:"); getContentPane().add(jLabelCustom2); jLabelCustom2.setBounds(420, 370, 230, 16); jLabelCapacitancePrompt.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelCapacitancePrompt.setText("Capacitance (uF):"); getContentPane().add(jLabelCapacitancePrompt); jLabelCapacitancePrompt.setBounds(420, 390, 110, 16); jLabelTimePrompt.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelTimePrompt.setText("Time (usec):"); getContentPane().add(jLabelTimePrompt); jLabelTimePrompt.setBounds(420, 410, 110, 16); jTextCapacitance.setText("10000"); jTextCapacitance.setNextFocusableComponent(jTextTime); jTextCapacitance.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { actionCapacitance(evt); } }); getContentPane().add(jTextCapacitance); jTextCapacitance.setBounds(550, 388, 100, 20); jTextTime.setText("1000"); jTextTime.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { actionTime(evt); } }); getContentPane().add(jTextTime); jTextTime.setBounds(550, 408, 100, 20); jLabelInductance.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelInductance.setText("Inductance:"); getContentPane().add(jLabelInductance); jLabelInductance.setBounds(420, 430, 110, 16); jLabelAnswer.setFont(new java.awt.Font("Dialog", 0, 12)); jLabelAnswer.setText("(answer)"); getContentPane().add(jLabelAnswer); jLabelAnswer.setBounds(550, 430, 110, 16); }//GEN-END:initComponents private void actionTime(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionTime String sAnswer = computeCustom(jTextTime.getText(), jTextCapacitance.getText()); jLabelAnswer.setText(sAnswer); }//GEN-LAST:event_actionTime private void actionCapacitance(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionCapacitance String sAnswer = computeCustom(jTextTime.getText(), jTextCapacitance.getText()); jLabelAnswer.setText(sAnswer); }//GEN-LAST:event_actionCapacitance /** * After user enters a custom time and capacitor, compute the inductance */ public String computeCustom(String sMicroSeconds, String sMicroFarads) { double farads; double seconds; try { farads = Double.parseDouble(sMicroFarads) / 1E6; seconds = Double.parseDouble(sMicroSeconds) / 1E6; } catch (NumberFormatException doubleTry) { return "not a number"; } double henries = inductor.computeInductance(seconds, farads); return nearest.toStringEng(henries, 3, "H"); } /** * When radio button such as "Zero Crossing Pulse Width" is pushed, * select its inductance formula and update the GUI. */ private void actionFullCycle(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionFullCycle inductor = new Inductor(Inductor.FULL_CYCLE); actionTimeButton(evt, "fullcycle.gif"); }//GEN-LAST:event_actionFullCycle private void actionHalfPower(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionHalfPower inductor = new Inductor(Inductor.HALF_POWER); actionTimeButton(evt, "halfpower.gif"); }//GEN-LAST:event_actionHalfPower private void actionZeroCrossing(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionZeroCrossing inductor = new Inductor(Inductor.ZERO_CROSSING); actionTimeButton(evt, "zerocrossing.gif"); }//GEN-LAST:event_actionZeroCrossing // Helper function for radio buttons // This does all the common processing to update GUI private void actionTimeButton(java.awt.event.ActionEvent evt, String iconName) { // Show image example of timing selected jLabelIcon.setIcon(createImageIcon(iconName)); // Set title above table to exactly match the button's label String p = evt.getActionCommand(); jLabelTime.setText(p); initInductanceTable(); // fill in entire table with inductor values actionCapacitance(evt); // fill in answer for custom entry of cap+time } /** * Scroll time scale right/left */ private void jButtonScrollRightMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonScrollRightMouseClicked scrollTime( SCROLL_RIGHT ); }//GEN-LAST:event_jButtonScrollRightMouseClicked private void jButtonScrollLeftMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonScrollLeftMouseClicked scrollTime( SCROLL_LEFT ); }//GEN-LAST:event_jButtonScrollLeftMouseClicked // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonScrollLeft; private javax.swing.JButton jButtonScrollRight; private javax.swing.JLabel jLabelAnswer; private javax.swing.JLabel jLabelAuthor; private javax.swing.JLabel jLabelCapacitancePrompt; private javax.swing.JLabel jLabelCustom1; private javax.swing.JLabel jLabelCustom2; private javax.swing.JLabel jLabelIcon; private javax.swing.JLabel jLabelInductance; private javax.swing.JLabel jLabelTime; private javax.swing.JLabel jLabelTimeButtons; private javax.swing.JLabel jLabelTimePrompt; private javax.swing.JLabel jLabelTitle; private javax.swing.JLabel jLabelVersion; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable; private javax.swing.JTextField jTextCapacitance; private javax.swing.JTextField jTextTime; private javax.swing.JRadioButton jrbFullCycle; private javax.swing.JRadioButton jrbHalfPower; private javax.swing.JRadioButton jrbZeroCrossing; private javax.swing.ButtonGroup timeButtonGroup; // End of variables declaration//GEN-END:variables // array of capacitors in the leftmost column of the table private Capacitor[] cap; // helper class for formatting numbers private Nearest nearest = new Nearest(); // array of time values (in seconds) in the table public double[] time; // time value (seconds) for each column in table public final int TIME_COLUMNS = 10; // number of columns in table that show "time" private boolean SCROLL_LEFT = false; private boolean SCROLL_RIGHT = true; // input fields for custom values in text entry fields double inputCap = 0.01; double inputTime = 0.001; // pointer to class that calculates inductance // The object is updated when you select a new time interval (zero-crossing, half power, etc.) private Inductor inductor = new Inductor( Inductor.ZERO_CROSSING ); }