Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: How to remove a brick (Java)

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to remove a brick (Java)

    Hi,

    Im stuck on what i think maybe a simple problem but im not sure what im doing wrong.

    My assignment is for a ball to move by the click of a button round the page and if it hits the brick, remove the brick and add 10 to the score. I've managed to do it all except when my ball hits one side of the brick it adds 10 to the score and when it hits the other side of the brick it adds another 10. How do i get it so that when it hits the brick it only adds 10 no matter how often it hits where the brick is/was. I know its the way i have done the coding but i dont no how else to do it and was wondering if someone get help me or point me in the right direction please.

    public void moveBall()
     
    	{
     
    		Graphics myPen = paper.getGraphics();
    		myPen.setColor(Color.black);
    		myPen.fillOval(x, y, diameter, diameter);
     
    		x = x + xChange;
    		y = y + yChange;
     
    		if(x <= 0)
    			xChange = -xChange;
    		if(y <= 0)
    			yChange = -yChange;
    		if(x >= paper.getWidth())
    			xChange = -xChange;
    		if(y >= paper.getHeight())
    			yChange = -yChange;
     
    		myPen.setColor(Color.white);
    		myPen.fillOval(x, y, diameter, diameter);
     
     
     
    		if((y==100 && x == 80) || (y==110 && x == 80))
    			removeRectangle();	
    		if((y==100 && x == 85) || (y==110 && x == 85))
    			removeRectangle();
    		if((y==100 && x == 90) || (y==110 && x == 90))
    			removeRectangle();
    		if((y==100 && x == 95) || (y==110 && x == 95))
    			removeRectangle();		
    		if((y==100 && x == 100) || (y==110 && x == 100))
    			removeRectangle();		
    		if((y==100 && x == 105) || (y==110 && x == 105))
    			removeRectangle();		
    		if((y==100 && x == 110) || (y==110 && x == 110))
    			removeRectangle();	
     
    		if((y==102 && x == 80) || (y==102 && x == 110))
    			removeRectangle();		
    		if((y==104 && x == 80) || (y==104 && x == 110))
    			removeRectangle();		
    		if((y==106 && x == 80) || (y==106 && x == 110))
    			removeRectangle();		
    		if((y==108 && x == 80) || (y==108 && x == 110))
    			removeRectangle();
     
     
    public void drawRectangle()
    	{
     
    	int x1, y1, width, height, col1, col2, col3;	
    	Graphics myPen1 = paper.getGraphics();
    	myPen1.fillRect(80, 100, paper.getWidth(), paper.getHeight());
     
     
     
     
    		col1 = random.nextInt(255);
    		col2 = random.nextInt(255);
    		col3 = random.nextInt(255);
     
     
    		myPen1.setColor(new Color(col1, col2, col3));
     
    		x1= 80;
    		y1= 100;
    		width = 30;
    		height = 10;
    		myPen1.drawRect(x1, y1, width, height);
    	}
     
    public void removeRectangle()
    	{
    		Graphics myPen1 = paper.getGraphics();
    		myPen1.setColor(Color.black);
    		myPen1.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
     
     
    		score = score+10;
    		txtScore.setText(score + "");
     
    	}
    Attached Files Attached Files
    Last edited by usherlad; August 10th, 2012 at 04:54 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    How do i get it so that when it hits the brick it only adds 10
    An idea: Have a flag for the brick that records if it has been been hit. If the flag is set, don't add.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    im a bit of a beginner at this to say the least. How would i go about doing this?

    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    In the object for a brick add a boolean: beenHit that is initially false. Set it true when the brick is hit.
    Use its value to control when to add: add if false, do not add if true.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    ok i will try that. Thank you

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to remove a brick (Java)

    Quote Originally Posted by usherlad View Post
    Hi,

    Im stuck on what i think maybe a simple problem but im not sure what im doing wrong.

    My assignment is for a ball to move by the click of a button round the page and if it hits the brick, remove the brick and add 10 to the score. I've managed to do it all except when my ball hits one side of the brick it adds 10 to the score and when it hits the other side of the brick it adds another 10. How do i get it so that when it hits the brick it only adds 10 no matter how often it hits where the brick is/was. I know its the way i have done the coding but i dont no how else to do it and was wondering if someone get help me or point me in the right direction please.

    public void moveBall()
     
    	{
     
    		Graphics myPen = paper.getGraphics();
    		myPen.setColor(Color.black);
    		myPen.fillOval(x, y, diameter, diameter);
     
    		x = x + xChange;
    		y = y + yChange;
     
    		if(x <= 0)
    			xChange = -xChange;
    		if(y <= 0)
    			yChange = -yChange;
    		if(x >= paper.getWidth())
    			xChange = -xChange;
    		if(y >= paper.getHeight())
    			yChange = -yChange;
     
    		myPen.setColor(Color.white);
    		myPen.fillOval(x, y, diameter, diameter);
     
     
     
    		if((y==100 && x == 80) || (y==110 && x == 80))
    			removeRectangle();	
    		if((y==100 && x == 85) || (y==110 && x == 85))
    			removeRectangle();
    		if((y==100 && x == 90) || (y==110 && x == 90))
    			removeRectangle();
    		if((y==100 && x == 95) || (y==110 && x == 95))
    			removeRectangle();		
    		if((y==100 && x == 100) || (y==110 && x == 100))
    			removeRectangle();		
    		if((y==100 && x == 105) || (y==110 && x == 105))
    			removeRectangle();		
    		if((y==100 && x == 110) || (y==110 && x == 110))
    			removeRectangle();	
     
    		if((y==102 && x == 80) || (y==102 && x == 110))
    			removeRectangle();		
    		if((y==104 && x == 80) || (y==104 && x == 110))
    			removeRectangle();		
    		if((y==106 && x == 80) || (y==106 && x == 110))
    			removeRectangle();		
    		if((y==108 && x == 80) || (y==108 && x == 110))
    			removeRectangle();
     
     
    public void drawRectangle()
    	{
     
    	int x1, y1, width, height, col1, col2, col3;	
    	Graphics myPen1 = paper.getGraphics();
    	myPen1.fillRect(80, 100, paper.getWidth(), paper.getHeight());
     
     
     
     
    		col1 = random.nextInt(255);
    		col2 = random.nextInt(255);
    		col3 = random.nextInt(255);
     
     
    		myPen1.setColor(new Color(col1, col2, col3));
     
    		x1= 80;
    		y1= 100;
    		width = 30;
    		height = 10;
    		myPen1.drawRect(x1, y1, width, height);
    	}
     
    public void removeRectangle()
    	{
    		Graphics myPen1 = paper.getGraphics();
    		myPen1.setColor(Color.black);
    		myPen1.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
     
     
    		score = score+10;
    		txtScore.setText(score + "");
     
    	}
    In 3 different methods you have the same line (except once the variable name is different:
    Graphics myPen = paper.getGraphics();
    Graphics myPen1 = paper.getGraphics();
    Graphics myPen1 = paper.getGraphics();
    This tells me you should have a higher level scope for just one variable and use it in all of the methods.

    In your moveBall method it looks like you are keeping the values of x and y outside the method. I would suggest using an object rather than just two variables (a ball class perhaps). Then when your ball moves, you are drawing a new black ball over top of the old white ball. Then you check against the edges of your paper and draw a new white ball. This means that every time you move your ball, you actually draw 2 new ones. How long before you have hundreds of white ovals drawn under black ovals? thousands? run out of memory? By tracking just one ball object, you can actually move it, rather than drawing 2 new every moveBall call, and only ever have one ball drawn on the screen at a time.

    In your moveBall method you check for hitting a brick by checking the location of the ball. Does this actually verify a brick is present? Or does it just verify the ball is in the position of a brick?

    As far as your removeRectangle() method, changing the color of the brick to match the color of the background does not remove the object. Same problem the ball has with being redrawn. You never actually remove the brick, you just draw a black one over it. Now there are two bricks in that location. Here I would suggest the same solution as the ball. Perhaps a new brick class. Keep track of a group of bricks on the paper, and when a brick is hit, actually remove that brick object from the paper, don't hide it behind a new object.

    Post all of your code if you have more questions so we can get a better idea of what is going on, and be more prepared to offer help. Sometimes a code snippet is enough, but sometimes it just helps to see the whole thing or a smaller version of the whole thing.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    Hi, Sorry its taken ages for me to get back to you. I've been away.

    I understand what your saying about drawing a black ball over a white ball and potentially running out of memory but thats the only way that i have been taught. I'm only using one brick so i shouldnt run out of memory.

    In my moveBall method it verifys that the ball is in the position of the brick.

    Im not sure how to remove the brick tho. I keep trying but i always end up where i started with a little bit more grey hair.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
     
    public class BouncingBall1 extends JFrame implements ActionListener {
     
    	private JPanel paper;
    	private JButton btnGo, btnStop, btnFinish, btnLeft, btnRight, btnUp, btnDown;
    	private int x, y, xChange, yChange, diameter;
    	private int x1, y1;
    	private Timer timer1;
    	private JTextField txtScore;
    	private JLabel lblScore;
    	private int score;
    	private Random random;
     
    	public static void main(String[] args)
    	{
     
    		BouncingBall1 ball1 = new BouncingBall1();
    		ball1.setSize(550, 550);
    		ball1.setBackground(Color.GREEN);
    		ball1.setVisible(true);
     
    	}
     
    	public BouncingBall1(){
    		setLayout(new FlowLayout());	
     
    		btnGo = new JButton("Start");
    		btnGo.setPreferredSize(new Dimension (70,40));
    		btnGo.setBackground(Color.gray);
     
    		btnStop = new JButton("Stop");
    		btnStop.setPreferredSize(new Dimension (70,40));
    		btnStop.setBackground(Color.gray);
     
    		btnFinish = new JButton("Finish");
    		btnFinish.setPreferredSize(new Dimension (70,40));
    		btnFinish.setBackground(Color.gray);
     
    		btnLeft = new JButton("Left");
    		btnLeft.setPreferredSize(new Dimension (70,40));
    		btnLeft.setBackground(Color.gray);
     
    		btnRight = new JButton("Right");
    		btnRight.setPreferredSize(new Dimension (70,40));
    		btnRight.setBackground(Color.gray);
     
    		btnUp = new JButton("Up");
    		btnUp.setPreferredSize(new Dimension (70,40));
    		btnUp.setBackground(Color.gray);
     
    		btnDown = new JButton("Down");
    		btnDown.setPreferredSize(new Dimension (70,40));
    		btnDown.setBackground(Color.gray);
     
    		lblScore = new JLabel("Score");
    		lblScore.setPreferredSize(new Dimension(50,40));
    		lblScore.setBackground(Color.gray);
     
    		txtScore = new JTextField(3);
    		txtScore.setFont(new Font("TimesRoman", Font.BOLD,16));
    		txtScore.setBackground(Color.white);
    		txtScore.setForeground(Color.blue);
     
    		paper = new JPanel();
    		paper.setPreferredSize(new Dimension(500,400));
    		paper.setBackground(Color.black);
     
     
     
    		add(btnGo);
    		add(lblScore);
    		add(txtScore);
    		add(btnStop);
    		add(btnFinish);
    		add(paper);
    		add(btnLeft);
    		add(btnRight);
    		add(btnUp);
    		add(btnDown);
     
    		btnGo.addActionListener(this);
    		btnStop.addActionListener(this);
    		btnFinish.addActionListener(this);
    		btnLeft.addActionListener(this);
    		btnRight.addActionListener(this);
    		btnUp.addActionListener(this);
    		btnDown.addActionListener(this);
     
    		random = new Random();
     
    		x=10; y=10; xChange = 10; yChange = 0; diameter = 10;
     
    	}
     
    	public void moveBall()
     
    	{
     
    		Graphics myPen = paper.getGraphics();
    		myPen.setColor(Color.black);
    		myPen.fillOval(x, y, diameter, diameter);
     
    		x = x + xChange;
    		y = y + yChange;
     
    		if(x <= 0)
    			xChange = -xChange;
    		if(y <= 0)
    			yChange = -yChange;
    		if(x >= paper.getWidth())
    			xChange = -xChange;
    		if(y >= paper.getHeight())
    			yChange = -yChange;
     
    		myPen.setColor(Color.white);
    		myPen.fillOval(x, y, diameter, diameter);
     
     
    		if((x>=80 && x<=110) && (y==110))
    			removeRectangle();
     
     
     
    	}
     
    	public void drawRectangle()
    	{
     
    	int x1, y1, width, height, col1, col2, col3;	
    	Graphics myPen1 = paper.getGraphics();
    	myPen1.fillRect(80, 100, paper.getWidth(), paper.getHeight());
     
     
     
     
    		col1 = random.nextInt(255);
    		col2 = random.nextInt(255);
    		col3 = random.nextInt(255);
     
     
    		myPen1.setColor(new Color(col1, col2, col3));
     
    		x1= 80;
    		y1= 100;
    		width = 30;
    		height = 10;
    		myPen1.drawRect(x1, y1, width, height);
    	}
     
     
     
     
     
    	public void removeRectangle()
    	{
    		Graphics myPen1 = paper.getGraphics();
    		myPen1.setColor(Color.black);
    		myPen1.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
     
    		Score();
     
    	}
     
    	public void Score()
     
    	{
    		score = score+10;
    		txtScore.setText(score + "");
     
    	}
     
     
     
     
     
    	public void actionPerformed(ActionEvent event)
     
    	{
    		if(event.getSource()==btnGo)
     
    		{
    			timer1 = new Timer(50,this);
    			timer1.start();
     
    		}
     
    		if(event.getSource()==timer1)
    		{
    			moveBall();
    		}
     
    		if(event.getSource()==btnStop)
    		{
    			timer1.stop();
     
    		}
     
    		if(event.getSource()==btnGo)
     
    		{
    			drawRectangle();
    		}
     
     
    		if(event.getSource()==btnFinish)
    		{
    			JOptionPane.showMessageDialog(null, "Thank You for Playing! You scored  " +score++);
    			System.exit(0);
     
    		}
     
    		if(event.getSource()==btnLeft)
    		{
    			xChange = -10; yChange = 0;
    		}
    		if(event.getSource()==btnRight)
    		{
    			xChange = 10; yChange = 0;
    		}
    		if(event.getSource()==btnUp)
    		{
    			xChange = 0; yChange = -10;
    		}
    		if(event.getSource()==btnDown)
    		{
    			xChange = 0; yChange = 10;
    		}
    		moveBall();
    	}
     
     
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    Can you explain what happens when you execute the program and what the problem is?
    When I execute it,I get a fast moving round white shape and a rectangle. Changing how the round shape moves using the buttons can cause it to pass through and remove the rectangle. ????
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    yeah thats what it does and when the ball hits the rectangle it adds 10points to the score. Its very basic i know but its all i need to do. The problem with it is that i only want it to add 10points once and not everytime it passes through the space where the rectangle is. Thats the only thing im stuck on and once i've managed to sort that out i will have finished the program

    Hope that makes sense

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    One problem I see in the code is too many hardcoded values. Most of the numbers should be in variables that describe their usage which will make the program more maintainable and easier to read and understand.

    i only want it to add 10points once and not everytime
    If the brick had a state: beenHit (a boolean) that was false before it was hit and true afterwards, its value could be used to determine if the score should be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    yeah i remember you saying that previously. Because im a beginner it all sounds double dutch unfortunately.

    I understand about the hardcoded values but i didnt know what else to do.

    I'm not asking you to do it for me at all but is there any way could point me abit more in the right direction please

    Thank you

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    Where are the variables defined that hold the state of a brick? For example its x,y location?
    Add another boolean variable to the group of variables: beenHit and set it initially false.
    When a brick is hit, test its beenHit variable
    if false, set it true and change the score
    if true, don't change the score.

    the hardcoded values but i didnt know what else to do
    Define some variables with descriptive names and assign them the value.
    In the code, replace the hardcoded value with the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    Quote Originally Posted by Norm View Post
    Where are the variables defined that hold the state of a brick?
    they're held in drawRectangle.

    been tryiing to do this all night but not getting anywhere.

    This may sound stupid but what are the public voids called. Are they called subclasses? can you link a boolean expression between them?

  14. #14
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    how do you write the code not to change the score? i have never been taught that

  15. #15
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    i know this isnt the correct way to do it but so far i have got
     public void moveBall()
     
    	{
    		boolean beenHit = false;	
    		Graphics myPen = paper.getGraphics();
    		myPen.setColor(Color.black);
    		myPen.fillOval(x, y, diameter, diameter);
     
    		x = x + xChange;
    		y = y + yChange;
     
    		if(x <= 0)
    			xChange = -xChange;
    		if(y <= 0)
    			yChange = -yChange;
    		if(x >= paper.getWidth())
    			xChange = -xChange;
    		if(y >= paper.getHeight())
    			yChange = -yChange;
     
    		myPen.setColor(Color.white);
    		myPen.fillOval(x, y, diameter, diameter);
     
     
    		if((x>=80 && x<=110) && (y==110) && (beenHit == false))
    			removeRectangle();
     
    	}

    im trying to get the boolean in removeRectangle() set to true so that once the rectangle has been removed it wont try and remove the rectangle again therefore not adding the score again if that makes sense.

     public void removeRectangle()
    	{
    		boolean beenHit = true;
    		Graphics myPen2 = paper.getGraphics();
    		myPen2.setColor(Color.black);
    		myPen2.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
     
     
    		Score();
     
    	}

    the code wont work and im not sure why.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    You need to consider how local variables (defined in a method) and variables defined at the class level remember their values. Local variables only hold values when the method is executing. When the method exits their values are gone. Class variables hold their values across mehod calls.
    You want the beenHit variable to remember its value between and across method calls, so you need to define it outside of any methods.
    how do you write the code not to change the score?
    if(!beenHit) {
      //  change score & set beenHit
    }else{
      //  do not change the score
    }
    Last edited by Norm; August 23rd, 2012 at 07:03 AM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    so there is no way of linking the boolean in the moveBall method and the removeRectangle method?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    No. Those two variables only exist when the methods are executed. You need to define a variable at the class level. For example the variable: paper is at the class level.

    You also need to remove the hardcoded values: 100, 110, 80 etc and use variables instead. The variables should be defined at the class level also.
    Using hardcoded variables makes the code very hard to change. Say you wanted to have the brick move to a new location each time a new brick is created. If its location is stored in a variable, then to move it, simply change the values in the variables.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    sorry if all my questions are stupid but im just trying to find out.

    is public static void main(String[] args) or public BouncingBall1(){ class level

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    Yes, all methods and constructors are defined at the class level.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    can i link a boolean and method together. For example:

    Boolen BeenHit = false

    .........

    if false
    drawRectangle()

    else
    removeRectangle()

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    What do you mean by "link a boolean and method together"?
    boolean is a data type. You can define variables of type boolean.
    A boolean variable can be used as the condition of an if statement.
    Boolean is a class that wraps a boolean value.

    Can you write a description in pseudo code or English of what you want to do?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to remove a brick (Java)

    ignore what i previously. I have wrote this at class level.

     
    if ((x>=x1 && x<=x2) && (y==y1) && beenHit == false){
    				removeRectangle();
     
    		}else{
     
    			beenHit = true;

    its so that once it has ran the removeRectangle method it wont run it again therefore not adding more than it should. At the moment its not even adding anything to the score

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to remove a brick (Java)

    What happens when the if is executed and beenHit is false?
    What happens when the if is executed and beenHit is true?

    Setting beenHit true when it's true seems redundant.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to remove a brick (Java)

    Quote Originally Posted by usherlad View Post
    Hi, Sorry its taken ages for me to get back to you. I've been away.

    I understand what your saying about drawing a black ball over a white ball and potentially running out of memory but thats the only way that i have been taught. I'm only using one brick so i shouldnt run out of memory.
    Only using one brick, (for now) is not a good reason to continue down a rocky road.

    Quote Originally Posted by usherlad View Post
    In my moveBall method it verifys that the ball is in the position of the brick.
    Correct. So even if the brick was gone rather than 'invisible-on-screen', your method will still signal that you hit a brick when the ball enters the location because that is what you test for.

    Quote Originally Posted by usherlad View Post
    Im not sure how to remove the brick tho. I keep trying but i always end up where i started with a little bit more grey hair.
    Store your brick object in a variable so you have access to it. Then you can add/relocate/remove it with a method call. Same for the ball.

Page 1 of 2 12 LastLast

Similar Threads

  1. JAVA AWT GRAPHICS REMOVE SOMETHING THAT IS DRAWN
    By michael55131 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 18th, 2012, 08:08 AM
  2. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  3. Make brick wall using BlueJ
    By boumasmoud in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 3rd, 2011, 04:32 PM
  4. Destroy Brick
    By Ceasar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 10th, 2009, 04:36 AM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM