-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jet.java
80 lines (60 loc) · 1.89 KB
/
Jet.java
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
//Justin Baldeosingh
//816021226
//COMP 3609 - Assignment 2
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Random;
public class Jet {
//Sets the drawing panel to the global panel variable.
private JPanel panel;
//Declares the coordinate variables of the jet.
private int x;
private int y;
//Declares the width and height dimensions of the jet.
private int width;
private int height;
private int dx; // increment to move along x-axis
private int dy; // increment to move along y-axis
//Declares the images that will be used for the jet orientations.
private Image jetImage;
private Image jetImageLeft;
private Image jetImageRight;
public Jet(JPanel p, int xPos, int yPos) {
//Loads the main panel, to be used as the drawing context.
panel = p;
//Sets the dimensions of the jet.
width = 70;
height = 70;
//Sets the initial x and y coordinates of the jet.
x = xPos;
y = yPos;
//Jet moves across the screen only.
dx = 25;
dy = 0;
//Loads the Jet image sprites.
jetImageLeft = ImageManager.loadImage ("images/jet-l.png");
jetImageRight = ImageManager.loadImage ("images/jet-r.png");
//Sets the initial jet orientation.
jetImage = jetImageRight;
}
public void draw (Graphics2D g2) {
g2.drawImage(jetImage, x, y, width, height, null);
}
//Moves the jet
public void move() {
if (!panel.isVisible ()) return;
//Updates the x and y coordinates of the jet as the jet moves.
x = x + dx;
if(x > panel.getWidth() - width){
dx = -dx;
jetImage = jetImageLeft;
} else if(x < 0){
dx = -dx;
jetImage = jetImageRight;
}
}
public int getJetX(){
return x;
}
}