import java.awt.event.*; import java.awt.*; import java.awt.geom.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.*; import org.jfree.chart.axis.*; import org.jfree.chart.event.*; import org.jfree.chart.*; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.xy.*; import javax.swing.*; import javax.swing.JApplet; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.*; import java.awt.Color; import java.lang.Math; import java.lang.Double; import java.util.Vector; public class SC extends JApplet implements ActionListener { // Classes for (x,y) data XYSeries data = new XYSeries("Potential DT1"); XYSeries data2 = new XYSeries("Potential DT2"); XYSeriesCollection dataALL = new XYSeriesCollection(); // For plotting JFreeChart chart; ChartPanel chartPanel; // Panels JPanel EBPanel, DT1Panel, DT2Panel, GraphPanel, OutputPanel1, OutputPanel2; // Variables double VDT = 2000.; double rDT = 0.012; double VDT2 = 2000.; double rDT2 = 0.004; double VCATH = -300.; double iEB = 0.01, vEB = 0.01, qEB = 1., rEB = 0.00004; double e0 = 8.85e-12; double twopi = 2.*3.1415926; double qe = 1.602e-19; double me = 9.11e-31; // TextFields/JLabels TextField txtDTRad; JLabel DTRadTag; TextField txtVDT; JLabel VDTTag; TextField txtDTRad2; JLabel DTRadTag2; TextField txtVDT2; JLabel VDTTag2; TextField txtEbeamRad; JLabel EBTag; TextField txtVCATH; JLabel VCATHTag; TextField txtCurrent; JLabel CurrentTag; JLabel V_0, V_rEB, V_rDT; JLabel V_02, V_rEB2, V_rDT2; // Button Button calcButton = new Button("Calc!"); //calcButton.setBackground(Color.lightGray); ChartChangeEvent ChartEvent = new ChartChangeEvent(calcButton,chart); public SC() { LayoutManager Layout; Container cp; cp = getContentPane(); cp.setLayout(new BoxLayout(cp, BoxLayout.PAGE_AXIS)); Layout = new FlowLayout(); EBPanel = new JPanel(); DT1Panel = new JPanel(); DT2Panel = new JPanel(); OutputPanel1 = new JPanel(); OutputPanel2 = new JPanel(); txtDTRad = new TextField(); DTRadTag = new JLabel(); txtVDT = new TextField(); VDTTag = new JLabel(); txtDTRad2 = new TextField(); DTRadTag2 = new JLabel(); txtVDT2 = new TextField(); VDTTag2 = new JLabel(); txtVCATH = new TextField(); VCATHTag = new JLabel(); txtEbeamRad = new TextField(); EBTag = new JLabel(); txtCurrent = new TextField(); CurrentTag = new JLabel(); V_0 = new JLabel(); V_rEB = new JLabel(); V_rDT = new JLabel(); V_02 = new JLabel(); V_rEB2 = new JLabel(); V_rDT2 = new JLabel(); EBPanel.setLayout(Layout); DT1Panel.setLayout(Layout); DT2Panel.setLayout(Layout); OutputPanel1.setLayout(Layout); OutputPanel2.setLayout(Layout); txtDTRad.setColumns(8); txtDTRad.setText(Double.toString(rDT)); DTRadTag.setText("DT1 Rad (m): "); txtVDT.setColumns(8); txtVDT.setText(Double.toString(VDT)); VDTTag.setText("V1_DT (V): "); txtDTRad2.setColumns(8); txtDTRad2.setText(Double.toString(rDT2)); DTRadTag2.setText("DT2 Rad (m): "); txtVDT2.setColumns(8); txtVDT2.setText(Double.toString(VDT2)); VDTTag2.setText("V_DT2 (V): "); EBTag.setText(" E-Beam Rad (m): "); txtEbeamRad.setColumns(8); txtEbeamRad.setText(Double.toString(rEB)); txtVCATH.setColumns(8); txtVCATH.setText(Double.toString(VCATH)); VCATHTag.setText("V_CATH (V): "); txtCurrent.setColumns(8); txtCurrent.setText(Double.toString(iEB)); CurrentTag.setText("Current (A): "); V_0.setText("V1(r=0) = " + Double.toString(f1(0.)) + ", "); V_rEB.setText("V1(r_eb) = " + Double.toString(f1(rEB)) + ", "); V_rDT.setText("V1(r_DT) = " + Double.toString(f1(rDT))); V_02.setText("V2(r=0) = " + Double.toString(f2(0.)) + ", "); V_rEB2.setText("V2(r_eb) = " + Double.toString(f2(rEB)) + ", "); V_rDT2.setText("V2(r_DT) = " + Double.toString(f2(rDT2))); EBPanel.add(calcButton); EBPanel.add(EBTag); EBPanel.add(txtEbeamRad); EBPanel.add(VCATHTag); EBPanel.add(txtVCATH); EBPanel.add(CurrentTag); EBPanel.add(txtCurrent); DT1Panel.add(DTRadTag); DT1Panel.add(txtDTRad); DT1Panel.add(VDTTag); DT1Panel.add(txtVDT); DT2Panel.add(DTRadTag2); DT2Panel.add(txtDTRad2); DT2Panel.add(VDTTag2); DT2Panel.add(txtVDT2); OutputPanel1.add(V_0); OutputPanel1.add(V_rEB); OutputPanel1.add(V_rDT); OutputPanel2.add(V_02); OutputPanel2.add(V_rEB2); OutputPanel2.add(V_rDT2); calcButton.addActionListener(this); //cp.add(EBPanel, BorderLayout.NORTH); //cp.add(DT1Panel,BorderLayout.CENTER); //cp.add(OutputPanel1, BorderLayout.SOUTH); cp.add(EBPanel); cp.add(DT1Panel); cp.add(DT2Panel); cp.add(OutputPanel1); cp.add(OutputPanel2); //cp.add(GraphPanel, BorderLayout.SOUTH); } public void init() { chart = ChartFactory.createXYLineChart( "Space Charge Potential with e-beam", "Radial Distance (cm)", "Voltage (V)", dataALL, PlotOrientation.VERTICAL, true, true, false ); chartPanel = new ChartPanel(chart); chartPanel.setPopupMenu(null); chartPanel.setLayout(new FlowLayout()); Container cp = getContentPane(); cp.add(chartPanel); //chartPanel.setVisible(true); } public void paint(Graphics g) { CreateGraph(chart); chart.fireChartChanged(); // Set the y range XYPlot plot = chart.getXYPlot(); final ValueAxis rangeAxis = (ValueAxis) plot.getRangeAxis(); double ymin, ymax; if( data.getMinY() < data2.getMinY() ) { ymin = data.getMinY(); } else { ymin = data2.getMinY(); } if( data.getMaxY() < data2.getMaxY() ) { ymax = data2.getMaxY(); } else { ymax = data.getMaxY(); } rangeAxis.setRangeWithMargins(ymin,ymax); chartPanel.setChart(chart); chartPanel.repaint(); EBPanel.repaint(); DT1Panel.repaint(); DT2Panel.repaint(); OutputPanel1.repaint(); OutputPanel2.repaint(); V_0.setText("V1(r=0) = " + Double.toString(f1(0.)) + ", "); V_rEB.setText("V1(r_eb) = " + Double.toString(f1(rEB)) + ", "); V_rDT.setText("V1(r_DT) = " + Double.toString(f1(rDT))); V_02.setText("V2(r=0) = " + Double.toString(f2(0.)) + ", "); V_rEB2.setText("V2(r_eb) = " + Double.toString(f2(rEB)) + ", "); V_rDT2.setText("V2(r_DT) = " + Double.toString(f2(rDT2))); } public void actionPerformed (ActionEvent evt) { if(evt.getSource() == calcButton) { updateParams(); repaint(); } } public static void main(String[] args) { run(new SC()); } public static void run(JApplet applet) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(1000,700); applet.init(); applet.start(); frame.setVisible(true); } public void CreateGraph(JFreeChart jChart) { double start = 0., end = rDT; double step = (end - start)/100.; data.clear(); data2.clear(); for(double i = start; i < end; i+=step) { // Convert x points to cm, assign y values data.add(i*100,f1(i)); } start = 0.; end = rDT2; step = (end - start)/100.; for(double i = start; i < end; i += step) { data2.add(i*100,f2(i)); } dataALL.removeSeries(data); dataALL.addSeries(data); dataALL.removeSeries(data2); dataALL.addSeries(data2); chart = ChartFactory.createXYLineChart( "Space Charge Potential with e-beam", "Radial Distance (cm)", "Voltage (V)", dataALL, PlotOrientation.VERTICAL, true, true, false ); } public double f1(double x) { calcParams(); if(x > rEB) { return (VDT + (qEB/(twopi*e0))*Math.log(rDT/x)); } else { return (VDT + (qEB/(twopi*e0))*( (rEB+x)*(rEB-x)/(2.*rEB*rEB) + Math.log(rDT/rEB))); } } public void calcParams() { vEB = Math.sqrt( 2*qe*(VDT - VCATH)/me ); qEB = -Math.abs(iEB/vEB); } public void updateParams() { rEB = Double.valueOf(txtEbeamRad.getText()); VCATH = Double.valueOf(txtVCATH.getText()); iEB = Double.valueOf(txtCurrent.getText()); rDT = Double.valueOf(txtDTRad.getText()); VDT = Double.valueOf(txtVDT.getText()); rDT2 = Double.valueOf(txtDTRad2.getText()); VDT2 = Double.valueOf(txtVDT2.getText()); } public double f2(double x) { calcParams2(); if(x > rEB) { return (VDT2 + (qEB/(twopi*e0))*Math.log(rDT2/x)); } else { return (VDT2 + (qEB/(twopi*e0))*( (rEB+x)*(rEB-x)/(2.*rEB*rEB) + Math.log(rDT2/rEB))); } } public void calcParams2() { vEB = Math.sqrt( 2*qe*(VDT2 - VCATH)/me ); qEB = -Math.abs(iEB/vEB); } }