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; public class SC extends JApplet implements ActionListener { // Classes for (x,y) data XYSeries data = new XYSeries("Potential"); XYSeriesCollection data2 = new XYSeriesCollection(); // For plotting JFreeChart chart; ChartPanel chartPanel; // Panels JPanel InputPanel, GraphPanel, OutputPanel; // Variables double VDT = 2000., VCATH = 500.; double rDT = 0.010, rEB = 0.00004; double iEB = 0.01, vEB = 0.01, qEB = 1.; double e0 = 8.85e-12; double twopi = 2.*3.1415926; double qe = 1.602e-19; double me = 9.11e-31; // TextFields/JLabels TextField txtEbeamRad; JLabel EBTag; TextField txtDTRad; JLabel DTRadTag; TextField txtVDT; JLabel VDTTag; TextField txtVCATH; JLabel VCATHTag; TextField txtCurrent; JLabel CurrentTag; JLabel V_0, V_rEB, V_rDT; // Button Button calcButton = new Button("Calc!"); //calcButton.setBackground(Color.lightGray); ChartChangeEvent ChartEvent = new ChartChangeEvent(calcButton,chart); public SC() { LayoutManager Layout; Container cp; cp = getContentPane(); Layout = new FlowLayout(); InputPanel = new JPanel(); OutputPanel = new JPanel(); txtEbeamRad = new TextField(); EBTag = new JLabel(); txtDTRad = new TextField(); DTRadTag = new JLabel(); txtVDT = new TextField(); VDTTag = new JLabel(); txtVCATH = new TextField(); VCATHTag = new JLabel(); txtCurrent = new TextField(); CurrentTag = new JLabel(); V_0 = new JLabel(); V_rEB = new JLabel(); V_rDT = new JLabel(); InputPanel.setLayout(Layout); OutputPanel.setLayout(Layout); txtEbeamRad.setColumns(8); txtEbeamRad.setText(Double.toString(rEB)); EBTag.setText(" E-Beam Rad (m): "); txtDTRad.setColumns(8); txtDTRad.setText(Double.toString(rDT)); DTRadTag.setText("DT Rad (m): "); txtVDT.setColumns(8); txtVDT.setText(Double.toString(VDT)); VDTTag.setText("V_DT (V): "); 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("V(r=0) = " + Double.toString(f(0.)) + ", "); V_rEB.setText("V(r_eb) = " + Double.toString(f(rEB)) + ", "); V_rDT.setText("V(r_DT) = " + Double.toString(f(rDT))); InputPanel.add(calcButton); InputPanel.add(EBTag); InputPanel.add(txtEbeamRad); InputPanel.add(DTRadTag); InputPanel.add(txtDTRad); InputPanel.add(VDTTag); InputPanel.add(txtVDT); InputPanel.add(VCATHTag); InputPanel.add(txtVCATH); InputPanel.add(CurrentTag); InputPanel.add(txtCurrent); OutputPanel.add(V_0); OutputPanel.add(V_rEB); OutputPanel.add(V_rDT); calcButton.addActionListener(this); cp.add(InputPanel, BorderLayout.NORTH); cp.add(OutputPanel, BorderLayout.SOUTH); //cp.add(GraphPanel, BorderLayout.SOUTH); } public void init() { chart = ChartFactory.createXYLineChart( "Space Charge Potential with e-beam", "Radial Distance (cm)", "Voltage (V)", data2, PlotOrientation.VERTICAL, true, true, false ); chartPanel = new ChartPanel(chart); chartPanel.setPopupMenu(null); 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(); rangeAxis.setRangeWithMargins(data.getMinY(),data.getMaxY()); chartPanel.setChart(chart); chartPanel.repaint(); InputPanel.repaint(); OutputPanel.repaint();; V_0.setText("V(r=0) = " + Double.toString(f(0.)) + ", "); V_rEB.setText("V(r_eb) = " + Double.toString(f(rEB)) + ", "); V_rDT.setText("V(r_DT) = " + Double.toString(f(rDT))); } 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(); for(double i = start; i < end; i+=step) { // Convert x points to cm, assign y values data.add(i*100,f(i)); } data2.removeSeries(data); data2.addSeries(data); chart = ChartFactory.createXYLineChart( "Space Charge Potential with e-beam", "Radial Distance (cm)", "Voltage (V)", data2, PlotOrientation.VERTICAL, true, true, false ); } public double f(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()); rDT = Double.valueOf(txtDTRad.getText()); VDT = Double.valueOf(txtVDT.getText()); VCATH = Double.valueOf(txtVCATH.getText()); iEB = Double.valueOf(txtCurrent.getText()); } }