Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Bouncing shapes

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, that was great
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic