(Apple problem # 3309656)
This applet pops up a Frame with the default "brushed metal" background. On the Frame is a component extending Canvas that places a black bar on the Frame. Once the user changes the bar, the brushed metal background is not used as the background for the bar until the user re-sizes the Frame.
To reproduce the problem:
Various invalidate/validate calls don't solve the problem and fiddling with the update method gives other problems. Changing "DistributionBar extends Canvas" to "DistributionBar extends Container" or "DistributionBar extends Component" does not fix the problem; however, these changes do result in a small white box appearing at the lower right of the Frame, as shown here.
If you have any insights, workarounds or comments about this test page please contact Mickey Segal. A listing of many Java resources is at this link.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class brush_bar extends Applet implements ActionListener, WindowListener {
Frame frame;
DistributionBar bar;
Button button;
double fractionOfMax;
public void init()
{
frame = new Frame("A frame");
frame.setLayout(new GridBagLayout());
frame.setBounds(100, 100, 600, 300);
bar = new DistributionBar(32);
bar.setToHeight(fractionOfMax = 0.1);
constrain(frame, bar, 0, 0, 1, 1, GridBagConstraints.WEST, 0, 0, 0, 0,
GridBagConstraints.VERTICAL, 0, 1);
button = new Button("Change bar");
button.addActionListener(this);
constrain(frame, button, 1, 0, 1, 1, GridBagConstraints.WEST, 0, 20, 0, 0,
GridBagConstraints.NONE, 0, 0);
frame.validate();
frame.show();
frame.addWindowListener(this);
}
static final void constrain(Container container, Component component, int grid_x, int grid_y,
int grid_width, int grid_height, int anchor, int topPad, int leftPad, int bottomPad,
int rightPad, int fill, double weightx, double weighty)
{
GridBagConstraints c = new GridBagConstraints();
c.gridx = grid_x;
c.gridy = grid_y;
c.gridwidth = grid_width;
c.gridheight = grid_height;
c.anchor = anchor;
c.insets.top = topPad;
c.insets.left = leftPad;
c.insets.bottom= bottomPad;
c.insets.right = rightPad;
c.fill = fill;
c.weightx = weightx;
c.weighty = weighty;
((GridBagLayout)container.getLayout()).setConstraints(component, c);
container.add(component);
}
public void windowOpened(WindowEvent we){}
public void windowClosed(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void windowClosing(WindowEvent we)
{
if (we.getSource() == frame)
{
frame.setVisible(false);
frame.dispose();
}
}
public void paint(Graphics g)
{
g.drawString("A frame should pop up", 10 , 20);
}
final double getProperHeight()
{
fractionOfMax += 0.1;
if (fractionOfMax >= 1.0) fractionOfMax = 0.1;
return(fractionOfMax);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == button)
{
bar.setToHeight(getProperHeight());
bar.invalidate();
invalidate();
bar.validate();
validate();
}
}
} // END OF Class brush_bar
final class DistributionBar extends Canvas {
int width = 0;
int height = 0;
double fractionOfMax = 0.0;
DistributionBar(int width)
{
this.width = width;
}
final void setToHeight(double fractionOfMax)
{
this.fractionOfMax = fractionOfMax;
repaint();
}
public void addNotify()
{
super.addNotify();
repaint();
}
public final void paint(Graphics g)
{
height = getSize().height;
int barHeight = (int)(height * fractionOfMax);
g.setColor(Color.black);
g.fillRect(0, height-barHeight, width, barHeight); // bar
}
public final Dimension getMinimumSize()
{
return(new Dimension(width, 10));
}
public final Dimension getPreferredSize()
{
return(new Dimension(width, 140));
}
} // END OF Class DistributionBar