-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotateFX.java
More file actions
87 lines (52 loc) · 2.06 KB
/
RotateFX.java
File metadata and controls
87 lines (52 loc) · 2.06 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
import java.util.Random;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.lang.Math;
public class RotateFX implements ImageFX {
private static final int WIDTH = 40; // width of the image
private static final int HEIGHT = 40; // height of the image
private GamePanel panel;
private int x;
private int y;
private BufferedImage spriteImage; // image for sprite effect
Graphics2D g2;
float angle, angleChange; // angle controls the amount of rotation
public RotateFX (GamePanel p) {
panel = p;
Random random = new Random();
x = random.nextInt (panel.getWidth() - WIDTH);
y = random.nextInt (panel.getHeight() - 150 - HEIGHT);
angle = 5; // set to 10 degrees
angleChange = 5; // change to angle each time in update()
spriteImage = ImageManager.loadBufferedImage("images/Star.png");
}
public void draw (Graphics2D g2) {
int width, height;
width = spriteImage.getWidth(); // find width of image
height = spriteImage.getHeight(); // find height of image
BufferedImage dest = new BufferedImage (width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dest.createGraphics();
AffineTransform origAT = g2d.getTransform();
// save original transform
// rotate the coordinate system of the destination image around its center
AffineTransform rotation = new AffineTransform();
rotation.rotate(Math.toRadians(angle*-1), width/2, height/2);
g2d.transform(rotation);
g2d.drawImage(spriteImage, 0, 0, null); // copy in the image
g2d.setTransform(origAT); // restore original transform
g2.drawImage(dest, x, y, WIDTH, HEIGHT, null);
g2d.dispose();
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, WIDTH, HEIGHT);
}
public void update() { // modify angle of rotation
angle = angle + angleChange;
if (angle >= 360) // reset to 10 degrees if 360 degrees reached
angle = 10;
}
}