• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Quick Java Help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

TalRW

Member
Joined
Mar 2, 2004
Location
Irvine, CA
Hi, I'm programming the basic version of breakout. The class I'm taking (not for a grade) is the online version of CS106A (http://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111)

If you read the PDF on the breakout assignment they actually don't specify that you should make different classes for each part but I want to take a more object oriented way of programming this game. So thus I have a ball class which keeps track of the ball used in the game. The part I'm having trouble with is the collision detection part. This class makes use of the ACM libraries. When you do graphics using the ACM libraries the easiest way to think of it is like a felt board. You have a GCanvis which is like the board and then you add objects to it. The ball is a GOval which is defined by a x and y location and a length and width. Basically it's defined by a rectangle which surrounds the GOval. To do basic collision detection they want you to check those 4 points surrounding the ball and see if it has hit anything. You are supposed to use the method getElementAt(x,y) which returns the topmost object if it detects a object there and null if nothing. The problem is I don't know how to call that method getElementAt(x,y) from inside this class I'm defining. I can call this method from the main program but not from inside the ball class. if anyone could help me out that would be great. Here is that I have so far. The problem is in the method called checkCorner()

When I try calling GCanvas.getElementAt(xLocation, yLocation) I get the following error
Description Resource Path Location Type
Cannot make a static reference to the non-static method getElementAt(double, double) from the type GCanvas Ball.java Book_Problems line 53 Java Problem

Code:
/*  This class implements the ball in my implementation of "breakout"
 	I am claiming fair use as this implementation is for educational
 	purposes and not for commercial use.  
    Copyright (C) 2009  Grant H.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class Ball extends GOval{
	
	public Ball (double xLoc, double yLoc) {
		super(xLoc,yLoc, 2 * BALL_RADIUS, 2 * BALL_RADIUS);
		setFilled(true);
		setColor(Color.black);
	}
	
	public int getRadius() {
		return BALL_RADIUS;
	}
	
	/* The ball is a GOval which is defined by its bounding box.  This method checks the four corners
	 * surrounding the ball to see if it has collided with anything (wall, paddle, bricks, ect.)
	 */
	public void checkForCollisions() {
		/* Provides the corner locations for the ball*/
		double upperLeftX = this.getX(); double upperLeftY = this.getY(); 											/* X and Y coordinates of the upper-left corner */
		double lowerLeftX = this.getX(); double lowerLeftY = this.getY() + this.getHeight(); 						/* X and Y coordinates of the lower-left corner */
		double upperRightX = this.getX() + this.getWidth(); double upperRightY = this.getY(); 						/* X and Y coordinates of the upper-right corner */
		double lowerRightX = this.getX() + this.getWidth(); double lowerRightY = this.getY() + this.getHeight(); 	/* X and Y coordinates of the lower-right corner */
		
		checkCorner(upperLeftX, upperLeftY); 	/*Checks the upper-left corner for a collision */
		checkCorner(lowerLeftX, lowerLeftY);	/*Checks the lower-left corner for a collision */
		checkCorner(upperRightX, upperRightY); 	/*Checks the upper-right corner for a collision */
		checkCorner(lowerRightX, lowerRightY); 	/*Checks the lower-right corner for a collision */
	}
	
	private boolean checkCorner(double xLocation, double yLocation) {
		if(getElementAt(xLocation,yLocation) != null) return true;
		return false;
	}
	
	/** Radius of the ball in pixels */
	private static final int BALL_RADIUS = 10;

}
 
Last edited:
OP
TalRW

TalRW

Member
Joined
Mar 2, 2004
Location
Irvine, CA
Ok, well problem solved. I asked my dad who is a software engineer. Just for future reference what I had to do was pass the breakout class (the main program) as a parameter in the ball constructor. This is because the GCanvas is part of the graphics program. Then I use that variable and I was able to call the getElementAt method.