Hey all, long time no see ;p
Having not done any Java2D in many months now, I thought it was about time I got stuck right in.
I know that you can set the background colour for a content pane easily through methods such as frame.getContentPane().setBackground(Color.BLACK), but as soon as I wanted to make my own custom content pane, contentpane.setBackground never seemed to work. Blissfully unaware of the reasons for it, the only way I've seemed able to get a background colour illusion to work is using the demonstration code below, where I simply draw a rectangle to cover the content pane.
public class ContentPane extends JComponent { public ContentPane() { setBackground(Color.PINK); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); Graphics2D g2d = (Graphics2D) g; //Begin drawing } public static void main(String[] args) { JFrame gui = new JFrame("Window Title"); ContentPane cp = new ContentPane(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(800, 600); gui.setResizable(false); gui.setLocationRelativeTo(null); gui.setContentPane(cp); gui.setVisible(true); } }
Simply, what I'm asking is if there are any "formal" ways to do this; i.e. is there a way to get setBackground to work on a custom set CP? as without
g.setColor(getBackground());is just doesn't work.
g.fillRect(0, 0, getWidth(), getHeight());
- Late night question of the week done! :3