Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

David JonesK

Greenhorn
+ Follow
since Mar 11, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David JonesK

Thanks a lot, that was great
14 years ago
hi, does anyone know how to get a triangle to bounce around inside a frame, for example, it keeps moving till it reaches the border and the moves in another direction
any information on this would be helpful
thanks
14 years ago
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
}
}
}
}
14 years ago
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
}
}
}
}
14 years ago
hi, i'm really stuck, can anyone tell me how to generate random numbers between 100 and 200 in java.
thank you
14 years ago