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 2 of 2 FirstFirst 12
Results 26 to 34 of 34

Thread: How to remove a brick (Java)

  1. #26
    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
    What happens when the if is executed and beenHit is false?
    nothing but it was meant to run the removeRectangle method

    Quote Originally Posted by Norm View Post
    What happens when the if is executed and beenHit is true?
    Again nothing but i wanted it to be that if it was true then it wouldn't run the removeRectangle method

    Quote Originally Posted by Norm View Post
    Setting beenHit true when it's true seems redundant.
    yeah i see what you mean about it being redundant.

    Why cant i add any methods into this part of my code

     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;
     
     
    	}

    it comes up with these errors which i dont know what they mean

     Exception in thread "main" java.lang.NullPointerException
    	at BouncingBall1.moveBall(BouncingBall1.java:118)
    	at BouncingBall1.<init>(BouncingBall1.java:110)
    	at BouncingBall1.main(BouncingBall1.java:33)
    Last edited by usherlad; August 30th, 2012 at 05:56 AM. Reason: missed code out

  2. #27
    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 jps View Post
    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.
    I have it stored as a method. How do i store it as a variable?

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

    Default Re: How to remove a brick (Java)

    I have finally managed to do it even though it may not be the correct way.

    I have done it for a brick i know the location of but now i want to try and improve it by having a randomly displayed brick.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to remove a brick (Java)

    i want to try and improve it by having a randomly displayed brick.
    Can you post the code you have now?
    The normal way is to make a class that represents the brick. The class contains all the info about the brick and has methods for accessing and changing that info.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to remove a brick (Java)

    yeah sure. I know have a button that adds the brick instead of the brick being added straight away. Only reason for this is because it states that in the assignment criteria.

     
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
     
    public class BouncingBall extends JFrame implements ActionListener {
     
    	private JPanel paper;
    	private JButton btnGo, btnStop, btnFinish, btnBrick, btnLeft, btnRight, btnUp, btnDown;
    	private int x, y, xChange, yChange, diameter;
    	private int x1, x2, y1, y2, width, height;
    	int col1, col2, col3;
    	private Timer timer1;
    	private JTextField txtScore;
    	private JLabel lblScore;
    	private int score;
    	private Random random;
     
    	boolean beenHit = false;
     
    	{
    	x1= 80;
    	x2 =120;
    	y1= 100;
    	y2 = 110;
    	width = 30;
    	height = 10;
     
    	}
     
    	public static void main(String[] args)
    	{
     
    		BouncingBall ball = new BouncingBall();
    		ball.setSize(550, 550);
    		ball.setBackground(Color.GREEN);
    		ball.setVisible(true);
     
    	}
     
    	public BouncingBall(){
     
    		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);
     
    		btnBrick = new JButton("Add Brick");
    		btnBrick.setPreferredSize(new Dimension (100,40));
    		btnBrick.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(btnBrick);
    		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);
    		btnBrick.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>=x1 && x<=x2) && (y==y1) && (beenHit == false))
     
    			removeRectangle();
     
     
    	}
     
     
     
    	public void drawRectangle()
    	{
     
    		Graphics myPen1 = paper.getGraphics();
    		myPen1.fillRect(80, 80, paper.getWidth(), paper.getHeight());
     
    			col1 = random.nextInt(255);
    			col2 = random.nextInt(255);
    			col3 = random.nextInt(255);
     
    		myPen1.setColor(new Color(col1, col2, col3));
    		myPen1.drawRect(x1, y1, width, height);
    	}
     
     
    	public void removeRectangle()
    	{
     
    		Graphics myPen2 = paper.getGraphics();
    		myPen2.setColor(Color.black);
    		myPen2.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
     
    		Score();
    		beenHit = true;		
    	}
     
    	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()==btnBrick)
     
    		{
    			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();
    	}
     
     
    	}

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to remove a brick (Java)

    Where is the class definition for the brick?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to remove a brick (Java)

    i don't know how to link classes together so i just used a method. The methos is called drawRectangle

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to remove a brick (Java)

    You need to learn how to define a class and use it. That will make it easier to have more than one brick and to have the bricks be at unique locations.
    Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    The code still has hardcoded/"magic" numbers in it. You need to replace all those numbers with variables whose values can be changed to allow the brick to be shown at different locations.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    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
    I have it stored as a method. How do i store it as a variable?
    Your method does not really store a brick, it just draws a filled rectangle.

    Like Norm said in post #29, you should have a brick class (and a ball class).

    The brick class constructor would be used to create an object to store in a variable.
    Brick myBrick = new Brick(myBrickSpec1, myBrickSpec2, myBrickSpecN...);

    Then later you can say:
    myBrick.move(deltaX, deltaY);
    or: (iirc you called your canvas 'paper')
    paper.remove(myBrick);

Page 2 of 2 FirstFirst 12

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