Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JComponent.setbackground(Color.BLACK) is NOT working

843807Oct 29 2009 — edited Oct 30 2009
I am creating Space Invaders game and have GamePanel as my JComponent, named panel. The background is not changing for sum reason though???
import java.awt.*;
import javax.swing.*;

public class SpaceInvaders{
	
	//variables
	public static final int WIDTH = 800;
	public static final int HEIGHT = 800;
	GamePanel panel;
	
	public SpaceInvaders()
	{
		//create frame
		JFrame demoFrame = new JFrame("Space Invaders");
		demoFrame.setBounds(0,0,WIDTH,HEIGHT);
		demoFrame.setVisible(true);
		demoFrame.setResizable(false);
		demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		demoFrame.setBackground(Color.BLACK);
		
		//create panel
		GamePanel panel = new GamePanel(this);
		panel.setBackground(Color.BLACK);
		demoFrame.add(panel);
		
		//add buttons
		JButton startButton = new JButton("Start");
		startButton.setEnabled(true);
		JButton stopButton = new JButton("Stop");
		stopButton.setEnabled(true);
.....
import java.awt.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.*;
import java.awt.event.*;

public class GamePanel extends JComponent implements KeyListener, ActionListener
{
	SpaceInvaders game;
	static Player player1;
	Bullets bullet;
	private BufferStrategy strategy;
	static int alienNum = 36;
	static int alienRow = 6;
	static int alienCol = 6;
	static Aliens[][] aliens;
	static Bullets[] bullets;
	private Timer timer;
	int bulletNum=0;
	
	public GamePanel(SpaceInvaders game)
	{
		this.game = game;
		player1 = new Player(this);
		player1.start();
	
		aliens = new Aliens[alienRow][alienCol];
		for(int i=0; i<alienRow; i++)
			for(int j=0; j<alienCol; j++){
			{
				aliens[i][j] = new Aliens(this);
				aliens[i][j].setX(i*50);
				aliens[i][j].setY(j*50);
			}
		}
		bullets = new Bullets[5];
		
			bullets[0]=new Bullets(this);
		
		
		
		
		timer=new Timer(500, this);
		//timer.setDelay(500);
		timer.start();
		this.setFocusable(true);
        this.addKeyListener(this);
	}
	
	public void paintComponent(Graphics g)
	{
		
		super.paintComponent(g);
		
		Image pic1 = player1.getImage();
...........

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 27 2009
Added on Oct 29 2009
4 comments
231 views