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();
...........