2

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:

enter image description here

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
so.very.tired
  • 2,958
  • 4
  • 41
  • 69
  • Don't expect to get any help on depricated stuff, they don't even teach them in college anymore. – Mordechai May 11 '15 at 17:02
  • Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 12 '15 at 05:17
  • I use AWT because I need to use JFreeChart (http://www.jfree.org/jfreechart/api/javadoc/) which supports only AWT components, as far as I know... – so.very.tired May 12 '15 at 06:56

1 Answers1

1

The default layout of a Frame is BorderLayout (though a lot of the example code using the AWT based Frame class was written in a time when the default layout was FlowLayout). Without layout constraints, a component added to a BorderLayout will end up in the CENTER constraint which stretches a component so it fits to the available height and width.

Instead we might use a GridBagLayout. When we add a single component to a GridBagLayout without any constraint, it will be centered and the size will be respected. E.G. (See further comments in code.)

Centered Canvas in Frame

import java.awt.*;
import java.awt.event.*;

public class Gui extends Frame {

    public Gui() {
        setSize(400, 200);
        setLocationByPlatform(true);
        setLayout(new GridBagLayout());

        Canvas cd=new Canvas();
        // TODO: Override rather than set & make it preferred rather than actual
        cd.setSize(300, 100); 
        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); // wrong method!
        super.paint(g); // right method, but does nothing different to original!
    }

    public static void main(String[] args) {
        // TODO: AWT/Swing based GUIs should be started on the EDT
        Gui g=new Gui();
        g.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What does "AWT/Swing based GUIs should be started on the EDT"? Do you mean that it must run on separate thread? – so.very.tired May 12 '15 at 12:22
  • 1
    See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) & particularly **[Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)** for more details. – Andrew Thompson May 12 '15 at 12:24