Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Breakout Game: JButton problems

    I am a beginner programmer and I am trying to make a simple 2D game, Breakout. I have the logic for the game itself working but I am having trouble with getting the buttons to work with the game. I added a start and pause button to the top of the screen but whenever I pressed one of these buttons, the paddle at the bottom of the screen cannot move.

    When I take out the buttons the game runs perfectly.

    Any help would be greatly appreciated! Thank you in advance.

    Below is the code for my game. I only included the Breakout class because this was were I ran into trouoble.


    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.TimerTask;
    
    public class Breakout extends JPanel implements KeyListener, ActionListener
    {
    	boolean rightPressed;
    	boolean leftPressed;
    	boolean upPressed;
    	boolean downPressed;
    	
    	boolean inGame;
    	
    	int width = 1450;
    	int height = 900;
    	Timer tm = new Timer(5, this);
    	int numRows  =5;
    	int numCols = 8;
    	int numBricks = 40;
    	
    	int mouseX;
    	int mouseY;
    	
    	Paddle paddle = new Paddle(width/2, height-40);
    	Ball ball = new Ball();
    	//BrickArray brick = new BrickArray(numRows, numCols, width/numCols, (height-100)/numRows);
    	Brick[][] bricks = new Brick[8][5];
    	
    	JButton start = new JButton("Start");
    	JButton pause = new JButton("Pause");
    	JButton reset = new JButton("Reset");
    	JButton exit = new JButton("Exit");
    	JButton help = new JButton("Help");
    	
    	public Breakout()
    	{
    		setDoubleBuffered(true);
    		//tm.start();
    		addKeyListener(this);
    		setFocusable(true);
    		
    		add(start);	
    		start.addActionListener(new StartAction());		
    		add(pause);
    		pause.addActionListener(new PauseAction());
    		add(reset);
    		add(exit);
    		add(help);
    		
    		ball.setMaxX(width);
    		ball.setMaxY(height);
    		ball.setWidth(width);
    		ball.setHeight(height);		
    		ball.setX(width/2);	//width/2
    		ball.setY(height/2);	//height
    		
    		paddle.setMaxX(width-paddle.getWidth());
    		
    		int k=0;
    		//row 1
    		for(int a=0; a<bricks.length; a++)
    		{
    			bricks[a][0]=new Brick(120,40);
    			bricks[a][0].setX(a*180);
    			bricks[a][0].setY(50);
    			//System.out.println(k + ": " + a*80);
    		}
    		
    		//row 2
    		for(int b=0; b<bricks.length; b++)
    		{
    			bricks[b][1]=new Brick(120,40);
    			bricks[b][1].setX(b*180);
    			bricks[b][1].setY(100);
    		}
    		
    		//row 3
    		for(int c=0; c<bricks.length; c++)
    		{
    			bricks[c][2]=new Brick(120,40);
    			bricks[c][2].setX(c*180);
    			bricks[c][2].setY(150);
    		}
    		
    		//row 4
    		for(int d=0; d<bricks.length; d++)
    		{
    			bricks[d][3]=new Brick(120,40);
    			bricks[d][3].setX(d*180);
    			bricks[d][3].setY(200);
    		}
    		
    		for(int e=0; e<bricks.length; e++)
    		{
    			bricks[e][4]=new Brick(120,40);
    			bricks[e][4].setX(e*180);
    			bricks[e][4].setY(250);
    		}
    		
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		tm.setRepeats(true);
    		
    		if(rightPressed)
    		{
    			paddle.setX(paddle.getX()+5);
    			//System.out.println("Pressed");	
    		}
    		if(leftPressed)
    			paddle.setX(paddle.getX()-5);
    		/*if(upPressed)
    			y--;
    		if(downPressed)
    			y++;*/	
    		
    		ball.checkCollision(paddle);		
    		for(int i=0; i<bricks.length; i++)
    		{
    			for(int j=0; j<bricks[i].length; j++)
    			{
    				if(ball.checkCollision(bricks[i][j]))
    				{
    					bricks[i][j].destroy();
    					//System.out.println(bricks[i][j].isDestroyed());
    					numBricks--;
    					ball.move();
    					repaint();
    				}
    			}
    		}		
    					
    		
    		//ball.checkCollision(brick);
    		ball.move();		
    		repaint();
    	}
    	
    	public void keyPressed(KeyEvent e)
    	{
    		int c = e.getKeyCode();
    		if(c == KeyEvent.VK_LEFT)
    		{
    			leftPressed=true;
    		}
    		/*if(c == KeyEvent.VK_UP)
    		{
    			upPressed=true;
    		}*/
    		if(c == KeyEvent.VK_RIGHT)
    		{
    			rightPressed=true;
    			//System.out.println("Pressed");	
    		}
    		/*if(c == KeyEvent.VK_DOWN)
    		{
    			downPressed=true;
    		}*/
    		
    	}
    	
    	public void keyTyped(KeyEvent e)
    	{
    	}
    	
    	public void keyReleased(KeyEvent e)
    	{
    		int c = e.getKeyCode();
    		if(c == KeyEvent.VK_LEFT)
    		{
    			leftPressed=false;
    		}
    		if(c == KeyEvent.VK_UP)
    		{
    			upPressed=false;
    		}
    		if(c == KeyEvent.VK_RIGHT)
    		{
    			rightPressed=false;
    		}
    		if(c == KeyEvent.VK_DOWN)
    		{
    			downPressed=false;
    		}
    	}	
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		
    		for(int a=0; a<bricks.length; a++)
    		{
    			g.setColor(Color.RED);
    			bricks[a][0].paintComponent(g);
    		}
    		for(int b=0; b<bricks.length; b++)
    		{
    			g.setColor(Color.ORANGE);
    			bricks[b][1].paintComponent(g);
    		}
    		for(int c=0; c<bricks.length; c++)
    		{
    			g.setColor(Color.YELLOW);
    			bricks[c][2].paintComponent(g);
    		}
    		for(int d=0; d<bricks.length; d++)
    		{
    			g.setColor(Color.GREEN);
    			bricks[d][3].paintComponent(g);
    		}	
    		for(int e=0; e<bricks.length; e++)
    		{
    			g.setColor(Color.BLUE);
    			bricks[e][4].paintComponent(g);
    		}		
    		ball.paintComponent(g);
    		paddle.paintComponent(g);
    		g.setColor(Color.BLACK);
    		g.drawString("Bricks left: " + numBricks, 10, 10);
    	
    	}
    	
    	public static void main(String[] args)
    	{
    		Breakout obj = new Breakout();
    		JFrame jf = new JFrame();
    		jf.setTitle("Breakout");
    		jf.setSize(1450,900);
    		jf.setVisible(true);
    		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jf.add(obj);
    	}
    	
    	class StartAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			tm.start();
    			System.out.println("Start");
    		}
    	}
    	
    	class PauseAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			tm.stop();
    			System.out.println("Pause");
    		}
    	}
    
    	
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: Breakout Game: JButton problems

    That's because the focus is going from the component you're listening to for key presses, to the JButton you just clicked. You either have to make the JButton unfocusable, give the focus back, add the KeyListener to the JButton as well, or use key bindings instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Breakout Game: JButton problems

    I would suggest just using key bindings like Kevin said. Just make the game pause and un-pause when you press the escape button. That way you don't waste screen space, and you can keep the focus in your window.

Similar Threads

  1. [Help]Make a Breakout Game - CS106A
    By YLTO in forum New To Java
    Replies: 3
    Last Post: 10-25-2011, 04:45 PM
  2. Breakout game in the assignment 3 cs106
    By dage in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:07 PM
  3. Making a breakout Game
    By JavaIsSoEasy?? in forum Java Gaming
    Replies: 0
    Last Post: 03-05-2011, 10:52 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
  •