Results 1 to 4 of 4
  1. #1
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default 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.

    Breakout game (wanted to add score board in the layout)-untitled.jpg

    Here's the code snippet

    Java 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

    Java 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();
                }
            }
    }

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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

  3. #3
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Breakout game (wanted to add score board in the layout)

    where do i place the JLabel

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breakout game (wanted to add score board in the layout)

    Quote Originally Posted by tulip View Post
    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

Similar Threads

  1. Breakout game in the assignment 3 cs106
    By dage in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:07 PM
  2. Making a breakout Game
    By JavaIsSoEasy?? in forum Java Gaming
    Replies: 0
    Last Post: 03-05-2011, 10:52 AM
  3. How to do a score board?
    By vlan in forum Java Applets
    Replies: 11
    Last Post: 06-03-2010, 10:10 AM
  4. Need help with Breakout game
    By tfitz666 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 05:26 AM
  5. Breakout Game code help.
    By Ceasar in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •