class ExpPanel extends Panel
implements ActionListener,
AdjustmentListener {
TScore controller;
TextField exT;
Scrollbar sB;
Label sumL;
int max = 100;
int block = 1;
float ff,ss;
int rr,cc;
ExpPanel(TScore u,String t,
int x,float a,float b,int m,int n) {
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
int tmp_i;
controller = u;
ss = a; ff = b; cc = m; rr = n;
float sf_ratio = (float)ss / (float)ff;
tmp_i = (int)(10000 * sf_ratio);
float sf_percent = (float)tmp_i / (float)100.0;
float sum_ratio = (float)(ss + ((float)rr*ff*sf_ratio/(float)cc)) / (float)(ff*(1.0 + (float)rr/(float)cc));
tmp_i = (int)(10000 * sum_ratio);
float sum_percent = (float)tmp_i / (float)100.0;
String cs = " " + ss + "/" + ff;
cs = cs + " = " + sf_percent + "%";
String r = "remaining " + rr + " sets at (%) ";
String s = " " + sum_percent + "%";
//Set up default layout constraints.
c.fill = GridBagConstraints.NONE;
Label tL = new Label(t, Label.LEFT);
// c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(tL, c);
add(tL);
Label cL = new Label(cs, Label.CENTER);
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(cL, c);
add(cL);
Label sL = new Label("subtotal=", Label.RIGHT);
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 0;
gridbag.setConstraints(sL, c);
add(sL);
sumL = new Label(s, Label.RIGHT);
c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
c.gridx = 4;
c.gridy = 0;
gridbag.setConstraints(sumL, c);
add(sumL);
if(x==1) {
Label rL = new Label(r, Label.RIGHT);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(rL, c);
add(rL);
exT = new TextField("0", 3);
c.anchor = GridBagConstraints.EAST;
c.gridwidth = 1; //The default value.
c.gridx = 2;
c.gridy = 1;
gridbag.setConstraints(exT, c);
add(exT);
exT.addActionListener(this);
c.anchor = GridBagConstraints.CENTER; // default
//Add the slider. It's horizontal, and it has the maximum
//value specified by the instance variable max. Its initial
//and minimum values are the default (0). A click increments
//the value by block units.
sB = new Scrollbar(Scrollbar.HORIZONTAL);
sB.setMaximum(max + 10);
sB.setBlockIncrement(block);
// c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 2;
c.gridx = 3;
c.gridy = 1;
gridbag.setConstraints(sB, c);
add(sB);
sB.addAdjustmentListener(this);
exT.setText(String.valueOf(sf_percent));
sB.setValue((int)sf_percent);
}
}
// Draws a box around this panel.
public void paint(Graphics g) {
Dimension d = getSize();
g.drawRect(0,0, d.width - 1, d.height - 1);
}
public Insets getInsets() {
return new Insets(5,5,5,8);
}
float getSubTotal() {
String s_str = sumL.getText().replace('%','\0').trim();
float s_real = Float.valueOf(s_str).floatValue();
return (s_real);
}
double getValue() {
double f;
try {
f = (double)Double.valueOf(exT.getText()).doubleValue();
} catch (java.lang.NumberFormatException e) {
f = 0.0;
}
return f;
}
// entered into exT
public void actionPerformed(ActionEvent e) {
int tf = (int) getValue();
if (tf > max)
tf = max;
if (tf < 0)
tf = 0;
sB.setValue(tf);
exT.setText(String.valueOf((int)tf));
recalcSumm(tf);
}
// slider sB changed
public void adjustmentValueChanged(AdjustmentEvent e) {
Scrollbar source = (Scrollbar)e.getSource();
int my_i = (int)source.getValue();
exT.setText(String.valueOf(e.getValue()));
recalcSumm(my_i);
}
void recalcSumm(int tf) {
float my_r = (float) tf / (float)100.0;
int tmp_i= (int)(10000*(ss+(rr*ff*my_r/cc))/(float)(ff*(1.0+rr/cc)));
float sum = (float)tmp_i / (float)100.0;
sumL.setText(sum + "%");
controller.recalcTermScore();
}
// Set the values in the slider and text field.
void setValue(double f) {
setSliderValue(f);
exT.setText(String.valueOf((float)f));
}
void setSliderValue(double f) {
int sliderValue = (int)f;
if (sliderValue > max)
sliderValue = max;
if (sliderValue < 0)
sliderValue = 0;
sB.setValue(sliderValue);
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>