1 Attachment(s)
Breakout game (wanted to add score board in the layout)
Hi,
I wanted to add a score board along with lives with text field to the right of the game panel using JPanel/JFrame
Here's the snapshot.
Attachment 1833
Here's the code snippet
Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Breakout extends JFrame{
public Breakout()
{
add(new Board());
setTitle("Breakout");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(Commons.WIDTH, Commons.HEIGTH);
setLocationRelativeTo(null);
setIgnoreRepaint(true);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Breakout();
}
}
And one more
Code:
public class Board extends JPanel implements Commons {
Image ii;
Timer timer;
String message = "Game Over";
Ball ball;
Paddle paddle;
Brick bricks[];
int score=0;
boolean ingame = true;
int timerId;
public Board() {
addKeyListener(new TAdapter());
setFocusable(true);
bricks = new Brick[30];
setDoubleBuffered(true);
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}
public void addNotify() {
super.addNotify();
gameInit();
}
public void gameInit() {
ball = new Ball();
paddle = new Paddle();
int k = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
bricks[k] = new Brick(j * 40 + 30, i * 10 + 50);
k++;
}
}
}
public void paint(Graphics g) {
super.paint(g);
if (ingame) {
g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
ball.getWidth(), ball.getHeight(), this);
g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
paddle.getWidth(), paddle.getHeight(), this);
for (int i = 0; i < 30; i++) {
if (!bricks[i].isDestroyed())
g.drawImage(bricks[i].getImage(), bricks[i].getX(),
bricks[i].getY(), bricks[i].getWidth(),
bricks[i].getHeight(), this);
}
}else {
/*Font font = new Font("Verdana", Font.BOLD, 18);
FontMetrics metr = this.getFontMetrics(font);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(message,
90,
Commons.WIDTH / 4);*/
}
Toolkit.getDefaultToolkit().sync();
// g.dispose();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
paddle.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
paddle.keyPressed(e);
}
}
class ScheduleTask extends TimerTask {
public void run() {
ball.move();
paddle.move();
checkCollision();
repaint();
}
}
public void stopGame() {
ingame = true;
timer.cancel();
}
public static class Wait {
public static void oneSec() {
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void checkCollision() {
if (ball.getRect().getMaxY() > Commons.BOTTOM) {
// stopGame();
Wait.oneSec();
ball = new Ball();
}
for (int i = 0, j = 0; i < 30; i++) {
if (bricks[i].isDestroyed()) {
j++;
}
if (j == 30) {
message = "Victory";
stopGame();
}
}
}
Re: Breakout game (wanted to add score board in the layout)
Add a new JFrame near it, or just use a JLabel to display the score
Re: Breakout game (wanted to add score board in the layout)
where do i place the JLabel
Re: Breakout game (wanted to add score board in the layout)
Quote:
Originally Posted by
tulip
where do i place the JLabel
Wherever you want to place it. Myself, I'd keep score either at the top or the bottom of the GUI, by adding a JPanel that holds the JLabel to the GUI's main container, probably its contentPane, in a BorderLayout.NORTH or BorderLayout.SOUTH position. This suggest though that the contentPane is using BorderLayout as its layout manager which it usually does by default.
You can read up more on the layout managers here: A Visual Guide to the Layout Managers