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

New Topic/Question
Reply


MultiQuote




|