package animation;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
//Class is used to create a jPanel animation of a ball in motion
public class Animation extends JFrame {
int dim = 50; //range [50, 400]
private double G = 9.81; //range [-20, 20]
private double time = 0;
private double yvel = 0*(400/dim); //range [-40, 40]
private double xvel = 0*(400/dim); //range [-40, 40]
private double ypos = 400 - 40*(400/dim); //range [0, 400]
private double xpos = 25*(400/dim); //range [0, 400]
private double ballh = 2*(400/dim); //range [1, 10]
private double ballw = 2*(400/dim); //range [1, 10]
private double cores = 1; //range [0, 1]
// ActionListener is used to perform time based calculations for the ball's position/velocity
java.awt.event.ActionListener Move = new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
time += 0.01;
int flag = 0;
//determine if a collsion has occurred
if (ypos >= (400 - ballh)) {
yvel = -Math.abs(yvel*cores);
ypos = 400 - ballh;
flag = 1;
}
else if (ypos <= ballh) {
yvel = Math.abs(yvel*cores);
ypos = ballh;
}
if (xpos >= (400 - ballw)) {
xvel = -Math.abs(xvel*cores);
xpos = 400 - ballw;
}
else if (xpos <= 0) {
xvel = Math.abs(xvel*cores);
xpos = 0;
}
//update values
ypos += ((yvel*0.01) + (0.5*G*0.001*(400/dim)));
if (flag == 0) {
yvel += (0.01*G)*(400/dim);
}
xpos += xvel*0.01;
repaint();
if (flag == 1) {
System.out.println("Collision has occurred.");
}
System.out.println(yvel);
System.out.println(ypos);
System.out.println();
}
};
//Timer used to call ActionListener
Timer timer = new Timer(10, Move);
//Constructor initializes JFrame and its properties
public Animation() {
super("Animation");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setBackground(Color.WHITE);
createBufferStrategy(2);
timer.start();
}
//Paint function draws the board to a buffer image, then copies over to the screen
public void paint(Graphics g) {
Graphics buffer;
Image bufferImage;
bufferImage = createImage(400,400);
buffer = bufferImage.getGraphics();
buffer.setColor(Color.red);
buffer.fillOval((int) xpos, (int) ypos, (int) ballh, (int) ballw);
g.drawImage(bufferImage, 0, 0, this);
}
//Function sets values for the animation
public void setValues(double gravity, double yvelocity, double xvelocity, double yposition, double xposition, double diameter, double restitution, int dimensions) {
if ((gravity <= 20 && gravity >= -20) && (yvelocity <= 40 && yvelocity >= -40) && (xvelocity <= 40 && xvelocity >= -40) && (yposition <= 400 && yposition >= 0) && (xposition <= 400 && xposition >= 0) && (diameter <= 10 && diameter >= 1) && (restitution <= 1 && restitution >= 0) && (dimensions <= 400 && dimensions >= 50)) {
G = gravity;
yvel = yvelocity*(400/dimensions);
xvel = xvelocity*(400/dimensions);
cores = restitution;
dim = dimensions;
ballh = diameter*(400/dimensions);
ballw = diameter*(400/dimensions);
if (yposition < ballh) {
ypos = ballh;
}
else if (yposition > 400 - ballh) {
ypos = 400 - ballh;
}
else {
ypos = yposition;
}
if (xposition > 400 - ballw) {
xpos = 400 - ballw;
}
else {
xpos = xposition;
}
}
else {
System.out.println("Values out of range, keeping previous values.");
}
}
//Function sets the default values depending on the lesson
public void setLessonDefaults (int lesson) {
switch (lesson) {
case 1: lesson = 1;
setValues(9.81, 0, 0, 100, 200, 2, 0, 50);
break;
case 2: lesson = 2;
setValues(9.81, 0, 0, 100, 200, 2, 1, 50);
break;
case 3: lesson = 3;
setValues(9.81, -10, 10, 384, 0, 2, 1, 50);
break;
default:
System.out.println("Invalid lesson.");
break;
}
}
public static void main(String[] args){
Animation A = new Animation();
A.setLessonDefaults(2);
}
}