Name: rmT116609 Date: 09/18/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
also:
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)
DESCRIPTION OF THE PROBLEM :
Centering multiple components with a SpringLayout requires a subclass of Spring which can divide the atrributes of a Spring in half. A "scale" operator
can perform this function and would fit well with the "sum", " minus" and "max" operators the class already provides.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. javac TestCenter2.java
2. java TestCenter2
EXPECTED VERSUS ACTUAL BEHAVIOR :
The provided source shows one way to center components
with a SpringLayout. This is an RFE, so no error is
produced.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
public class TestCenter2 {
public static class ScaleSpring extends Spring {
private Spring s;
private double factor;
private ScaleSpring(Spring s, double factor) {
this.s = s;
this.factor = factor;
}
public int getMinimumValue() {
return (int)((factor < 0 ? s.getMaximumValue() : s.getMinimumValue
()) * factor);
}
public int getPreferredValue() {
return (int)(s.getPreferredValue() * factor);
}
public int getMaximumValue() {
return (int)((factor < 0 ? s.getMinimumValue() : s.getMaximumValue
()) * factor);
}
public int getValue() {
return (int)(s.getValue() * factor);
}
public void setValue(int value) {
s.setValue((int)(value / factor));
}
}
public static Spring scale(Spring s, double factor) {
return new ScaleSpring(s, factor);
}
public static void main(String[] args) {
JFrame f = new JFrame();
Container p = f.getContentPane();
JComponent c1 = new JTextField("Spectacular");
JComponent c2 = new JTextField("Bar");
// JComponent c1 = new JButton("Foo");
// c1.setMinimumSize(new Dimension(0, 0));
// c1.setMaximumSize(new Dimension(Integer.MAX_VALUE,
Integer.MAX_VALUE));
// JComponent c2 = new JButton("Bar");
// c1.setMinimumSize(new Dimension(0, 0));
// c1.setMaximumSize(new Dimension(Integer.MAX_VALUE,
Integer.MAX_VALUE));
SpringLayout l = new SpringLayout();
p.setLayout(l);
p.add(c1);
p.add(c2);
SpringLayout.Constraints pc = l.getConstraints(p);
SpringLayout.Constraints bc1 = l.getConstraints(c1);
SpringLayout.Constraints bc2 = l.getConstraints(c2);
Spring middle = scale(pc.getWidth(), 0.5);
bc1.setX(Spring.sum(middle, Spring.minus(scale(bc1.getWidth(), 0.5))));
bc2.setX(Spring.sum(middle, Spring.minus(scale(bc2.getWidth(), 0.5))));
pc.setConstraint("East", Spring.constant(0, 300, 1000));
// pc.setConstraint("East", Spring.max(bc1.getWidth(), bc2.getWidth()));
bc1.setY(Spring.constant(0, 10, 100));
bc2.setY(Spring.sum(Spring.constant(10), bc1.getConstraint("South")));
pc.setConstraint("South", Spring.sum(Spring.constant(10),
bc2.getConstraint("South")));
f.pack();
f.setVisible(true);
}
}
---------- END SOURCE ----------
(Review ID: 164663)
======================================================================