# How to remove a brick (Java)

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• August 10th, 2012, 04:32
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.

Code java:

```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 + "");   }```
• August 10th, 2012, 07:16
Norm
Re: How to remove a brick (Java)
Quote:

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.
• August 10th, 2012, 07:33
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
• August 10th, 2012, 07:49
Norm
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.
• August 10th, 2012, 08:45
Re: How to remove a brick (Java)
ok i will try that. Thank you
• August 10th, 2012, 13:01
jps
Re: How to remove a brick (Java)
Quote:

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.

Code java:

```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:
Quote:

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.
• August 22nd, 2012, 06:19
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.

Code :

```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(); }     }```
• August 22nd, 2012, 09:19
Norm
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. ????
• August 22nd, 2012, 10:43
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
• August 22nd, 2012, 11:04
Norm
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.

Quote:

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.
• August 22nd, 2012, 11:12
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
• August 22nd, 2012, 11:26
Norm
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.

Quote:

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.
• August 23rd, 2012, 05:15
Re: How to remove a brick (Java)
Quote:

Originally Posted by Norm
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?
• August 23rd, 2012, 05:29
Re: How to remove a brick (Java)
how do you write the code not to change the score? i have never been taught that
• August 23rd, 2012, 06:02
Re: How to remove a brick (Java)
i know this isnt the correct way to do it but so far i have got
Code :

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

Code :

``` 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.
• August 23rd, 2012, 06:30
Norm
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.
Quote:

how do you write the code not to change the score?
Code :

```if(!beenHit) { // change score & set beenHit }else{ // do not change the score }```
• August 23rd, 2012, 07:21
Re: How to remove a brick (Java)
so there is no way of linking the boolean in the moveBall method and the removeRectangle method?
• August 23rd, 2012, 07:29
Norm
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.
• August 23rd, 2012, 07:40
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
• August 23rd, 2012, 08:47
Norm
Re: How to remove a brick (Java)
Yes, all methods and constructors are defined at the class level.
• August 29th, 2012, 10:41
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()
• August 29th, 2012, 10:51
Norm
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?
• August 29th, 2012, 11:01
Re: How to remove a brick (Java)
ignore what i previously. I have wrote this at class level.

Code :

```  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
• August 29th, 2012, 11:20
Norm
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.
• August 29th, 2012, 13:28
jps
Re: How to remove a brick (Java)
Quote:

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: