Member Avatar for Amoryethel
Amoryethel

Hi, I'm in the midst of creating a BrickBreaker game, and I'm having a bit of trouble repainting the Ball back to where it originally started at the beginning of the game. Which only happens when the ball hits the bottom of the screen (initY == bottomBound || initX == bottomBound). If anyone can give me a friendly push in the right direction, I would greatly appreciate it. Here's my code for the Ball class.

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
	 
public class Ball extends JPanel implements ActionListener
{
	public int initX, initY; //upper left coordinates of ball
	final static int DIAMETER = 10; 	
	public Ellipse2D Shape;
	public int rightBound = 230; 
	public int bottomBound = 350; //maximum x, y values
 	public int velocityX, velocityY; //controls speed and direction
	public Timer time;
	public int lives;
	
	public Ball()
	{
		initX = 98;
		initY = 302;
		velocityX = 5;
		velocityY = 5;
		time = new Timer(50, this);
	}   

	public void paint(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		draw(g2, initX, initY);
	}
	public void draw(Graphics2D g2, int x, int y)
	{
		g2.setPaint(Color.BLACK);
		g2.fill( new Ellipse2D.Double(initX, initY, 10, 10));
	}   
	public void setBounds(int width, int height)
	{
		rightBound = width - DIAMETER;
		bottomBound = height - DIAMETER;
	}     
	public void actionPerformed(ActionEvent e)
	{
		initX += velocityX;
		initY -= velocityY;
				
		if(initX < 0)
		{
			initX = 0;
			velocityX =- velocityX;
		}
		else
		{
			if(initX > rightBound)
			{
				initX = rightBound;
				velocityX =- velocityX;
			}
		}
		if(initY < 0)
		{
			initY = 0;
			velocityY =- velocityY;
		}
		if (initY == bottomBound || initX == bottomBound)
		{
			time.stop();
			lives = lives - 1;
			
		}
		else
		{
			if(initY > bottomBound)
			{
				initY = bottomBound;
				velocityY =- velocityY;
			}
		}
		repaint();
	}
	

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.