(Apple problem # 3309656)
The Brush Metal background does not get painted properly in 1.4.x. To see the problem do the following:
One could argue that the blue background should be present in all cases, or in no cases, but it is inconsistent for it to appear depending on whether the Frame is resized.
This applet differs from the original applet in that the background of the Frame in the current applet is set to a color (see the line of code marked red below). But setting a background color in the code is often done since applets are meant to run in other environments with no Brush Metal. One could argue that this is not a bug but merely an inconvenience in that existing applets will look bad, but one can code around it by detecting environments in which brush metal backgrounds are used.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class brush_bar3 extends Applet implements ActionListener, WindowListener
{
Frame frame;
DistributionBar bar;
Button button;
double fractionOfMax;
public void init()
{
frame = new Frame("A frame");
frame.setBackground(Color.blue);
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_bar3
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