Java tutorial
/* * 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 RLCcircuit; import java.awt.Color; import java.awt.Graphics; import java.util.Timer; import java.util.TimerTask; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** * * @author Jangbae */ public class RLCcircuitJFrame extends javax.swing.JFrame { /** * Creates new form RLCcircuitJFrame */ double pi = Math.PI; double time = 0; boolean StartOrStop = false; double inputVoltage = 0; double voltage0 = 5; double current; double current0; double phaseAngle; double frequency = 50; //frequency defalt(Hz) double Rvoltage, Lvoltage, Cvoltage; double R = 5; //resistance defalt(ohm) double L = 50 * Math.pow(10, -3); //inductance defalt(H) double C = 500 * Math.pow(10, -6); //capacitance defalt(F) double impedance; double angularFrequency; double Lreactance; double Creactance; public RLCcircuitJFrame() { initComponents(); } Timer timer; TimerTask task; public void VoltageInput(boolean StartOrStop) { if (!StartOrStop) { if (task != null) { task.cancel(); task = null; } return; } if (timer == null) timer = new Timer(); // time = 0; task = new TimerTask() { public void run() { graphPanel.repaint(); time = time + 1; } }; timer.scheduleAtFixedRate(task, 0, 100); } public void ValueCaculate(double time) { angularFrequency = 2 * pi * frequency; inputVoltage = voltage0 * Math.sin(angularFrequency * time); phaseAngle = Math.atan((Lreactance - Creactance) / R) * 180 / pi; Lreactance = angularFrequency * L; Creactance = 1 / angularFrequency / C; impedance = Math.sqrt(Math.pow(R, 2) + Math.pow(2 * pi * frequency * L - 1 / (2 * pi * frequency * C), 2)); current0 = voltage0 / impedance; current = current0 * Math.sin(angularFrequency * time + phaseAngle); Rvoltage = current0 * R * Math.sin(angularFrequency * time); Lvoltage = current0 * Lreactance * Math.sin(angularFrequency * time - pi / 2); Cvoltage = current0 * Creactance * Math.sin(angularFrequency * time + pi / 2); } public JFreeChart getResultChart() { XYSeries series1 = new XYSeries("Resistor"); XYSeries series2 = new XYSeries("Inductor"); XYSeries series3 = new XYSeries("Capacitor"); for (double i = 0 + time; i < 100 + time; i++) { ValueCaculate(i / 1000.); series1.add(i, Rvoltage); } for (double i = 0 + time; i < 100 + time; i++) { ValueCaculate(i / 1000.); series2.add(i, Lvoltage); } for (double i = 0 + time; i < 100 + time; i++) { ValueCaculate(-i / 1000.); series3.add(i, Cvoltage); } XYSeriesCollection data1 = new XYSeriesCollection(series1); XYSeriesCollection data2 = new XYSeriesCollection(series2); XYSeriesCollection data3 = new XYSeriesCollection(series3); final JFreeChart chart = ChartFactory.createXYLineChart("Time-Voltage Graph", "Time", "Voltage", data1, PlotOrientation.VERTICAL, true, false, false); chart.setBackgroundPaint(null); XYPlot plot = chart.getXYPlot(); plot.setDataset(1, data2); plot.setDataset(2, data3); XYLineAndShapeRenderer Renderer1 = new XYLineAndShapeRenderer(); XYLineAndShapeRenderer Renderer2 = new XYLineAndShapeRenderer(); XYLineAndShapeRenderer Renderer3 = new XYLineAndShapeRenderer(); plot.setRenderer(0, Renderer1); plot.setRenderer(1, Renderer2); plot.setRenderer(2, Renderer3); Renderer1.setSeriesShapesVisible(0, false); Renderer2.setSeriesShapesVisible(0, false); Renderer3.setSeriesShapesVisible(0, false); plot.setBackgroundPaint(Color.black); plot.getDomainAxis().setVisible(false); plot.getRangeAxis().setVisible(false); plot.getRangeAxis().setRange(-20, 20); //chart.plotChanged(new PlotChangeEvent(plot)); return chart; } public class GraphViewPanel extends ChartPanel { public GraphViewPanel(JFreeChart chart) { super(chart); } public void paintComponent(Graphics g2) { super.paintComponent(g2); this.setChart(getResultChart()); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { backgroundPanel = new javax.swing.JPanel(); displayPanel = new javax.swing.JPanel(); circuitImage = new javax.swing.JLabel(); graphPanel = new GraphViewPanel(getResultChart()); impedanceLabel = new javax.swing.JLabel(); phaseAngleLabel = new javax.swing.JLabel(); impedanceNumber = new javax.swing.JLabel(); phaseAngleNumber = new javax.swing.JLabel(); phaseAngleUnit = new javax.swing.JLabel(); impedanceUnit = new javax.swing.JLabel(); settingPanel = new javax.swing.JPanel(); startButton = new javax.swing.JButton(); resistanceLabel = new javax.swing.JLabel(); resistanceSlider = new javax.swing.JSlider(); resistanceNumber = new javax.swing.JTextField(); inductanceLabel = new javax.swing.JLabel(); inductanceSlider = new javax.swing.JSlider(); inductanceNumber = new javax.swing.JTextField(); capacitanceSlider = new javax.swing.JSlider(); capacitanceLabel = new javax.swing.JLabel(); capacitanceNumber = new javax.swing.JTextField(); voltageLabel = new javax.swing.JLabel(); voltageNumber = new javax.swing.JTextField(); voltageSlider = new javax.swing.JSlider(); frequencyLabel = new javax.swing.JLabel(); frequencyNumber = new javax.swing.JTextField(); frequencySlider = new javax.swing.JSlider(); resistanceUnit = new javax.swing.JLabel(); inductanceUnit = new javax.swing.JLabel(); capacitanceUnit = new javax.swing.JLabel(); voltageUnit = new javax.swing.JLabel(); frequencyUnit = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBounds(new java.awt.Rectangle(0, 0, 600, 500)); setMinimumSize(new java.awt.Dimension(600, 500)); backgroundPanel.setMinimumSize(new java.awt.Dimension(600, 500)); backgroundPanel.setLayout(null); displayPanel.setLayout(null); circuitImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RLCcircuit/RLC.png"))); // NOI18N displayPanel.add(circuitImage); circuitImage.setBounds(10, 20, 300, 160); graphPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); javax.swing.GroupLayout graphPanelLayout = new javax.swing.GroupLayout(graphPanel); graphPanel.setLayout(graphPanelLayout); graphPanelLayout.setHorizontalGroup(graphPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 396, Short.MAX_VALUE)); graphPanelLayout.setVerticalGroup(graphPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 286, Short.MAX_VALUE)); displayPanel.add(graphPanel); graphPanel.setBounds(0, 210, 400, 290); impedanceLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); impedanceLabel.setText("Impedance:"); displayPanel.add(impedanceLabel); impedanceLabel.setBounds(240, 150, 80, 15); phaseAngleLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); phaseAngleLabel.setText("Phase angle:"); displayPanel.add(phaseAngleLabel); phaseAngleLabel.setBounds(240, 170, 80, 15); impedanceNumber.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); impedanceNumber.setText("0"); displayPanel.add(impedanceNumber); impedanceNumber.setBounds(330, 150, 50, 15); phaseAngleNumber.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); phaseAngleNumber.setText("0"); displayPanel.add(phaseAngleNumber); phaseAngleNumber.setBounds(330, 170, 50, 15); phaseAngleUnit.setText(" "); displayPanel.add(phaseAngleUnit); phaseAngleUnit.setBounds(380, 170, 20, 15); impedanceUnit.setText(""); displayPanel.add(impedanceUnit); impedanceUnit.setBounds(380, 150, 10, 15); backgroundPanel.add(displayPanel); displayPanel.setBounds(0, 0, 400, 500); settingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Setting")); settingPanel.setLayout(null); startButton.setText("Start"); startButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startButtonActionPerformed(evt); } }); settingPanel.add(startButton); startButton.setBounds(80, 40, 57, 23); resistanceLabel.setText("Resistance"); settingPanel.add(resistanceLabel); resistanceLabel.setBounds(70, 100, 70, 15); resistanceSlider.setMinimum(1); resistanceSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { resistanceSliderStateChanged(evt); } }); settingPanel.add(resistanceSlider); resistanceSlider.setBounds(10, 120, 120, 23); resistanceNumber.setEditable(false); resistanceNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT); resistanceNumber.setText("5.0"); settingPanel.add(resistanceNumber); resistanceNumber.setBounds(130, 120, 40, 30); inductanceLabel.setText("Inductance"); settingPanel.add(inductanceLabel); inductanceLabel.setBounds(70, 150, 70, 15); inductanceSlider.setMinimum(1); inductanceSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { inductanceSliderStateChanged(evt); } }); settingPanel.add(inductanceSlider); inductanceSlider.setBounds(10, 170, 120, 23); inductanceNumber.setEditable(false); inductanceNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT); inductanceNumber.setText("50"); settingPanel.add(inductanceNumber); inductanceNumber.setBounds(130, 170, 40, 30); capacitanceSlider.setMaximum(1000); capacitanceSlider.setMinimum(1); capacitanceSlider.setValue(500); capacitanceSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { capacitanceSliderStateChanged(evt); } }); settingPanel.add(capacitanceSlider); capacitanceSlider.setBounds(10, 220, 120, 23); capacitanceLabel.setText("Capacitance"); settingPanel.add(capacitanceLabel); capacitanceLabel.setBounds(70, 200, 80, 15); capacitanceNumber.setEditable(false); capacitanceNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT); capacitanceNumber.setText("500"); settingPanel.add(capacitanceNumber); capacitanceNumber.setBounds(130, 220, 40, 30); voltageLabel.setFont(new java.awt.Font("", 1, 12)); // NOI18N voltageLabel.setText("Voltage"); settingPanel.add(voltageLabel); voltageLabel.setBounds(80, 290, 50, 15); voltageNumber.setEditable(false); voltageNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT); voltageNumber.setText("5"); settingPanel.add(voltageNumber); voltageNumber.setBounds(130, 320, 40, 30); voltageSlider.setMaximum(10); voltageSlider.setMinimum(1); voltageSlider.setPaintTicks(true); voltageSlider.setValue(5); voltageSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { voltageSliderStateChanged(evt); } }); settingPanel.add(voltageSlider); voltageSlider.setBounds(10, 320, 120, 30); frequencyLabel.setFont(new java.awt.Font("", 1, 12)); // NOI18N frequencyLabel.setText("Frequency"); settingPanel.add(frequencyLabel); frequencyLabel.setBounds(70, 360, 70, 15); frequencyNumber.setEditable(false); frequencyNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT); frequencyNumber.setText("50"); settingPanel.add(frequencyNumber); frequencyNumber.setBounds(130, 390, 40, 30); frequencySlider.setMinimum(10); frequencySlider.setPaintTicks(true); frequencySlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { frequencySliderStateChanged(evt); } }); settingPanel.add(frequencySlider); frequencySlider.setBounds(10, 390, 120, 31); resistanceUnit.setText(""); settingPanel.add(resistanceUnit); resistanceUnit.setBounds(170, 120, 20, 20); inductanceUnit.setText("mH"); settingPanel.add(inductanceUnit); inductanceUnit.setBounds(170, 170, 20, 20); capacitanceUnit.setText("F"); settingPanel.add(capacitanceUnit); capacitanceUnit.setBounds(170, 220, 20, 20); voltageUnit.setText("V"); settingPanel.add(voltageUnit); voltageUnit.setBounds(170, 320, 20, 20); frequencyUnit.setText("Hz"); settingPanel.add(frequencyUnit); frequencyUnit.setBounds(170, 390, 20, 20); backgroundPanel.add(settingPanel); settingPanel.setBounds(400, 0, 200, 500); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(backgroundPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(backgroundPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)); pack(); }// </editor-fold>//GEN-END:initComponents private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed StartOrStop = !StartOrStop; VoltageInput(StartOrStop); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); if (StartOrStop) { startButton.setText("Stop"); } else { startButton.setText("Start"); } }//GEN-LAST:event_startButtonActionPerformed private void resistanceSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_resistanceSliderStateChanged R = resistanceSlider.getValue() / 10.; resistanceNumber.setText(String.valueOf(resistanceSlider.getValue() / 10.)); repaint(); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); }//GEN-LAST:event_resistanceSliderStateChanged private void inductanceSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_inductanceSliderStateChanged L = inductanceSlider.getValue() * Math.pow(10, -3); inductanceNumber.setText(String.valueOf(inductanceSlider.getValue())); repaint(); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); }//GEN-LAST:event_inductanceSliderStateChanged private void capacitanceSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_capacitanceSliderStateChanged C = capacitanceSlider.getValue() * Math.pow(10, -6); capacitanceNumber.setText(String.valueOf(capacitanceSlider.getValue())); repaint(); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); }//GEN-LAST:event_capacitanceSliderStateChanged private void voltageSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_voltageSliderStateChanged voltage0 = voltageSlider.getValue(); voltageNumber.setText(String.valueOf(voltageSlider.getValue())); repaint(); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); }//GEN-LAST:event_voltageSliderStateChanged private void frequencySliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_frequencySliderStateChanged frequency = frequencySlider.getValue(); frequencyNumber.setText(String.valueOf(frequencySlider.getValue())); repaint(); impedanceNumber.setText(String.valueOf(Math.round(impedance * 100) / 100.)); phaseAngleNumber.setText(String.valueOf(Math.round(phaseAngle * 100) / 100.)); }//GEN-LAST:event_frequencySliderStateChanged /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(RLCcircuitJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(RLCcircuitJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(RLCcircuitJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(RLCcircuitJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RLCcircuitJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel backgroundPanel; private javax.swing.JLabel capacitanceLabel; private javax.swing.JTextField capacitanceNumber; private javax.swing.JSlider capacitanceSlider; private javax.swing.JLabel capacitanceUnit; private javax.swing.JLabel circuitImage; private javax.swing.JPanel displayPanel; private javax.swing.JLabel frequencyLabel; private javax.swing.JTextField frequencyNumber; private javax.swing.JSlider frequencySlider; private javax.swing.JLabel frequencyUnit; private javax.swing.JPanel graphPanel; private javax.swing.JLabel impedanceLabel; private javax.swing.JLabel impedanceNumber; private javax.swing.JLabel impedanceUnit; private javax.swing.JLabel inductanceLabel; private javax.swing.JTextField inductanceNumber; private javax.swing.JSlider inductanceSlider; private javax.swing.JLabel inductanceUnit; private javax.swing.JLabel phaseAngleLabel; private javax.swing.JLabel phaseAngleNumber; private javax.swing.JLabel phaseAngleUnit; private javax.swing.JLabel resistanceLabel; private javax.swing.JTextField resistanceNumber; private javax.swing.JSlider resistanceSlider; private javax.swing.JLabel resistanceUnit; private javax.swing.JPanel settingPanel; private javax.swing.JButton startButton; private javax.swing.JLabel voltageLabel; private javax.swing.JTextField voltageNumber; private javax.swing.JSlider voltageSlider; private javax.swing.JLabel voltageUnit; // End of variables declaration//GEN-END:variables }