Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Bouncing shapes

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I really hope someone can help me with this, i'm trying to create a program which draws a triangle and then the triangle bounces around within a box, for example the shape keeps moving up till it hits the top of the frame then moves in another direction.
I have a program which bounces a ball but I can't change it to bounce any other shape, please can someone help me with this!
Here is the ball program:
import java.awt.*;
import java.applet.Applet;
public class OriginalBall extends Applet
{
/*
Written November 2001
Illustrates simple animation of a grapics object and uses
Thread.sleep () for producing a simple time delay
*/
private int x = 52, xChange = 5; // initial positions plus movement increment
private int y = 12, yChange = 5;
private int diameter = 10; // diameter of ball
private int rectLeftX = 50, rectRightX = 250; // board border coordinates
private int rectTopY = 10, rectBottomY = 180;
public void paint (Graphics g)
{
// draw a board for the game
g.drawRect (rectLeftX, rectTopY, rectRightX - rectLeftX, rectBottomY - rectTopY);
g.setColor (Color.lightGray);
g.fillRect ((rectLeftX+1), (rectTopY+1), (rectRightX-rectLeftX-1), (rectBottomY-rectTopY-1));
for (int n = 1; n < 120; n++) // set up game period loop
{
g.setColor (Color.lightGray); // make ball invisible at original position
g.fillOval(x, y, diameter, diameter);
// check for boundaries and reverse direction if reached
if (x + xChange <= rectLeftX) // left border reached
xChange = -xChange;
else if (x + xChange + diameter >= rectRightX) // right border reached
xChange = -xChange;
else if (y + yChange <= rectTopY) // top border reached
yChange = -yChange;
else if (y + yChange + diameter >= rectBottomY) // bottom border reached
yChange = -yChange;
x = x + xChange; // move ball
y = y + yChange;
g.setColor(Color.red); // make ball visible at new position
g.fillOval (x, y, diameter, diameter);
try
{
Thread.sleep (50); // delay for n ticks
}
catch (InterruptedException e) // must have this exception handler
{
// do nothing
}
}
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ March 11, 2004: Message edited by: Craig Wood ]
[ March 11, 2004: Message edited by: Craig Wood ]
 
David JonesK
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, that was great
 
money grubbing section goes here:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic