class ExmPanel extends Panel
implements ActionListener,
AdjustmentListener {
TScore controller;
TextField exT_ex, exT_pc;
Scrollbar sB_ex, sB_pc;
Label sumL;
int max = 100;
int block = 1;
int show_pc, exam_ex, epc_ex;
float s_ex, f_ex, s_pc, f_pc;
float c_factor;
// exam_type 0 Final
// 1, 2, 3, 4
ExmPanel(TScore u,int exam_type,int exam_x,int show_epc,int epc_x,
float a,float b,float w,float m,float n,float ex_default) {
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
Label tL;
float exam_ratio, epc_ratio;
float sum;
int tmp_i;
controller = u;
c_factor = w;
show_pc = show_epc;
exam_ex = exam_x;
epc_ex = epc_x;
s_ex = a; f_ex = b; s_pc = m; f_pc = n;
//Set up default layout constraints.
c.fill = GridBagConstraints.NONE;
//Add the label. It displays this panel's title, centered.
if(exam_type == 0 ) { // Final exam
tL = new Label("Final", Label.LEFT);
} else {
tL = new Label("Exam " + exam_type, Label.LEFT);
}
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(tL, c);
add(tL);
c.anchor = GridBagConstraints.CENTER;
if( exam_x == 0 ) {
// exam not extrapolated
exam_ratio = (float)s_ex / (float)f_ex;
tmp_i = (int) (10000 * exam_ratio );
float percent = (float)tmp_i / (float)100.0;
String cs = " " + a + "/" + b + " = " + percent + " %";
Label cL = new Label(cs, Label.CENTER);
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(cL, c);
add(cL);
} else {
// extrapolate exam
exam_ratio = ex_default;
exT_ex = new TextField("0", 3);
exT_ex.setName("EXAM");
c.gridwidth = 1; //The default value.
c.gridx = 2;
c.gridy = 0;
gridbag.setConstraints(exT_ex, c);
add(exT_ex);
exT_ex.addActionListener(this);
sB_ex = new Scrollbar(Scrollbar.HORIZONTAL);
sB_ex.setName("EXAMS");
sB_ex.setMaximum(max + 10);
sB_ex.setBlockIncrement(block);
c.gridwidth = 2;
c.gridx = 3;
c.gridy = 0;
gridbag.setConstraints(sB_ex, c);
add(sB_ex);
sB_ex.addAdjustmentListener(this);
}
epc_ratio = exam_ratio;
if(show_pc == 1) {
if(epc_x == 1) {
epc_ratio = exam_ratio;
} else {
epc_ratio = (float)s_pc / (float)f_pc;
}
if( epc_ratio > exam_ratio ) {
tmp_i= (int)(10000 * (exam_ratio + c_factor * (epc_ratio - exam_ratio)));
sum = (float)tmp_i / (float)100.0;
} else {
tmp_i= (int)(10000 * exam_ratio);
sum = (float)tmp_i / (float)100.0;
}
} else {
tmp_i= (int)(10000 * exam_ratio);
sum = (float)tmp_i / (float)100.0;
}
Label sL = new Label("subtotal=", Label.RIGHT);
c.gridwidth = 1;
c.gridx = 5;
c.gridy = 0;
gridbag.setConstraints(sL, c);
add(sL);
String s = " " + sum + "%";
sumL = new Label(s, Label.CENTER);
c.anchor = GridBagConstraints.EAST;
c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
c.gridx = 6;
c.gridy = 0;
gridbag.setConstraints(sumL, c);
add(sumL);
c.anchor = GridBagConstraints.CENTER;
if( show_epc == 1 ) {
Label eL = new Label("Correction " + exam_type, Label.LEFT);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(eL, c);
add(eL);
if( epc_x == 0 ) {
float ratio = (float) ( s_pc / f_pc);
tmp_i = (int) (10000 * ratio );
float percent = (float)tmp_i / (float)100.0;
String cs = " " + s_pc + "/" + f_pc + " = " + percent + " %";
Label cL = new Label(cs, Label.CENTER);
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 1;
gridbag.setConstraints(cL, c);
add(cL);
} else {
exT_pc = new TextField("0", 3);
exT_pc.setName("PC");
c.anchor = GridBagConstraints.EAST;
c.gridwidth = 1; //The default value.
c.gridx = 2;
c.gridy = 1;
gridbag.setConstraints(exT_pc, c);
add(exT_pc);
exT_pc.addActionListener(this);
sB_pc = new Scrollbar(Scrollbar.HORIZONTAL);
sB_pc.setName("PCS");
sB_pc.setMaximum(max + 10);
sB_pc.setBlockIncrement(block);
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.HORIZONTAL;
// c.gridwidth = 2;
c.gridx = 3;
c.gridy = 1;
gridbag.setConstraints(sB_pc, c);
add(sB_pc);
sB_pc.addAdjustmentListener(this);
}
}
setExValue(exam_ratio,epc_ratio);
}
// 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);
}
// entered into exT
public void actionPerformed(ActionEvent e) {
TextField source = (TextField)e.getSource();
String name = source.getName();
int tf = (int)Integer.valueOf(source.getText()).intValue();
if (tf > max)
tf = max;
if (tf < 0)
tf = 0;
if( name.equals(String.valueOf("EXAM")) ) {
sB_ex.setValue(tf);
exT_ex.setText(String.valueOf((int)tf));
} else {
sB_pc.setValue(tf);
exT_pc.setText(String.valueOf((int)tf));
}
recalcSumm();
}
// slider sB changed
public void adjustmentValueChanged(AdjustmentEvent e) {
Scrollbar source = (Scrollbar)e.getSource();
String name = source.getName();
int my_i = (int)source.getValue();
if( name.equals(String.valueOf("EXAMS")) ) {
sB_ex.setValue(my_i);
exT_ex.setText(String.valueOf((int)my_i));
} else {
sB_pc.setValue(my_i);
exT_pc.setText(String.valueOf((int)my_i));
}
recalcSumm();
}
void recalcSumm() {
int tmp_i;
float exam , epc, sum;
if( exam_ex == 1 ) {
tmp_i = (int)Integer.valueOf(exT_ex.getText()).intValue();
exam = (float) tmp_i / (float)100.0;
} else {
exam = (float)s_ex / (float)f_ex;
}
if( show_pc == 1 ) {
if(epc_ex == 1) {
tmp_i = (int)Integer.valueOf(exT_pc.getText()).intValue();
epc = (float) tmp_i / (float)100.0;
} else {
epc = (float)s_pc / (float)f_pc;
}
if( epc > exam ) {
tmp_i= (int)(10000 * (exam + c_factor * (epc - exam)));
sum = (float)tmp_i / (float)100.0;
} else {
tmp_i= (int)(10000 * exam);
sum = (float)tmp_i / (float)100.0;
}
} else {
tmp_i= (int)(10000 * exam);
sum = (float)tmp_i / (float)100.0;
}
sumL.setText(sum + "%");
controller.recalcTermScore();
}
void setExValue (float a, float b) {
int exm, epc;
exm = (int) (a*100.0);
epc = (int) (b*100.0);
if (exm > max)
exm = max;
if (exm < 0)
exm = 0;
if (epc > max)
epc = max;
if (epc < 0)
epc = 0;
if( exam_ex ==1 ) {
sB_ex.setValue(exm);
exT_ex.setText(String.valueOf(exm));
}
if( (show_pc == 1) && (epc_ex==1) ) {
sB_pc.setValue(epc);
exT_pc.setText(String.valueOf(epc));
}
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>