• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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 frame, 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
}
}
}
}
 
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic