It is a colorful, eye-catching ball that bounces off the walls of the applet.
[insert single ball video here]
Even though the fundamental program might be boring, it enjoyed the experience of constantly adding features and innovations to make it more complex.
By clicking my mouse on any part of the applet, the ball will change directions and travel towards the point that the mouse was clicked. Since I could only change the ball’s pixel position using whole numbers, it was difficult to change the slope of travel in such a way that it wouldn’t drastically change the speed of the ball. For example, if I click almost vertically above the ball, the slope between the ball’s pixel position and the mouse click’s pixel position will be 220/5. You wouldn’t be able to see the ball if it moved that fast. So, I used the following code to round the ball’s slope of displacement to an improper fraction that wouldn’t move the ball more than 5 pixels in the x or y direction every 20 milliseconds.
//changex is pixel distance from the ball to the mouse click (x direction)
//changey is pixel distance from the ball to the mouse click (y direction)
double maxDifference=5;
double difference=0;
int newchangex, newchangey;
for (int ting1 = 1; ting1<5; ting1++){
for (int ting2 = 1; ting2difference){
difference = Math.abs((Math.abs((double)(changey)/changex))-((double)(ting1)/ting2));
if (maxDifference>difference){
//this if statement will constantly replace the old newchangex &newchangey
//values with the current one because these new values
//create a slope that is closer to the changey/changex slope
//than the old values.
maxDifference = difference;
newchangex = ting2;
newchangey = ting1;
}
}
}
//newchangex and newchangey is going to represent the new slope of travel for the ball
After this, I would just change the signs of the newchangex and newchangey and it’ll work.
I also added balls that would mirror the movement of the initial ball. The ball also changed rgb values at a constant rate.
[Insert multiple ball video here]