// "Java Tech"
// Code provided with book for educational purposes only.
// No warranty or guarantee implied.
// This code freely available. No copyright claimed.
// 2003
//
// Begun from StartJApplet11
// Include the tag here for testing with the Appletviewer
/*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Create a random distribution of values and then fit a
* polynominal to the bin values.
*
* The applet uses the HistPanel to display contents of
* an instance of Histogram. HistFormat used by HistPanel to
* format the scale values.
*
* This program will run as an applet inside
* an application frame.
*
* Includes "Go" button to add random values from a Gaussian
* distribution to the histogram. The number of values taken from
* entry in a JTextField. "Clear" button clears the histogram.
* In standalone mode, the Exit button closes the program.
*
**/
public class HistFitApplet extends JApplet
implements ActionListener
{
// Use the HistPanel JPanel subclass here
HistErrPanel fOutputPanel;
HistogramStatR1 fHistogram;
DrawFunction [] fDrawFunctions;
double [] fFitParameters;
int fNumDataPoints = 10000;
// Set the angular range for scattered tracks
double fMinAngle;
double fMaxAngle;
// Random number generator
java.util.Random fRan;
// A text field for input strings
JTextField fTextField;
// Flag for whether the applet is in a browser
// or running via the main () below.
boolean fInBrowser=true;
//Buttons
JButton fGoButton;
JButton fClearButton;
JButton fExitButton;
/**
* Create a User Interface with histograms and buttons to
* control the program. A textfield holds number of entries
* to be generated for the histogram.
**/
public void init () {
JPanel panel = new JPanel (new BorderLayout ());
// Create a histogram with Gaussian distribution.
makeHist ();
// Create the Histogram drawing panel and include
// drawing objects.
fDrawFunctions = new DrawFunction[1];
fDrawFunctions[0] = new DrawPoly ();
fOutputPanel = new HistErrPanel (fHistogram, fDrawFunctions);
fOutputPanel.setDrawType (HistErrPanel.DRAW_PTS_ERRS);
// Fit the line to the histogram.
fitHist ();
panel.add (fOutputPanel,"Center");
// Use a textfield for an input parameter.
fTextField =
new JTextField (Integer.toString (fNumDataPoints), 10);
// If return hit after entering text, the
// actionPerformed will be invoked.
fTextField.addActionListener (this);
fGoButton = new JButton ("Go");
fGoButton.addActionListener (this);
fClearButton = new JButton ("Clear");
fClearButton.addActionListener (this);
fExitButton = new JButton ("Exit");
fExitButton.addActionListener (this);
JPanel controlPanel = new JPanel ();
controlPanel.add (fTextField);
controlPanel.add (fGoButton);
controlPanel.add (fClearButton);
controlPanel.add (fExitButton);
if (fInBrowser) fExitButton.setEnabled (false);
panel.add (controlPanel,"South");
// Add text area with scrolling to the contentPane.
add (panel);
} // init
/**
* Respond to the buttons.
**/
public void actionPerformed (ActionEvent e) {
Object source = e.getSource ();
if (source == fGoButton || source == fTextField) {
String strNumDataPoints = fTextField.getText ();
try {
fNumDataPoints = Integer.parseInt (strNumDataPoints);
}
catch (NumberFormatException ex) {
// Could open an error dialog here but just
// display a message on the browser status line.
showStatus ("Bad input value");
return;
}
makeHist ();
fitHist ();
repaint ();
}
else if (source == fClearButton) {
fHistogram.clear ();
repaint ();
}
else if (!fInBrowser)
System.exit (0);
} // actionPeformed
/**
* Create a random distribution of angles to
* simulate the tracks in a particle scattering
* experiment entering a detector element.
**/
void makeHist () {
// Create an instance of the Random class for
// producing our random values.
fRan = new java.util.Random ();
// Could modify the program to select these via the GUI
fMinAngle = 25.0;
fMaxAngle = 50.0;
// Create an instance of our basic histogram class.
// Make it wide enough enough to include most of the
// gaussian values.
if (fHistogram == null)
fHistogram = new HistogramStatR1 ("Scatter Angles into a Detector",
"Degrees",
25,fMinAngle,fMaxAngle);
// Use the method to generate a
// quadratic distribution.
for (int i=0; i < fNumDataPoints; i++) {
// Generate random vals 0.0 to 1.0
double r1 = fRan.nextDouble ();
double angle = fMinAngle + (fMaxAngle-fMinAngle)*r1;
if (acceptAngle (angle)){
fHistogram.add (angle);
}
}
// Make the errors from the bin contents
fHistogram.makeBinErrors ();
} // makeHist
/**
* This rejection function uses a quadratic
* function to determine the acceptance of the
* angle.
**/
boolean acceptAngle (double angle){
if ( angle < fMinAngle || angle > fMaxAngle) return false;
double c = 0.2;
double slope = 0.020;
double non_lin_factor = 0.0007;
double limit = c + (angle - fMinAngle) * slope -
(angle - fMinAngle) * (angle - fMinAngle) * non_lin_factor;
double r2 = fRan.nextDouble ();
if ( r2 <= limit) return true;
return false;
} // acceptAngle
/**
* Fit a polynominal to the hist data.
**/
void fitHist () {
// Use it to fit the histogram data.
fFitParameters = HistogramFit.fit (fHistogram, 3);
// Pass the parameters to the drawing function object.
fDrawFunctions[0].setParameters (fFitParameters,null);
} // fitHist
public static void main (String[] args)
{
//
int frame_width=450;
int frame_height=300;
//
HistFitApplet applet = new HistFitApplet ();
applet.fInBrowser = false;
applet.init ();
// Following anonymous class used to close window & exit program
JFrame f = new JFrame ("Demo");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Add applet to the frame
f.getContentPane ().add ( applet);
f.setSize (new Dimension (frame_width,frame_height));
f.setVisible (true);
} // main
} // HistFitApplet