asteroids-2d-game/src/GamePanels/GamePanel.java

148 lines
4.2 KiB
Java
Raw Normal View History

2015-12-21 17:54:44 +00:00
package GamePanels;
import Engine.*;
import javax.swing.*;
import java.awt.*;
2015-12-24 20:47:47 +00:00
//import java.awt.image.BufferedImage;
2015-12-21 17:54:44 +00:00
import java.net.URL;
/**
* Created by sagi on 12/18/15.
*/
public class GamePanel extends JPanel implements Runnable{
private final URL bgURL= getClass().getResource("/Images/bg2.gif");
private final URL gameOverURL= getClass().getResource("/Images/gameOver.png");
private final URL startURL= getClass().getResource("/Images/start.png");
2015-12-24 20:47:47 +00:00
// private int width, height;
2015-12-21 17:54:44 +00:00
private GameEngine engine;
private JLabel lbl_score, lbl_countDown, lbl_gameOver, lbl_bg, lbl_start;
private ImageIcon img_bg, img_go, img_start;
private Image bg_image;
2015-12-24 20:47:47 +00:00
/**
*
* Constructor.
* initializes all objects on panel and creates gameEngine
*/
2015-12-21 17:54:44 +00:00
public GamePanel(int width, int height){
this.setLayout(null);
2015-12-24 20:47:47 +00:00
// this.width = width;
// this.height = height;
2015-12-21 17:54:44 +00:00
this.engine = new GameEngine(width, height);
this.addKeyListener(engine);
this.setFocusable(true);
this.requestFocus();
2015-12-24 20:47:47 +00:00
//set score label at top left corner
2015-12-21 17:54:44 +00:00
this.lbl_score = new JLabel("");
lbl_score.setBounds(15,15,width,30);
2015-12-24 20:47:47 +00:00
//set countDown label at center
2015-12-21 17:54:44 +00:00
this.lbl_countDown = new JLabel("");
lbl_countDown.setBounds(width/2,0,width,height);
System.out.println("URL = " +bgURL);
2015-12-24 20:47:47 +00:00
//set background
2015-12-21 17:54:44 +00:00
img_bg = new ImageIcon(bgURL);
Image tmp_BG = img_bg.getImage();
bg_image = tmp_BG.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
img_bg = new ImageIcon(tmp_BG);
lbl_bg = new JLabel(img_bg);
lbl_bg.setBounds(0,0,width,height);
lbl_countDown.setFont(new Font("Ariel", Font.BOLD, 100));
lbl_countDown.setForeground(Color.WHITE);
this.add(lbl_countDown);
img_start = new ImageIcon(startURL);
lbl_start = new JLabel(img_start);
lbl_start.setVisible(true);
lbl_start.setBounds(0,0,width,height);
this.add(lbl_start);
img_go = new ImageIcon(gameOverURL);
lbl_gameOver = new JLabel(img_go);
lbl_gameOver.setVisible(false);
lbl_gameOver.setBounds(0,0,width,height);
this.add(lbl_gameOver);
lbl_score.setFont(new Font("Ariel", Font.BOLD, 24));
lbl_score.setForeground(Color.GREEN);
this.add(lbl_score);
repaint();
}
@Override
public void run() {
while(engine.gameOn){
engine.update();
2015-12-24 20:47:47 +00:00
//check if game is over and draw labels accordingly
2015-12-21 17:54:44 +00:00
if(engine.isGameOver()){
this.lbl_countDown.setVisible(false);
if(!engine.isFirstGame)
this.lbl_gameOver.setVisible(true);
}else{
this.lbl_gameOver.setVisible(false);
this.lbl_countDown.setVisible(true);
lbl_start.setVisible(false);
}
2015-12-24 20:47:47 +00:00
//render graphics
2015-12-21 17:54:44 +00:00
engine.render(this);
2015-12-24 20:47:47 +00:00
//sleep to slow down game
2015-12-21 17:54:44 +00:00
try {
2015-12-24 20:47:47 +00:00
Thread.sleep(5);
2015-12-21 17:54:44 +00:00
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
2015-12-24 20:47:47 +00:00
//sleep for other processes to take control
2015-12-21 17:54:44 +00:00
try {
2015-12-24 20:47:47 +00:00
Thread.sleep(20);
2015-12-21 17:54:44 +00:00
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void addNotify(){
super.addNotify();
(new Thread(this)).start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g.drawImage(bg_image, 0, 0, this); //draw the background
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
2015-12-24 20:47:47 +00:00
2015-12-21 17:54:44 +00:00
//Some labels - Score & countDown
lbl_score.setText("SCORE : " + engine.getScore());
if(engine.getCountDown() > 0){
lbl_countDown.setText(engine.getCountDown()+"");
}else if(engine.getCountDown() == 0){
lbl_countDown.setText("GO!");
}else{
lbl_countDown.setText("");
}
}
}