Cannot find class error.

Finished an assignment, yet I can't figure out why it produces thi

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 5801 Views - Last Post: 10 December 2008 - 08:29 PM Rate Topic: -----

#1 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Cannot find class error.

Posted 10 December 2008 - 09:28 AM

I just finished an assignment that required making balls bounce inside a box. That class was called BoxBall and was run from another class called BallDemo. I thought I did everything correct but when I go to compile, I receive the error that it cannot find the class BoxBall cannot be found and I do not no why. I'm probably missing something really obvious.

Ball Demo code
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Iterator;

public class BallDemo
{
	private Canvas myCanvas;


	/**
	 * Create a BallDemo object. Creates a fresh canvas and makes it visible.
	 */
	public BallDemo()
	{
		myCanvas = new Canvas("Ball Demo", 600, 500);
		myCanvas.setVisible(true);
	}
 
	/**
	 * Demonstrate some of the drawing operations that are
	 * available on a Canvas object.
	 */
	public void drawDemo()
	{
		myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
		myCanvas.setForegroundColor(Color.red);

		myCanvas.drawString("We can draw text, ...", 20, 30);
		myCanvas.wait(1000);

		myCanvas.setForegroundColor(Color.black);
		myCanvas.drawString("...draw lines...", 60, 60);
		myCanvas.wait(500);
		myCanvas.setForegroundColor(Color.gray);
		myCanvas.drawLine(200, 20, 300, 50);
		myCanvas.wait(500);
		myCanvas.setForegroundColor(Color.blue);
		myCanvas.drawLine(220, 100, 370, 40);
		myCanvas.wait(500);
		myCanvas.setForegroundColor(Color.green);
		myCanvas.drawLine(290, 10, 320, 120);
		myCanvas.wait(1000);
		myCanvas.setForegroundColor(Color.red);
		myCanvas.drawLine(290, 22, 350, 110);
		myCanvas.wait(1000);

		myCanvas.setForegroundColor(Color.gray);
		myCanvas.drawString("...and shapes!", 110, 90);

		myCanvas.setForegroundColor(Color.red);

		// the shape to draw and move
		int xPos = 10;
		Rectangle rect = new Rectangle(xPos, 150, 30, 20);

		// move the rectangle across the screen
		for(int i = 0; i < 200; i ++) {
			myCanvas.fill(rect);
			myCanvas.wait(10);
			myCanvas.erase(rect);
			xPos++;
			rect.setLocation(xPos, 150);
		}
		// at the end of the move, draw once more so that it remains visible
		myCanvas.fill(rect);
	}

	/**
	 * Simulate user defined set of bouncing balls.
	 */
   
	public void bounce(int numOfBalls)
	{
		int ground = 400;   // position of the ground line
		myCanvas.setVisible(true);

		// draw the ground
		myCanvas.drawLine(50, ground, 550, ground);


		// create and show the balls
		ArrayList<BouncingBall> balls = new ArrayList();
		Random numGen = new Random();
		
		
//		 BouncingBall ball = new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
//		 ball.draw();
//		 BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
//		 ball2.draw();

		for (int counter = 0; counter < numOfBalls; counter++)
			{
				BouncingBall ball = new BouncingBall(numGen.nextInt(350), numGen.nextInt(80), numGen.nextInt(50), Color.blue, ground, myCanvas);
				balls.add(ball);
			}
   
		// make them bounce
		boolean finished =  false;
		while(!finished) {
			myCanvas.wait(50);  // small delay
			
			for (int ballCount = 0; ballCount < numOfBalls; ballCount++)
			{
				balls.get(ballCount).move();
			}
			
			// stop once ball has travelled a certain distance on x axis
			if(balls.get(1).getXPosition() >= 550) {
				finished = true;
			}
		}
//		 ball.erase();
//		 ball2.erase();
	}
	
	/**
	 * Draws different colored balls bouncing in a box
	 */
	public void boxBounce(int numOfBalls)
	{
		myCanvas.setVisible(true);
		
		// draw the box
		Rectangle box = new Rectangle(50,50,300,300);
		myCanvas.draw(box);
		
		//create the balls
		Random numGen = new Random();
		ArrayList<BouncingBall> balls = new ArrayList();
		for (int ballNum = 0; ballNum <numOfBalls; ballNum++)
		{
			Dimension size = myCanvas.getSize();
			int x = (int) box.getX() + numGen.nextInt((int) box.getWidth());
			int y = (int) box.getY() + numGen.nextInt((int) box.getHeight());
			int xSpeed = numGen.nextInt(25);
			int ySpeed = numGen.nextInt(25);
			
			Color color = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
			BoxBall ball= new Boxball(x, y, xSpeed, ySpeed, 16, color, box, myCanvas);
			
			balls.add(ball);
			ball.draw();
		}
		
		//bounce the balls
		boolean finished = false;
		while (!finished)
		{
		   myCanvas.wait(75);
		   Iterator it = balls.iterator();
		   
		   while(it.hasNext())
		   {
			   BoxBall ball = (BoxBall) it.next();
			   ball.move();
			   
			   //stop after ball moves a certain distance across the x axis
			   
			   if (ball.isMoving() == false)
			   {
				   stoppedballs ++;
				}
				if (stoppedballs == numOfBalls)
				{
					finished = true;
				}
		}
		Iterator it = balls.iterator();
		while(it.hasNExt())
		{
			BoxBall ball = (BoxBall) it.next();
			ball.erase();
		}
	   }
   }
	
	/**
	 * Draw a frame 20 pixels in from the size of the canvas.
	 */
	public void drawFrame()
	{
		myCanvas.getSize();
		Rectangle frame = new Rectangle(20, 20, ((int)myCanvas.getSize().getWidth() - 40), ((int)myCanvas.getSize().getHeight() - 40));
		myCanvas.draw(frame);
	}
		   
}





BoxBall code
import java.awt.*;
import java.awt.geom.*;
public class BoxBall
{
	private int ballDegradation = 2;
	private Ellipse2D.Double circle;
	private Color color;
	private int diameter;
	private int xPos;
	private int yPos;
	private final Rectangle walls;	  
	private Canvas canvas;
	private int xSpeed;
	private int ySpeed;
   
	/**
	 * Constructor for objects of the class BoxBall
	 */
	public BoxBall(Color ballColor, int ballDiameter, int xPos_, int yPos_, Canvas myCanvas,
					int xSpeed, int ySpeed, Rectangle boundary)
	{
		color = ballColor;
		diameter = ballDiameter;
		xPos = xPos_;
		yPos = yPos_;
		canvas = myCanvas;
		this.xSpeed = xSpeed;
		this.ySpeed = ySpeed;
		walls = boundary;
		
	}
	
	/**
	 * Draw the ball on the canvas.
	 */
	public void drawBall()
	{
		canvas.setForegroundColor(color);
		canvas.fillCircle(xPos, yPos, diameter);
	}
	
	public void eraseBall()
	{
		canvas.eraseCircle(xPos, yPos, diameter);
	}
	
	public void move()
	{
		//remove the ball from canvas
		eraseBall();
		
		yPos += ySpeed;
		xPos += xSpeed;
		
		//check for ball collision with the border
		if (yPos >= (walls.getMaxY() - diameter) && ySpeed > 0)
		{
			yPos = (int)(walls.getMaxY() - diameter);
			ySpeed = -ySpeed + ballDegradation;
			
			if (ySpeed > 0) 
			{
				ySpeed = 0;
			}
		}
		else if(yPos <= (walls.getMinY()) && ySpeed < 0)
		{
			yPos = (int)(walls.getMinY());
			ySpeed = -ySpeed - ballDegradation; 
			
			if(ySpeed < 0)
			{
				ySpeed = 0;
			}
		}
		else if(xPos >= (walls.getMaxX() - diameter) && xSpeed > 0) 
		{
			xPos = (int)(walls.getMaxX() - diameter);
			xSpeed = -xSpeed + ballDegradation; 
			
			if(xSpeed > 0)

			{
				xSpeed = 0;
			}
		}
		else if(xPos <= (walls.getMinX()) && xSpeed < 0) 
		{
			xPos = (int)(walls.getMinX());
			xSpeed = -xSpeed - ballDegradation;
			if(xSpeed < 0) 
			{
				xSpeed = 0;
			}
		}
		
		//redraw at a new position
		drawBall();
	}
	
	/**
	 * Return the ball's horizontal position
	 */
	public int getXPosition()
	{
		return xPos;
	}
	
	/**
	 * Return the ball's vertical position
	 */
	public int getYPosition()
	{
		return yPos;
	}
	
	/**
	 * Returns true if the xSpeed and ySpeed of the ball is not 0,
	 * therefore, the ball is moving.  Otherwise return false.
	 */
	 public boolean isMoving()
	{
		if (xSpeed == 0 && ySpeed == 0)
		{
			return false;
		}
		
		return true;
	}
	
}





Is This A Good Question/Topic? 1

Replies To: Cannot find class error.

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:29 AM

Did you compile BoxBall first? If not, then your driver may not have the .class file to work with.
Was This Post Helpful? 0
  • +
  • -

#3 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 09:34 AM

Yea I compiled BoxBall first and tried extending it as part of BallDemo but that did not work either
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:36 AM

What is your file directory structure? Extending means BallDemo Inherits BoxBall and that doesn't really make sense in this context.
Was This Post Helpful? 0
  • +
  • -

#5 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 09:42 AM

Everything is located in one folder. The folder contains the .class for every class I created. The dependencies are the same as a friends project and we helped each other with it, yet his works.
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:44 AM

What is the exact error verbatim?
Error on this line?

 BoxBall ball= new Boxball(x, y, xSpeed, ySpeed, 16, color, box, myCanvas);



Was This Post Helpful? 0
  • +
  • -

#7 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 09:47 AM

Yea, the error is on that line.
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:47 AM

Quote

What is the exact error verbatim?


Where is BouncingBall defined?

This post has been edited by KYA: 10 December 2008 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#9 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 09:49 AM

Ah sorry. The error occurs on that line. It is "cannot find symbol - class BoxBall"
Was This Post Helpful? 0
  • +
  • -

#10 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:51 AM

Canvas at Java API

Are you using some other external library? You are using constructors and methods that vanilla canvases don't have.
Was This Post Helpful? 0
  • +
  • -

#11 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 09:57 AM

Yes, the code for canvas was written by the books authors. Here it is.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/**
 * Class Canvas - a class to allow for simple graphical 
 * drawing on a canvas.
 * 
 * @author Michael Kolling (mik)
 * @author Bruce Quig
 *
 * @version 2008.03.30
 */

public class Canvas
{
	private JFrame frame;
	private CanvasPane canvas;
	private Graphics2D graphic;
	private Color backgroundColor;
	private Image canvasImage;

	/**
	 * Create a Canvas with default height, width and background color 
	 * (300, 300, white).
	 * @param title  title to appear in Canvas Frame	 
	 */
	public Canvas(String title)
	{
		this(title, 300, 300, Color.white);		
	}

	/**
	 * Create a Canvas with default background color (white).
	 * @param title  title to appear in Canvas Frame
	 * @param width  the desired width for the canvas
	 * @param height  the desired height for the canvas
	 */
	public Canvas(String title, int width, int height)
	{
		this(title, width, height, Color.white);
	}

	/**
	 * Create a Canvas.
	 * @param title  title to appear in Canvas Frame
	 * @param width  the desired width for the canvas
	 * @param height  the desired height for the canvas
	 * @param bgClour  the desired background color of the canvas
	 */
	public Canvas(String title, int width, int height, Color bgColor)
	{
		frame = new JFrame();
		canvas = new CanvasPane();
		frame.setContentPane(canvas);
		frame.setTitle(title);
		canvas.setPreferredSize(new Dimension(width, height));
		backgroundColor = bgColor;
		frame.pack();
	}

	/**
	 * Set the canvas visibility and brings canvas to the front of screen
	 * when made visible. This method can also be used to bring an already
	 * visible canvas to the front of other windows.
	 * @param visible  boolean value representing the desired visibility of
	 * the canvas (true or false) 
	 */
	public void setVisible(boolean visible)
	{
		if(graphic == null) {
			// first time: instantiate the offscreen image and fill it with
			// the background color
			Dimension size = canvas.getSize();
			canvasImage = canvas.createImage(size.width, size.height);
			graphic = (Graphics2D)canvasImage.getGraphics();
			graphic.setColor(backgroundColor);
			graphic.fillRect(0, 0, size.width, size.height);
			graphic.setColor(Color.black);
		}
		frame.setVisible(true);
	}

	/**
	 * Provide information on visibility of the Canvas.
	 * @return  true if canvas is visible, false otherwise
	 */
	public boolean isVisible()
	{
		return frame.isVisible();
	}

	/**
	 * Draw the outline of a given shape onto the canvas.
	 * @param  shape  the shape object to be drawn on the canvas
	 */
	public void draw(Shape shape)
	{
		graphic.draw(shape);
		canvas.repaint();
	}
 
	/**
	 * Fill the internal dimensions of a given shape with the current 
	 * foreground color of the canvas.
	 * @param  shape  the shape object to be filled 
	 */
	public void fill(Shape shape)
	{
		graphic.fill(shape);
		canvas.repaint();
	}

	/**
	 * Fill the internal dimensions of the given circle with the current 
	 * foreground color of the canvas.
	 */
	public void fillCircle(int xPos, int yPos, int diameter)
	{
		Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
		fill(circle);
	}

	/**
	 * Fill the internal dimensions of the given rectangle with the current 
	 * foreground color of the canvas. This is a convenience method. A similar 
	 * effect can be achieved with the "fill" method.
	 */
	public void fillRectangle(int xPos, int yPos, int width, int height)
	{
		fill(new Rectangle(xPos, yPos, width, height));
	}

	/**
	 * Erase the whole canvas.
	 */
	public void erase()
	{
		Color original = graphic.getColor();
		graphic.setColor(backgroundColor);
		Dimension size = canvas.getSize();
		graphic.fill(new Rectangle(0, 0, size.width, size.height));
		graphic.setColor(original);
		canvas.repaint();
	}

	/**
	 * Erase the internal dimensions of the given circle. This is a 
	 * convenience method. A similar effect can be achieved with
	 * the "erase" method.
	 */
	public void eraseCircle(int xPos, int yPos, int diameter)
	{
		Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
		erase(circle);
	}

	/**
	 * Erase the internal dimensions of the given rectangle. This is a 
	 * convenience method. A similar effect can be achieved with
	 * the "erase" method.
	 */
	public void eraseRectangle(int xPos, int yPos, int width, int height)
	{
		erase(new Rectangle(xPos, yPos, width, height));
	}

	/**
	 * Erase a given shape's interior on the screen.
	 * @param  shape  the shape object to be erased 
	 */
	public void erase(Shape shape)
	{
		Color original = graphic.getColor();
		graphic.setColor(backgroundColor);
		graphic.fill(shape);			  // erase by filling background color
		graphic.setColor(original);
		canvas.repaint();
	}

	/**
	 * Erases a given shape's outline on the screen.
	 * @param  shape  the shape object to be erased 
	 */
	public void eraseOutline(Shape shape)
	{
		Color original = graphic.getColor();
		graphic.setColor(backgroundColor);
		graphic.draw(shape);  // erase by drawing background color
		graphic.setColor(original);
		canvas.repaint();
	}

	/**
	 * Draws an image onto the canvas.
	 * @param  image   the Image object to be displayed 
	 * @param  x	   x co-ordinate for Image placement 
	 * @param  y	   y co-ordinate for Image placement 
	 * @return  returns boolean value representing whether the image was 
	 *		  completely loaded 
	 */
	public boolean drawImage(Image image, int x, int y)
	{
		boolean result = graphic.drawImage(image, x, y, null);
		canvas.repaint();
		return result;
	}

	/**
	 * Draws a String on the Canvas.
	 * @param  text   the String to be displayed 
	 * @param  x	  x co-ordinate for text placement 
	 * @param  y	  y co-ordinate for text placement
	 */
	public void drawString(String text, int x, int y)
	{
		graphic.drawString(text, x, y);   
		canvas.repaint();
	}

	/**
	 * Erases a String on the Canvas.
	 * @param  text	 the String to be displayed 
	 * @param  x		x co-ordinate for text placement 
	 * @param  y		y co-ordinate for text placement
	 */
	public void eraseString(String text, int x, int y)
	{
		Color original = graphic.getColor();
		graphic.setColor(backgroundColor);
		graphic.drawString(text, x, y);   
		graphic.setColor(original);
		canvas.repaint();
	}

	/**
	 * Draws a line on the Canvas.
	 * @param  x1   x co-ordinate of start of line 
	 * @param  y1   y co-ordinate of start of line 
	 * @param  x2   x co-ordinate of end of line 
	 * @param  y2   y co-ordinate of end of line 
	 */
	public void drawLine(int x1, int y1, int x2, int y2)
	{
		graphic.drawLine(x1, y1, x2, y2);   
		canvas.repaint();
	}

	/**
	 * Sets the foreground color of the Canvas.
	 * @param  newColor   the new color for the foreground of the Canvas 
	 */
	public void setForegroundColor(Color newColor)
	{
		graphic.setColor(newColor);
	}

	/**
	 * Returns the current color of the foreground.
	 * @return   the color of the foreground of the Canvas 
	 */
	public Color getForegroundColor()
	{
		return graphic.getColor();
	}

	/**
	 * Sets the background color of the Canvas.
	 * @param  newColor   the new color for the background of the Canvas 
	 */
	public void setBackgroundColor(Color newColor)
	{
		backgroundColor = newColor;   
		graphic.setBackground(newColor);
	}

	/**
	 * Returns the current color of the background
	 * @return   the color of the background of the Canvas 
	 */
	public Color getBackgroundColor()
	{
		return backgroundColor;
	}

	/**
	 * changes the current Font used on the Canvas
	 * @param  newFont   new font to be used for String output
	 */
	public void setFont(Font newFont)
	{
		graphic.setFont(newFont);
	}

	/**
	 * Returns the current font of the canvas.
	 * @return	 the font currently in use
	 **/
	public Font getFont()
	{
		return graphic.getFont();
	}

	/**
	 * Sets the size of the canvas.
	 * @param  width	new width 
	 * @param  height   new height 
	 */
	public void setSize(int width, int height)
	{
		canvas.setPreferredSize(new Dimension(width, height));
		Image oldImage = canvasImage;
		canvasImage = canvas.createImage(width, height);
		graphic = (Graphics2D)canvasImage.getGraphics();
		graphic.drawImage(oldImage, 0, 0, null);
		frame.pack();
	}

	/**
	 * Returns the size of the canvas.
	 * @return	 The current dimension of the canvas
	 */
	public Dimension getSize()
	{
		return canvas.getSize();
	}

	/**
	 * Waits for a specified number of milliseconds before finishing.
	 * This provides an easy way to specify a small delay which can be
	 * used when producing animations.
	 * @param  milliseconds  the number 
	 */
	public void wait(int milliseconds)
	{
		try
		{
			Thread.sleep(milliseconds);
		} 
		catch (InterruptedException e)
		{
			// ignoring exception at the moment
		}
	}

	/************************************************************************
	 * Inner class CanvasPane - the actual canvas component contained in the
	 * Canvas frame. This is essentially a JPanel with added capability to
	 * refresh the image drawn on it.
	 */
	private class CanvasPane extends JPanel
	{
		public void paint(Graphics g)
		{
			g.drawImage(canvasImage, 0, 0, null);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 09:59 AM

That is IMPORTANT!!!!

That is not a java standard file and none of us have access to it. Never assume that we do. Now, my question still remains, what is BouncingBall and where have you defined it?

edit: You also declare the same iterator twice, only need to that once and then reassign it later.

This post has been edited by KYA: 10 December 2008 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#13 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 10:06 AM

Yea I tend not to think about important things like that... Bouncing ball is another class separate from the other three. I'll post its code as well. I didn't post all four because I didn't think it was needed, but I definitely should have posted the Canvas code. Sorry about that.

import java.awt.*;
import java.awt.geom.*;

/**
 * Class BouncingBall - a graphical ball that observes the effect of gravity. The ball
 * has the ability to move. Details of movement are determined by the ball itself. It
 * will fall downwards, accelerating with time due to the effect of gravity, and bounce
 * upward again when hitting the ground.
 *
 * This movement can be initiated by repeated calls to the "move" method.
 * 
 * @author Bruce Quig
 * @author Michael Kolling (mik)
 * @author David J. Barnes
 *
 * @version 2008.03.30
 */

public class BouncingBall
{
	private static final int GRAVITY = 3;  // effect of gravity

	private int ballDegradation = 2;
	private Ellipse2D.Double circle;
	private Color color;
	private int diameter;
	private int xPosition;
	private int yPosition;
	private final int groundPosition;	  // y position of ground
	private Canvas canvas;
	private int ySpeed = 1;				// initial downward speed

	/**
	 * Constructor for objects of class BouncingBall
	 *
	 * @param xPos  the horizontal coordinate of the ball
	 * @param yPos  the vertical coordinate of the ball
	 * @param ballDiameter  the diameter (in pixels) of the ball
	 * @param ballColor  the color of the ball
	 * @param groundPos  the position of the ground (where the wall will bounce)
	 * @param drawingCanvas  the canvas to draw this ball on
	 */
	public BouncingBall(int xPos, int yPos, int ballDiameter, Color ballColor,
						int groundPos, Canvas drawingCanvas)
	{
		xPosition = xPos;
		yPosition = yPos;
		color = ballColor;
		diameter = ballDiameter;
		groundPosition = groundPos;
		canvas = drawingCanvas;
	}

	/**
	 * Draw this ball at its current position onto the canvas.
	 **/
	public void draw()
	{
		canvas.setForegroundColor(color);
		canvas.fillCircle(xPosition, yPosition, diameter);
	}

	/**
	 * Erase this ball at its current position.
	 **/
	public void erase()
	{
		canvas.eraseCircle(xPosition, yPosition, diameter);
	}	

	/**
	 * Move this ball according to its position and speed and redraw.
	 **/
	public void move()
	{
		// remove from canvas at the current position
		erase();
			
		// compute new position
		ySpeed += GRAVITY;
		yPosition += ySpeed;
		xPosition +=2;

		// check if it has hit the ground
		if(yPosition >= (groundPosition - diameter) && ySpeed > 0) {
			yPosition = (int)(groundPosition - diameter);
			ySpeed = -ySpeed + ballDegradation; 
		}

		// draw again at new position
		draw();
	}	

	/**
	 * return the horizontal position of this ball
	 */
	public int getXPosition()
	{
		return xPosition;
	}

	/**
	 * return the vertical position of this ball
	 */
	public int getYPosition()
	{
		return yPosition;
	}
}




EDIT: The dependencies for the whole package:
BallDemo depends on BoxBall and BouncingBall
BoxBall depends on Canvas
BouncingBall depends on Canvas

This post has been edited by skim: 10 December 2008 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#14 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Cannot find class error.

Posted 10 December 2008 - 10:12 AM

If I can't compile and run your program, I cannot offer guidance, advice, etc... Include all code in the future please.

Your problem:

First you had the constructor as Boxball, you have it defined as BoxBall (note the capitalization issues).

Second your use of the constructor does not match what is in the class, You call int, int...etc...

but if you look in the class file, the parameter list starts with Color. They need to match or you need to overload.

//actual constructor
 public BoxBall(Color ballColor, int ballDiameter, int xPos_, int yPos_, Canvas myCanvas,
					int xSpeed, int ySpeed, Rectangle boundary)


//your call:
BoxBall ball= new BoxBall(x, y, xSpeed, ySpeed, 16, color, box, myCanvas);

//does not match!


Was This Post Helpful? 1
  • +
  • -

#15 skim   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Cannot find class error.

Posted 10 December 2008 - 10:45 AM

Thank you for the help. Sorry for the original lack of information. I really appreciate all the help, Thanks again.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2