-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFruit.java
More file actions
94 lines (74 loc) · 2.26 KB
/
Copy pathFruit.java
File metadata and controls
94 lines (74 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* An abstract representation of a fruit to be tossed and sliced.
*
* @author Grant Visker, John Hurley, Joseph Capper, and Logan Belak.
* @version 5/7/2021
*/
public abstract class Fruit extends Thread
{
// delay time between frames of animation (ms)
public static final int DELAY_TIME = 33;
// what to add to ySpeed to simulate gravity?
public static final double GRAVITY = 0.25;
protected double xSpeed, ySpeed;
protected boolean done;
protected JComponent container;
protected int size;
protected double upperLeftX, upperLeftY;
protected boolean sliced;
/**
Construct a new Fruit object.
@param xSpeed initial x speed, pixels per second
@param ySpeed initial y speed, pixels per second
@param container the Swing component in which this fruit is being
drawn to allow it to call that component's repaint method
*/
public Fruit(double xSpeed, double ySpeed,
JComponent container, int size, double upperLeftX) {
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.container = container;
this.upperLeftX = upperLeftX;
upperLeftY = container.getHeight();
this.size = size;
sliced = false;
}
/**
Draw the fruit at its current location.
@param g the Graphics object on which the fruit should be drawn
*/
public abstract void paint(Graphics g);
/**
This object's run method, which manages the life of the fruit as it
is tossed on the screen.
*/
@Override
public void run() {
while (!done) {
try {
sleep(DELAY_TIME);
}
catch (InterruptedException e) {
}
// every iteration, update the coordinates
// by a pixel
upperLeftX += xSpeed;
upperLeftY += ySpeed;
// gravity factor also
ySpeed += GRAVITY;
container.repaint();
}
}
/**
Return whether the fruit has completed its fall to the bottom.
@return whether the fruit has completed its fall to the bottom
*/
public boolean done() {
return done;
}
}