Member Avatar for Morley93

Hi all, I'm a long time reader of the forums and thought this would be a good place to ask a question. For the record, I am very new to Java!

Basically, I have a MainClass which inherits JFrame, the constructor for which sets window size, title etc. It also adds a new instance of my class Board. In my constructor for Board, I try to set the background color, but I still have the grey background when I interpret using Eclipse. I know for a fact that Board is being added to the JFrame due to other things that I've painted to it.

Here's my MainClass

import javax.swing.JFrame; //Allows the game to use a window

public class MainClass extends JFrame{
	//Main/program opening method
	public static void main(String args[]){
		new MainClass();
	}

	//Constructor
	public MainClass(){
		add(new Board());
		setDefaultCloseOperation(EXIT_ON_CLOSE); //Ensures that the program ends properly
		setSize(520, 545); //Sets the size of the window
		setLocationRelativeTo(null); //Makes the window start in the centre of the screen
		setTitle("Revision Game"); //Title-bar
		setResizable(false); //The window can't be resized
		setVisible(true); //Ensures the window is visible
	}	
}

And here's constructor of the JPanel Board that I'm trying to change the background of.

import java.awt.Color; //Allows the use of colour
import javax.swing.JPanel; //Allows the board to make a new panel in the window

public class Board extends JPanel{
	//Constructor
	public Board(){
		setBackground(Color.black);
		initGame();
	}
}

There's a lot of code that I've omitted, but I've tried to include everything that's relevant.

Any Ideas?
Thanks in advance!

Morley

Recommended Answers

All 10 Replies

Member Avatar for hfx642

Yes... You want to set the colour of your JPanels that you are using.
ie My_Pnl.setBackground (Color.BLACK);

Member Avatar for Morley93

Well seeing as Board extends JPanel and setBackground() is used in the constructor, shouldn't this work?

Have you filled your Board with non-transparent components?

Member Avatar for Morley93

The only things that I've put on the Board are some circles using a paint() method and fillOval() from the Graphics class - they appear just fine.

have you by any chance included a super(g) somewhere in either your constructor or paintComponent or repaint() method? if yes, remove it.

Member Avatar for Morley93

have you by any chance included a super(g) somewhere in either your constructor or paintComponent or repaint() method? if yes, remove it.

Thanks for the reply, but I haven't used that.

After experimenting quite a bit, I have discovered that if I comment out my paint() method, the black background works just fine. I'm taking this to mean that something in that method is overwriting the black background.

Here's the code for the paint() method in my Board class.

//Paints each snake to the screen
	public void paint(Graphics g){
		for(Snake s: snakes){ //Loops through objects in the snakes[] array
			g.setColor(s.getHeadColour());
			g.fillOval(s.getX(0), s.getY(0), Settings.CELL_SIZE, Settings.CELL_SIZE);
			for(int z = 1; z < s.getSegCount(); z++){
				g.setColor(s.getBodyColour());
				g.fillOval(s.getX(z), s.getY(z), Settings.CELL_SIZE, Settings.CELL_SIZE);
			}
		}	
	}

And yes, as I'm sure you realised, I'm making a Snake game!

i guess you pass new Graphics g to it or something. do you pass the same g that you set color to on to paint()?

Member Avatar for Morley93

Sorted it, just had to add:

super.paintComponent(g);

Thanks for the replies - much progress has been made!

Actually, you should override paintComponent(Graphics) instead of paint(Graphics) and called super(g). The paint() method has a few other responsibilities such as painting the border and children as well managing partial repaints.

Overriding paintComponent() is recommended for custom rendering.

Member Avatar for Morley93

Yeah sorry, forgot to mention that I changed paint() to paintComponent() as I heard it was more efficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.