Question:
Java - how to make the balls bounce within a rectangle.?
sammyboywonder
2010-01-27 10:17:25 UTC
I'm trying to modify a java source file that simulates balls bouncing on the screen. A line is drawn near the bottom and acts as the ground and the balls bounce off this line. How do I go about making the balls bounce inside the rectangle? I've included the code below, I've been trying it for ages with no luck.

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

/**
* Class BallDemo - provides two short demonstrations showing how to use the
* Canvas class.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*/

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.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 two bouncing balls
*/
public void bounce()
{
int ground = 400; // position of the ground line

myCanvas.setVisible(true);

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

//draw rectangle
//myCanvas.fillRectangle(50, 50, 50, 50);

// crate and show the balls
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();

// make them bounce
boolean finished = false;
while(!finished) {
myCanvas.wait(50); // small delay
ball.move();
ball2.move();
// stop once ball has travelled a certain distance on x axis
if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
finished = true;
}
}
ball.erase();
ball2.erase();
}
}
Three answers:
mule
2010-01-27 10:31:01 UTC
Well you're definitely missing a few things.



1) What is this "bouncingball" - it's not defined anywhere. Did you have to make this yourself? It would be beneficial if that was provided.



2) You're using various methods on your Canvas myCanvas object that aren't applicable. For instance, setForegroundColor(Color) should be setForeground(Color), amongst other misplaced methods.



3) Is this able to compile? If so, that's very interesting... If not, it would be very benefiical to see what the result of the javac was (what errors occured.)



Basically, this looks like it's on the right track, and just needs some work.



If you provide more info, I can probably help further.



cheers



mule
?
2016-12-19 00:04:49 UTC
that's a observed as a dull ball if it reaches a batsman after a bouncing two times. yet while it stops shifting formerly the participant, he's invited to hit it. And consequently score runs. yet 2 participant can not have runners at a same time. in easy terms you are able to have a runner.
anonymous
2010-01-27 10:37:20 UTC
You need to check the ball class for a member function that sets boundaries. Often there is one, if not do you have the code for the bouncing ball class.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...