8 Replies - 804 Views - Last Post: 15 December 2012 - 05:24 PM Rate Topic: -----

#1 BRI122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-December 12

! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 12:07 PM

I am using JCreator Pro to do this project. I have been working on this game for over 24 hours and I've been researching so much and going crazy trying to figure out how to get the ball to bounce off the paddle. I need some guidance because I am so stuck. I also cannot figure how how to even begin to get the ball to bounce off the bricks and make them dissapear.

Here is my Ball class:
import java.awt.*;
import javax.swing.*; // needed for drawing shapes
import java.awt.Graphics;
import java.awt.Color;
import java.awt.RadialGradientPaint;
import java.awt.GradientPaint;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Rectangle;

public class Ball
{
	// Declare the data members here.
	private static final int Diameter = 30;
	int x = 0;
	int y = 0;
	int xSpeed = 2;
	int ySpeed = 1;
	private Rectangle rectangleBall;
 	
	public Ball(int x, int y)
	{
		this.x = x;
		this.y = y;
	}

	public void show(Graphics g)
	{
    	Graphics2D g2 = (Graphics2D)g;
    	Ellipse2D e = new Ellipse2D.Float(x + 6, y + 30, Diameter, Diameter);
    	GradientPaint gp = new GradientPaint(50, 50, Color.getHSBColor(3.1f, .75f, .75f), 100, 100, Color.BLACK, true);
    	g2.setPaint(gp);
    	g2.fill(e);
	}
    
    public int getX()
    {	
    	return x;
    }
    
	public void setX(int newX)
	{	
		x = newX;
	}
		    		
    public int getY()
    {	
    	return y;
    }
    
	public void setY(int newY)
	{	
		y = newY;
	}	
		    		
    public int getXSpeed()
    {	
    	return xSpeed;
    }
    
	public void setXSpeed(int newXSpeed)
	{	
		xSpeed = newXSpeed;
	}
	
	public int getYSpeed()
    {	
    	return ySpeed;
    }
    
	public void setYSpeed(int newYSpeed)
	{	
		ySpeed = newYSpeed;
	}
	
	public Rectangle rectangleBall()
	{
		return new Rectangle(x, y, 35, 35);
	}	
}




Here is my Paddle class:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.RadialGradientPaint;
import java.awt.GradientPaint;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Rectangle;


public class Paddle {

    //declare data members here
	int Width = 150;
	int Height = 20;
    int x = 0;
    int y = 0;
    private Rectangle rectanglePaddle;
    
    public Paddle(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public void show(Graphics g)
    {
    	// Draw paddle
        Graphics2D g8 = (Graphics2D)g;
        GradientPaint gp7 = new GradientPaint(6, 30, Color.getHSBColor(.0f, .92f, .25f), 20, 20, Color.getHSBColor(.0f, .6f, .65f), true);
        g8.setPaint(gp7);
        g.fillRect(getX(), 470, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        g.drawRect(getX(), 470, getWidth(), getHeight());
    }
    
    public int getX()
    {
        return x;
    }

    public void setX(int newX)
    {
		x = newX;
    }
    
    public int getY()
    {
        return y;
    }
    
    public void setY(int newY)
    {
		y = newY;
    }
    
    public int getWidth()
    {
        return Width;
    }
    
    public int getHeight()
    {
        return Height;
    }
    
    public Rectangle getBoundsPaddle()
	{
		return new Rectangle(x, y, 150, 20);
	}	
}



Here is my main class:
import java.awt.*;
import javax.swing.*; // needed for drawing shapes
import java.awt.event.*;
import java.io.*; // Needed for disk IO
import java.util.*; // Needed for Scanner class
import java.awt.GridLayout;
import java.awt.RadialGradientPaint;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.*;

public class GameEnvironmentR7  extends DrawableAdapter
{   static GameEnvironmentR7 ge = new GameEnvironmentR7(); 
	static GameBoard gb = new GameBoard(ge, "BRICK BREAKER!");
	static Ball ball = new Ball(200, 100);
	static Blocks[] blocks;
	static Blocks[] blocks2;
	static Blocks[] blocks3;
	static Blocks[] blocks4;
	static char keyPressed = 'N';
	static int count = 60;
	static int score = 0;
	static Paddle paddle = new Paddle(190, 470);

	public static void main(String[] args) 
    {	
 		gb.setBackground(Color.BLACK);
 		
		//JOptionPane.showMessageDialog(null, "Welcome to the Christmas edition of BRICK BREAKER!");
 		//JOptionPane.showMessageDialog(null, "Game Objective: Get the moving ball to hit as many bricks as possible.");
 		//JOptionPane.showMessageDialog(null, "Seem easy? There's more! You will have only 1 minute to complete this feat.");
 		//JOptionPane.showMessageDialog(null, "The bricks will disappear when the ball hits them.");
 		//JOptionPane.showMessageDialog(null, "HOLD DOWN the Left or Right keys to control the paddle left or right.");
 		//JOptionPane.showMessageDialog(null, "There are 32 bricks total, and you will get 1 point for each brick you hit.");
 		//JOptionPane.showMessageDialog(null, "If the ball drops past the paddle, the game is over!");
 		//JOptionPane.showMessageDialog(null, "Remember, you have 1 minute after you press start, then time is up!");
 		//JOptionPane.showMessageDialog(null, "Good luck and have fun! Click OK to play!");
 		
 		// Declare row 1
 		blocks = new Blocks[8];
		for(int i = 0; i < 8; i++)
		{
			blocks[i] = new Blocks(30 + i * 56, 20);
		}
		
		// Declare row 2
		blocks2 = new Blocks[8];
		for(int i = 0; i < 8; i++)
		{
			blocks2[i] = new Blocks(30 + i * 56, 50);
		}
		
		// Declare row 3
		blocks3 = new Blocks[8];
		for(int i = 0; i < 8; i++)
		{
			blocks3[i] = new Blocks(30 + i * 56, 80);
		}
 		
 		// Declare row 4
 		blocks4 = new Blocks[8];
		for(int i = 0; i < 8; i++)
		{
			blocks4[i] = new Blocks(30 + i * 56, 110);
		}
 		
    	gb.setTimerInterval(3, 20);
    	gb.stopTimer(60);
    	showGameBoard(gb);
   	}
   	
   	public void draw(Graphics g)
   	{
   		// DRAWS FIRST ROW 
		for(int i = 0; i < 8; i++)
		{
			Graphics2D g3 = (Graphics2D)g;
    		GradientPaint gp2 = new GradientPaint(6, 30, Color.getHSBColor(1.3f, .66f, .25f), 20, 20, Color.getHSBColor(1.3f, .66f, .68f), true);
    		g3.setPaint(gp2);
    		if(i % 2 == 0)
    		{
    			Graphics2D g4 = (Graphics2D)g;
    			GradientPaint gp3 = new GradientPaint(6, 30, Color.getHSBColor(.0f, .92f, .25f), 20, 20, Color.getHSBColor(.0f, .6f, .65f), true);
    			g4.setPaint(gp3);
    		}
			blocks[i].show(g);
		}
		
		// DRAWS SECOND ROW 
		for(int i = 0; i < 8; i++)
		{
			Graphics2D g4 = (Graphics2D)g;
    		GradientPaint gp3 = new GradientPaint(6, 30, Color.getHSBColor(.0f, .92f, .25f), 20, 20, Color.getHSBColor(.0f, .6f, .65f), true);
    		g4.setPaint(gp3);
    		if(i % 2 == 0)
    		{
    			Graphics2D g5 = (Graphics2D)g;
    			GradientPaint gp4 = new GradientPaint(6, 30, Color.getHSBColor(1.3f, .66f, .25f), 20, 20, Color.getHSBColor(1.3f, .66f, .68f), true);
    			g5.setPaint(gp4);
    		}
			blocks2[i].show(g);
		}
		
		// DRAWS THIRD ROW 
		for(int i = 0; i < 8; i++)
		{
			Graphics2D g5 = (Graphics2D)g;
    		GradientPaint gp4 = new GradientPaint(6, 30, Color.getHSBColor(1.3f, .66f, .25f), 20, 20, Color.getHSBColor(1.3f, .66f, .68f), true);
    		g5.setPaint(gp4);
    		if(i % 2 == 0)
    		{
    			Graphics2D g6 = (Graphics2D)g;
    			GradientPaint gp5 = new GradientPaint(6, 30, Color.getHSBColor(.0f, .92f, .25f), 20, 20, Color.getHSBColor(.0f, .6f, .65f), true);
    			g6.setPaint(gp5);
    		}
			blocks3[i].show(g);
		}
		
		// DRAWS FOURTH ROW 
		for(int i = 0; i < 8; i++)
		{
			Graphics2D g6 = (Graphics2D)g;
    		GradientPaint gp5 = new GradientPaint(6, 30, Color.getHSBColor(.0f, .92f, .25f), 20, 20, Color.getHSBColor(.0f, .6f, .65f), true);
    		g6.setPaint(gp5);
    		if(i % 2 == 0)
    		{
    			Graphics2D g7 = (Graphics2D)g;
    			GradientPaint gp6 = new GradientPaint(6, 30, Color.getHSBColor(1.3f, .66f, .25f), 20, 20, Color.getHSBColor(1.3f, .66f, .68f), true);
    			g7.setPaint(gp6);
    		}
			blocks4[i].show(g);
		}
		
		// Designs on the bottom of the board
		g.setColor(Color.getHSBColor(1.3f, .66f, .68f));
		g.fillRect(0, 400, 500, 75);
		// Designs on the bottom of the board
		g.setColor(Color.getHSBColor(.0f, .6f, .65f));
		g.fillRect(0, 460, 500, 50);
		// Designs on the bottom of the board
		g.setColor(Color.getHSBColor(1.3f, .66f, .25f));
		g.fillRect(0, 400, 500, 5);
		// Designs on the bottom of the board
		g.setColor(Color.getHSBColor(.0f, .92f, .25f));
		g.fillRect(0, 460, 500, 5);
        
        // Count down
        g.setColor(Color.BLACK);
        g.fillRoundRect(18, 420, 210, 31, 10, 10);
    	g.setColor(Color.WHITE);
        g.fillRoundRect(20, 422, 206, 27, 10, 10);
        g.setColor(Color.BLACK);
        g.setFont(new Font("times new roman", Font.BOLD, 16));
    	g.drawString("Time Remaining: " + count + " seconds!", 24, 440);
    	
    	// Score
		g.setColor(Color.BLACK);
        g.fillRoundRect(418, 420, 74, 31, 10, 10);
    	g.setColor(Color.WHITE);
        g.fillRoundRect(420, 422, 70, 27, 10, 10);
    	g.setColor(Color.BLACK);
        g.setFont(new Font("times new roman", Font.BOLD, 16));
    	g.drawString("Score: " + score, 425, 440);
    	
    	// Draw ball
    	ball.show(g);
    	
    	// Draw paddle
    	paddle.show(g);
   	}

	public void timer1()
	{
		count = count - 1;
		score = score + 1;
		
		/*if(score == 32)
		{
			gb.stopTimer(1);
			JOptionPane.showMessageDialog(null, "CONGRATULATIONS! YOU WON!");
		}
		if(count == 60)
		{
			gb.stopTimer(1);
			JOptionPane.showMessageDialog(null, "Sorry! Time is up! Try again!");
		}*/
	}
	
	public void timer2()
	{
		int x;
		x = ball.getX();
		x = x + ball.getXSpeed();
		ball.setX(x);
		int y;
		y = ball.getY();
		y = y + ball.getYSpeed();
		ball.setY(y);
		int speed;

		if(ball.getX() >= 465 || ball.getX() <= 0)
		{
			speed = ball.getXSpeed();
			speed = -speed;
			ball.setXSpeed(speed);
		}
		if(ball.getY() >= 470 || ball.getY() <= 0)
		{
			speed = ball.getYSpeed();
			speed = 0;
			ball.setYSpeed(speed);
			JOptionPane.showMessageDialog(null, "You missed! Game over! Try again.");
		}	
	}
	
	
	public void keyStruck(char key)
	{
		System.out.println(key);
		keyPressed = key;
		switch(keyPressed)
		{
			case 'L':
				paddle.setX(paddle.getX() - 15);
				break;
				
			case 'R':
				paddle.setX(paddle.getX() + 15);
				break;
		}
	}
}




Any help/guidance would be appreciated. Thank you.

I forgot to include my Blocks class, sorry.

import java.awt.RadialGradientPaint;
import java.awt.GradientPaint;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.*;

public class Blocks
{
	// Declare the data members here.
	int x;
	int y;
	boolean visible = true;
	static int nBlocks = 0;  // shared amoung all Snowmen objects

	// Write the member methods here.
	public Blocks(int x, int y)
	{	
		this.x = x;
		this.y = y;
		nBlocks = nBlocks - 1;
	}
	
	public void show(Graphics g)
	{
    	g.fillRect(x + 6, y + 30, 40, 20);  // brim
	}
	
	public static int getnBlocks()
    {	
    	return nBlocks;
    }
    
    public int getX()
    {	
    	return x;
    }
    
	public void setX(int newX)
	{	
		x = newX;
	}	
		    		
    public int getY()
    {	
    	return y;
    }
    
	public void setY(int newY)
	{	
		y = newY;
	}  
		
	public boolean getVisible()
    {	
    	return visible;
    }

	public void setVisible(boolean newVisible)
	{	
		visible = newVisible;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: ! URGENT ! Brick Breaker Collisions

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:08 PM

And what are DrawableAdapter and GameBoard ?
Was This Post Helpful? 0
  • +
  • -

#3 BRI122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-December 12

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:13 PM

DRAWABLE ADAPTER CLASS:
import java.awt.*;

class DrawableAdapter implements Drawable
{	public static void showGameBoard(GameBoard gb) // Displays the Game Board gb
	{  
		gb.setVisible(true); 
		try
		{	Thread.sleep(10);
    		gb.repaint(true);
		}
		catch (InterruptedException e)
		{
		}	
	}	

	public void draw(Graphics g)
	{
	}
	public void timer1()
	{
	}
	public void timer2()
	{
	}
	public void timer3()
	{
	}
	public void leftButton()
	{
	}
	public void rightButton()
    {
    }
	public void upButton()
    {
    }
	public void downButton()
    {
    }
	public void keyStruck(char key)
	{
	}
	public void mouseClicked(int x, int y, int buttonclicked) // left button is 1, right button 3
	{
	}
	public void	mouseDragged(int x, int y)
	{
	}	    	

}




GAMEBOARD CLASS:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class GameBoard extends JFrame implements MouseMotionListener,  MouseListener, Drawable
{   boolean timer1On = true; int timer1Interval = 100;   // these lines activate
    boolean timer2On = true; int timer2Interval = 15;  // the timers and change
    boolean timer3On = true; int timer3Interval = 250; // intervals (milliseconds)
    Color backgroundColor = Color.red;  // the game background
    static String windowTitle;
 	static boolean gameStarted = false;

	public void repaint(boolean paint)
	{	
		if(paint == true)
		{	repaint();
		}
	}	

    public void draw(Graphics g) // draws the game objects
    {   // invoke the draw method for each of your game objects, e.g. snowMan1.draw(g);
		// Background
		gamePanel.draw(g); 
       	// ********** Part of the framework ***********
    }	

    class Timer1Handler implements ActionListener // Annimation every "tick" of TIMER 1 (services TIMER 1)
    {    public void actionPerformed(ActionEvent e)
        {  // add your annimation code here before the invocation of the repaint() method
			gamePanel.timer1();
				
           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not delete this statement
        }
    }

    class LeftHandler implements ActionListener // Executes every click of the LEFT button
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint() method
			gamePanel.leftButton();
			
           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not remove this line
        }
    }

    class RightHandler implements ActionListener // Executes every click of the RIGHT button
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint() method
			gamePanel.rightButton();
            
           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not remove this line
        }
    }

    class UpHandler implements ActionListener // Executes every click of the UP button
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint() method
 			gamePanel.upButton();
           
           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not remove this line
        }
    }

    class DownHandler implements ActionListener // Executes every click of the DOWN button
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint method
			gamePanel.downButton();
            
           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not remove this line
        }
    }

 	class KeyHandler extends KeyAdapter 
 	{    public void keyPressed(KeyEvent e)  //Executes whenever any keyboard key is pressed or held down
       	{   // add your code here before the invocation of the repaint method
			char c  = (KeyEvent.getKeyText(e.getKeyCode())).charAt(0); // USE THIS FOR Arrow keys: < > ^ \/
			gamePanel.keyStruck(c);

           //******** Part of the framework ************* 
           appletPaint = false; // do not delete this statement
           repaint(); // do not delete this statement
       }
 	}
 
    public void mouseMoved(MouseEvent me) // executes whenever the mouse is moved
    {
        //******** Part of the framework *************
        appletPaint = false; // do not delete this statement
        repaint(); // do not delete this statement
    }

    public void mouseDragged(MouseEvent me) // executes whenever the mouse is moved
    {	int x = me.getX();
    	int y = me.getY();
		gamePanel.mouseDragged(x, y);    	

        //******** Part of the framework *************
        appletPaint = false; // do not delete this statement
        repaint(); // do not delete this statement
    }

    public void mouseClicked(MouseEvent me) // executes whenever the mouse is clicked
    {	int x = me.getX();
    	int y = me.getY();
    	int buttonPressed = me.getButton();
    	gamePanel.mouseClicked(x, y, buttonPressed); // left button is 1, right button 3

        //******** Part of the framework *************
        appletPaint = false; // do not delete this statement
        repaint(); // do not delete this statement
    }

	public void mousePressed(MouseEvent me) // executes whenever the mouse button is pressed
	{
        //******** Part of the framework *************
        appletPaint = false; // do not delete this statement
        repaint(); // do not delete this statement
	}	

    class Timer2Handler implements ActionListener // Annimation every "tick" of TIMER 2 (services TIMER 2)
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint method
			gamePanel.timer2();

           //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint();// do not delete this statement
        }
    }

    class Timer3Handler implements ActionListener // Annimation every "tick" of TIMER 3 (services TIMER 3)
    {    public void actionPerformed(ActionEvent e)
        {   // add your code here before the invocation of the repaint method
            gamePanel.timer3();

            //******** Part of the framework *************
             appletPaint = false; // do not delete this statement
             repaint(); // do not delete this statement
        }
    }

	public void setTimerInterval(int timerNumber, int interval)
	{	if(timerNumber == 1)
		{	timer1Interval = interval;
			t1.stop();
			t1 = new Timer(timer1Interval, timer1Handler);
			if(gameStarted == true && timer1On == true)			
				t1.start();	
		}
		else if(timerNumber == 2)
		{	timer2Interval = interval;
			t2.stop();
			t2 = new Timer(timer2Interval, timer2Handler);
			if(gameStarted == true && timer2On == true)			
				t2.start();	
		}
		else if(timerNumber == 3)
		{	timer3Interval = interval;
			t3.stop();
			t3 = new Timer(timer3Interval, timer3Handler);
			if(gameStarted == true && timer3On == true)			
				t3.start();	
		}		
	}
		
	public void startTimer(int timerNumber)
	{	if(timerNumber == 1)
		{	//timer1Interval = interval;
			//t1.stop();
			//t1 = new Timer(timer1Interval, timer1Handler);
			timer1On = true;			
			t1.start();	
		}
		else if(timerNumber == 2)
		{	//timer2Interval = interval;
			//t2.stop();
			//t2 = new Timer(timer2Interval, timer2Handler);
			timer2On = true;			
			t2.start();	
		}
		else if(timerNumber == 3)
		{	//timer3Interval = interval;
			//t3.stop();
			//t3 = new Timer(timer3Interval, timer3Handler);
			timer3On = true;			
			t3.start();	
		}		
	}	

	public void stopTimer(int timerNumber)
	{	if(timerNumber == 1)
		{	t1.stop();
			timer1On = false;			
		}
		else if(timerNumber == 2)
		{	t2.stop();	
			timer2On = false;			
		}
		else if(timerNumber == 3)
		{	t3.stop();	
			timer3On = false;			
		}
	}	

	
//*******************************************************************************
//*********************** User Interface Initialization *************************
	Drawable gamePanel;
	int boardWidth, boardHeight;
    public GameBoard(Object ge, String windowTitle, int boardWidthLocal, int boardHeightLocal) //public GameBoard(String title)
    {    super (windowTitle);//super(title);
         gamePanel = (Drawable) ge;
		 boardWidth = boardWidthLocal;
		 boardHeight = boardHeightLocal;
		 this.setBackground(backgroundColor);
         this.setLayout(null);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(boardWidth + 122, boardHeight + 5); //         this.setSize(610, 500);
         this.setLocation(100, 100);
         int upButtonY = (int)(boardHeight/2.0);
         upButton.setBounds(new Rectangle(boardWidth + 39, upButtonY, 44, 22));
         upButton.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 8));
         upButton.setText("/\\");
         upButton.addActionListener(upHandler);
         startButton.setBounds(new Rectangle(boardWidth + 28, (int)(upButtonY - 46), 70, 22));
         startButton.setText("Start");
         startButton.addActionListener(startHandler);
         stopButton.setBounds(new Rectangle(boardWidth + 28, (int)(upButtonY - 24), 70, 22));
         stopButton.setText("Stop");
         stopButton.addActionListener(stopHandler);
         leftButton.setBounds(new Rectangle(boardWidth + 8, (int)(upButtonY + 22), 51, 22));
         leftButton.setHorizontalAlignment(SwingConstants.LEADING);
         leftButton.setText("<-");
         leftButton.addActionListener(leftHandler);
         rightButton.setBounds(new Rectangle(boardWidth + 59, (int)(upButtonY + 22), 51, 22));
         rightButton.setHorizontalAlignment(SwingConstants.CENTER);
         rightButton.setText("->");
         rightButton.addActionListener(rightHandler);
         downButton.setBounds(new Rectangle(boardWidth + 37, (int)(upButtonY + 44), 51, 22));
         downButton.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 8));
         downButton.setText("\\/");
         downButton.addActionListener(downHandler);
         
         this.add(rightButton);
         this.add(leftButton);
         this.add(upButton);
         this.add(downButton);
         this.add(startButton);
         this.add(stopButton);
         addMouseMotionListener(this);
         addMouseListener(this);
         addKeyListener(keyBoard);
  		 rightButton.setFocusable(false);
  		 leftButton.setFocusable(false);
  		 upButton.setFocusable(false);    	
  		 downButton.setFocusable(false);
  		 startButton.setFocusable(false);
  		 stopButton.setFocusable(false);
         //this.setVisible(true);

  		 //repaint();

    }	
 
     public GameBoard(Object ge, String windowTitle) //public GameBoard(String title)
    {    super (windowTitle);//super(title);
         gamePanel = (Drawable) ge;
		 boardWidth = 500;
		 boardHeight = 500;
		 this.setBackground(backgroundColor);
         this.setLayout(null);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(boardWidth + 122, boardHeight + 5); //         this.setSize(610, 500);
         this.setLocation(100, 100);
         int upButtonY = (int)(boardHeight/2.0);
         upButton.setBounds(new Rectangle(boardWidth + 39, upButtonY, 44, 22));
         upButton.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 8));
         upButton.setText("/\\");
         upButton.addActionListener(upHandler);
         startButton.setBounds(new Rectangle(boardWidth + 28, (int)(upButtonY - 46), 70, 22));
         startButton.setText("Start");
         startButton.addActionListener(startHandler);
         stopButton.setBounds(new Rectangle(boardWidth + 28, (int)(upButtonY - 24), 70, 22));
         stopButton.setText("Stop");
         stopButton.addActionListener(stopHandler);
         leftButton.setBounds(new Rectangle(boardWidth + 8, (int)(upButtonY + 22), 51, 22));
         leftButton.setHorizontalAlignment(SwingConstants.LEADING);
         leftButton.setText("<-");
         leftButton.addActionListener(leftHandler);
         rightButton.setBounds(new Rectangle(boardWidth + 59, (int)(upButtonY + 22), 51, 22));
         rightButton.setHorizontalAlignment(SwingConstants.CENTER);
         rightButton.setText("->");
         rightButton.addActionListener(rightHandler);
         downButton.setBounds(new Rectangle(boardWidth + 37, (int)(upButtonY + 44), 51, 22));
         downButton.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 8));
         downButton.setText("\\/");
         downButton.addActionListener(downHandler);
         
         this.add(rightButton);
         this.add(leftButton);
         this.add(upButton);
         this.add(downButton);
         this.add(startButton);
         this.add(stopButton);
         addMouseMotionListener(this);
         addMouseListener(this);
         addKeyListener(keyBoard);
  		 rightButton.setFocusable(false);
  		 leftButton.setFocusable(false);
  		 upButton.setFocusable(false);    	
  		 downButton.setFocusable(false);
  		 startButton.setFocusable(false);
  		 stopButton.setFocusable(false);
         //this.setVisible(true);

  		 //repaint();

    }	
   
   
    class StartHandler implements ActionListener // services the START button
    {    public void actionPerformed(ActionEvent e)
        {   gameStarted = true;
        	if(timer1On == true) // start timer 1
                t1.start();
             if(timer2On == true) // start timer 2
                t2.start(); 
             if(timer3On == true) // start timer 3
                t3.start(); 
			appletPaint = true;
            repaint(); // do not delete this statement
            appletPaint = false; // do not delete this statement
        }
    }

    class StopHandler implements ActionListener // services the STOP button
    {    public void actionPerformed(ActionEvent e)
        {   t1.stop();
            t2.stop();
            t3.stop();
        }
    }

















/********************************************************************************
****************************** No Man's Land ************************************
******************* Even Prof. McAllister Would Not Enter This Area *************
*********************************************************************************/
    boolean appletPaint = false;
    StartHandler startHandler = new StartHandler();
    StopHandler stopHandler = new StopHandler();
    LeftHandler leftHandler = new LeftHandler();
    RightHandler rightHandler = new RightHandler();
    UpHandler upHandler = new UpHandler();
    DownHandler downHandler = new DownHandler();
    Timer1Handler timer1Handler = new Timer1Handler();
    Timer2Handler timer2Handler = new Timer2Handler();
    Timer3Handler timer3Handler = new Timer3Handler();
    KeyHandler keyBoard = new KeyHandler();
    Timer t1 = new Timer(timer1Interval, timer1Handler);
    Timer t2 = new Timer(timer2Interval, timer2Handler);
    Timer t3 = new Timer(timer3Interval, timer3Handler);
    boolean isStandalone = false;
    BorderLayout borderLayout1 = new BorderLayout();
    JButton startButton = new JButton();
    JButton stopButton = new JButton();
    JButton leftButton = new JButton();
    JButton upButton = new JButton();
    JButton rightButton = new JButton();
    JButton downButton = new JButton();
    public void init() {
     }
   Image mImage, initImage;
   public void update(Graphics g) { paint(g); }

   public void paint(Graphics g)
   { // Clear the offscrean image
       Dimension d = this.getSize();
       if(appletPaint == true)
       	  super.paint(g);
       checkOffScreenImage();
       Graphics offG = mImage.getGraphics();
       offG.setColor(getBackground());
       offG.fillRect(4, 30, boardWidth, boardHeight);
    // Draw into the off screen image
       draw(mImage.getGraphics());
    // put the offscreen image on the screen
       g.drawImage(mImage, 0, 0, null);
       appletPaint = true;
   }

   private void checkOffScreenImage()
   {   Dimension d = getSize();
       if(mImage == null || mImage.getWidth(null) != boardWidth ||
                            mImage.getHeight(null) != boardHeight + 30)
       { mImage = createImage(boardWidth, boardHeight + 30);
       }    
   
   }
	public void mouseEntered(MouseEvent me)
	{ 
	}	
	public void mouseExited(MouseEvent me)
	{ 
	}	
	public void mouseReleased(MouseEvent me)
	{ 
	}	
	public void timer1()
	{
	}
	public void timer2()
	{
	}
 	public void timer3()
	{
	}
   public void leftButton()
    {
    }
	public void rightButton()
    {
    }
	public void upButton()
    {
    }
	public void downButton()
     {
    }
   public void keyStruck(char key)
    {
    }
    public void mouseClicked(int x, int y, int buttonPressed) // left button is 1, right button 3
    {
    }
	public void	mouseDragged(int x, int y)
	{
	}	    	
    	
}



View Postpbl, on 15 December 2012 - 03:08 PM, said:

And what are DrawableAdapter and GameBoard ?



I posted the codes to the DrawableAdapter and GameBoard!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:23 PM

And was is the interface Drawable ?
And why is yout GameEnvironmentR7 object a static instance of it's own class.
Why such a long main() method ? You can have put most of the stuff in the constructor of GameEnvironmentR7 and get ride of all these static fields ?
Was This Post Helpful? 0
  • +
  • -

#5 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:27 PM

For the paddle collisions you can just test for when the ball is at paddle height (plus or minus some small value), that is, since your handle is in the upper right hand corner, when the handle is the diameter above the paddle's handle. Then just test to see if the ball is over the paddle or empty space. so:

if(Math.abs(ball.y + ball.Diameter - paddle.y)) < epsilon) {
   // you know the ball is at the right height
   int dist = Math.abs(ball.x+ball.Diameter/2 - paddle.x+paddle.getWidth()/2);
   if(dist < paddle.getWidth()/2) {
       // you compare the distance from the middle of the ball to the middle of the paddle to half the length of the paddle
       // you know the ball is over the paddle, so make it bounce
       double num = someMultiplier*(dist*2/paddle.getWidth() + 1);
       ball.setYSpeed(-ball.getYSpeed()/num); // make it go up
       ball.setXSpeed(ball.getXSpeed()*num); // this will make the ball go at a different angle the farther from the center it hits.
   }
}



this is only for paddle collisions.

Think about the conditions that would define a collision with a block. Convert it so that it's in terms of the variables you have available to work with.

edit:

you can add an else to second if for when they miss the paddle.
Was This Post Helpful? 0
  • +
  • -

#6 BRI122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-December 12

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:29 PM

The actual JFrame gameboard itself was created previously by a professor because we did not learn how to create that an entire JFrame yet. I added in the Ball class, the Paddle class, the Blocks class, and all of the coding to the GameEnvironmentR7 class (which was there when I began my project, but it was blank). The DrawableAdapter and the GameBoard class are just for reference due to being previously created. I do not work with those classes or alter anything in them. I only work with the paddle class, the Ball class, the Blocks class, and the GameEnvironmentR7 class.

View Postpbl, on 15 December 2012 - 03:23 PM, said:

And was is the interface Drawable ?
And why is yout GameEnvironmentR7 object a static instance of it's own class.
Why such a long main() method ? You can have put most of the stuff in the constructor of GameEnvironmentR7 and get ride of all these static fields ?



The actual JFrame gameboard itself was created previously by a professor because we did not learn how to create that an entire JFrame yet. I added in the Ball class, the Paddle class, the Blocks class, and all of the coding to the GameEnvironmentR7 class (which was there when I began my project, but it was blank). The DrawableAdapter and the GameBoard class are just for reference due to being previously created. I do not work with those classes or alter anything in them. I only work with the paddle class, the Ball class, the Blocks class, and the GameEnvironmentR7 class.
Was This Post Helpful? 0
  • +
  • -

#7 BRI122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-December 12

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 03:35 PM

View Postihatesegfault, on 15 December 2012 - 03:27 PM, said:

For the paddle collisions you can just test for when the ball is at paddle height (plus or minus some small value), that is, since your handle is in the upper right hand corner, when the handle is the diameter above the paddle's handle. Then just test to see if the ball is over the paddle or empty space. so:

if(Math.abs(ball.y + ball.Diameter - paddle.y)) < epsilon) {
   // you know the ball is at the right height
   int dist = Math.abs(ball.x+ball.Diameter/2 - paddle.x+paddle.getWidth()/2);
   if(dist < paddle.getWidth()/2) {
       // you compare the distance from the middle of the ball to the middle of the paddle to half the length of the paddle
       // you know the ball is over the paddle, so make it bounce
       double num = someMultiplier*(dist*2/paddle.getWidth() + 1);
       ball.setYSpeed(-ball.getYSpeed()/num); // make it go up
       ball.setXSpeed(ball.getXSpeed()*num); // this will make the ball go at a different angle the farther from the center it hits.
   }
}



this is only for paddle collisions.

Think about the conditions that would define a collision with a block. Convert it so that it's in terms of the variables you have available to work with.

edit:

you can add an else to second if for when they miss the paddle.



Thank you for your reply! Where would I put this code though? Is this all I would need? No declaring rectangles or anything around the ball and paddle? And what variable does the "someMultiplier" stand for? Also, how does epsilon come into play? I am sorry for many questions, but I just want to make sure I fully understand the concept! Thank you again, I appreciate it!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 04:37 PM

Epsilon:

you can't really compare double numbers for equality using the == operator
because double that are the results of calculation may be different but you might want to consider the equals....
Let say that calculating the X coordinate of a component you end up to be at 10.0000000000 and calculating the X coordinate of another component you are at 9.99999999999999

Sure that x1 != x2 but for your program do you want to consider them == ?
In that case you defined double epsilon = yourf tolerance so lest say

double epsilon = 0.01;
abs(9.999999999 - 10.0000000) == 0.00000001 so it is < epsilon so should be considered ==

for int you can use ==
for double use: abs(double1 - double2) < tolerance

Happy coding
Was This Post Helpful? 0
  • +
  • -

#9 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: ! URGENT ! Brick Breaker Collisions

Posted 15 December 2012 - 05:24 PM

Just put it at the end of the loop that handles changing the ball's position.
You already have a "rectangle" declared for your paddle and ball, their just not one object. Your paddle and ball both have a handle x,y and width and height (diameter for the ball). Those are the bounding rectangles.

pbl told you what epsilon is.

someMultiplier will define how extremely the angle changes when the ball hits the paddle farther from the center. Note that the magnitude of the ball's velocity vector is not preserved. (sqrt(ball.getXSpeed()^2 + ball.getXSpeed^2) I know that's not how to do exponents). Watch what happens to the magnitude as multiple collisions occur. (assuming that someMultiplier = 1, ball's velocity is <1,1>, and it always hits on the end of the paddle)
0. magintude = sqrt(1^2 + 1^2) = sqrt(2)
1. = sqrt(2^2 + (1/2)^2) = sqrt(17/4) <2,1/2>
2. = sqrt(4^2 + (1/4)^2) = sqrt(257/16) <4,1/4>
In hindsight this was probably a bad way to do the angling. See if you can find one that preserves the magnitude.
Or you could just scale the vector so that it retains the same magnitude.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1