Why is it that changing the background color for the java.awt.Canvas, changes the background for the whole frame?
I set up a Frame object as follows:
public class Gui extends Frame {
public Gui() {
setSize(800, 600);
setLocation(0, 0);
Canvas cd=new Canvas();
cd.setSize(500, 300);
cd.setBackground(Color.BLUE);
add(cd);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
paintComponents(g);
}
public static void main(String[] args) {
Gui g=new Gui();
g.setVisible(true);
}
}
The above code sets the frame size to be 800x600, then adds a significantly smaller canvas to it - 500x300, and finally sets the background color to be Color.BLUE
, but instead of getting a 500x300 blue rectangle inside a bigger, 800x600 window-frame (with default grey color), the result is a 800x600 frame with blue background:
The docs says:
public void setBackground(Color c)
Sets the background color of this component.
The background color affects each component differently and the parts of the component that are affected by the background color may differ between operating systems.
Could that be the issue (I'm running this on Ubuntu)?
Or am I missing something else here?