Annotation of capa/capa51/JavaTools/ExpPanel.java, revision 1.1
1.1 ! albertel 1: class ExpPanel extends Panel
! 2: implements ActionListener,
! 3: AdjustmentListener {
! 4: TScore controller;
! 5: TextField exT;
! 6: Scrollbar sB;
! 7: Label sumL;
! 8: int max = 100;
! 9: int block = 1;
! 10: float ff,ss;
! 11: int rr,cc;
! 12:
! 13: ExpPanel(TScore u,String t,
! 14: int x,float a,float b,int m,int n) {
! 15: GridBagConstraints c = new GridBagConstraints();
! 16: GridBagLayout gridbag = new GridBagLayout();
! 17: setLayout(gridbag);
! 18: int tmp_i;
! 19:
! 20: controller = u;
! 21: ss = a; ff = b; cc = m; rr = n;
! 22: float sf_ratio = (float)ss / (float)ff;
! 23: tmp_i = (int)(10000 * sf_ratio);
! 24: float sf_percent = (float)tmp_i / (float)100.0;
! 25: float sum_ratio = (float)(ss + ((float)rr*ff*sf_ratio/(float)cc)) / (float)(ff*(1.0 + (float)rr/(float)cc));
! 26: tmp_i = (int)(10000 * sum_ratio);
! 27: float sum_percent = (float)tmp_i / (float)100.0;
! 28:
! 29: String cs = " " + ss + "/" + ff;
! 30: cs = cs + " = " + sf_percent + "%";
! 31: String r = "remaining " + rr + " sets at (%) ";
! 32: String s = " " + sum_percent + "%";
! 33:
! 34: //Set up default layout constraints.
! 35: c.fill = GridBagConstraints.NONE;
! 36:
! 37: Label tL = new Label(t, Label.LEFT);
! 38: // c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
! 39: c.gridwidth = 1;
! 40: c.gridx = 0;
! 41: c.gridy = 0;
! 42: gridbag.setConstraints(tL, c);
! 43: add(tL);
! 44:
! 45: Label cL = new Label(cs, Label.CENTER);
! 46: c.gridwidth = 2;
! 47: c.gridx = 1;
! 48: c.gridy = 0;
! 49: gridbag.setConstraints(cL, c);
! 50: add(cL);
! 51:
! 52: Label sL = new Label("subtotal=", Label.RIGHT);
! 53: c.gridwidth = 1;
! 54: c.gridx = 3;
! 55: c.gridy = 0;
! 56: gridbag.setConstraints(sL, c);
! 57: add(sL);
! 58:
! 59: sumL = new Label(s, Label.RIGHT);
! 60: c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
! 61: c.gridx = 4;
! 62: c.gridy = 0;
! 63: gridbag.setConstraints(sumL, c);
! 64: add(sumL);
! 65: if(x==1) {
! 66: Label rL = new Label(r, Label.RIGHT);
! 67: c.gridwidth = 2;
! 68: c.gridx = 0;
! 69: c.gridy = 1;
! 70: gridbag.setConstraints(rL, c);
! 71: add(rL);
! 72:
! 73: exT = new TextField("0", 3);
! 74: c.anchor = GridBagConstraints.EAST;
! 75: c.gridwidth = 1; //The default value.
! 76: c.gridx = 2;
! 77: c.gridy = 1;
! 78: gridbag.setConstraints(exT, c);
! 79: add(exT);
! 80: exT.addActionListener(this);
! 81: c.anchor = GridBagConstraints.CENTER; // default
! 82:
! 83: //Add the slider. It's horizontal, and it has the maximum
! 84: //value specified by the instance variable max. Its initial
! 85: //and minimum values are the default (0). A click increments
! 86: //the value by block units.
! 87: sB = new Scrollbar(Scrollbar.HORIZONTAL);
! 88: sB.setMaximum(max + 10);
! 89: sB.setBlockIncrement(block);
! 90: // c.gridwidth = GridBagConstraints.REMAINDER; //It ends a row.
! 91: c.fill = GridBagConstraints.HORIZONTAL;
! 92: c.gridwidth = 2;
! 93: c.gridx = 3;
! 94: c.gridy = 1;
! 95: gridbag.setConstraints(sB, c);
! 96: add(sB);
! 97: sB.addAdjustmentListener(this);
! 98: exT.setText(String.valueOf(sf_percent));
! 99: sB.setValue((int)sf_percent);
! 100: }
! 101:
! 102: }
! 103: // Draws a box around this panel.
! 104: public void paint(Graphics g) {
! 105: Dimension d = getSize();
! 106: g.drawRect(0,0, d.width - 1, d.height - 1);
! 107: }
! 108: public Insets getInsets() {
! 109: return new Insets(5,5,5,8);
! 110: }
! 111: float getSubTotal() {
! 112: String s_str = sumL.getText().replace('%','\0').trim();
! 113: float s_real = Float.valueOf(s_str).floatValue();
! 114: return (s_real);
! 115: }
! 116: double getValue() {
! 117: double f;
! 118: try {
! 119: f = (double)Double.valueOf(exT.getText()).doubleValue();
! 120: } catch (java.lang.NumberFormatException e) {
! 121: f = 0.0;
! 122: }
! 123: return f;
! 124: }
! 125: // entered into exT
! 126: public void actionPerformed(ActionEvent e) {
! 127: int tf = (int) getValue();
! 128: if (tf > max)
! 129: tf = max;
! 130: if (tf < 0)
! 131: tf = 0;
! 132: sB.setValue(tf);
! 133: exT.setText(String.valueOf((int)tf));
! 134: recalcSumm(tf);
! 135: }
! 136: // slider sB changed
! 137: public void adjustmentValueChanged(AdjustmentEvent e) {
! 138: Scrollbar source = (Scrollbar)e.getSource();
! 139: int my_i = (int)source.getValue();
! 140: exT.setText(String.valueOf(e.getValue()));
! 141: recalcSumm(my_i);
! 142: }
! 143: void recalcSumm(int tf) {
! 144: float my_r = (float) tf / (float)100.0;
! 145: int tmp_i= (int)(10000*(ss+(rr*ff*my_r/cc))/(float)(ff*(1.0+rr/cc)));
! 146: float sum = (float)tmp_i / (float)100.0;
! 147: sumL.setText(sum + "%");
! 148: controller.recalcTermScore();
! 149: }
! 150: // Set the values in the slider and text field.
! 151: void setValue(double f) {
! 152: setSliderValue(f);
! 153: exT.setText(String.valueOf((float)f));
! 154: }
! 155: void setSliderValue(double f) {
! 156: int sliderValue = (int)f;
! 157:
! 158: if (sliderValue > max)
! 159: sliderValue = max;
! 160: if (sliderValue < 0)
! 161: sliderValue = 0;
! 162: sB.setValue(sliderValue);
! 163:
! 164: }
! 165: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>