-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulletAccelerating.java
81 lines (71 loc) · 1.9 KB
/
BulletAccelerating.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
81
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BulletAccelerating extends Bullet
{
private double dx;
private double dy;
private double factor;
private int imgWidth;
private int imgHeight;
private Image image;
public BulletAccelerating(int width, int height, double x, double y, double r, int power, Color color, double dx, double dy, double factor)
{
super(width, height, x, y, r, power, color);
this.dx = dx;
this.dy = dy;
this.factor = factor;
try
{
image = ImageIO.read(new File("yelloworb.png"));
}
catch (IOException ex)
{
}
imgWidth = image.getWidth(null);
imgHeight = image.getHeight(null);
}
public BulletAccelerating(int width, int height, double x, double y, double r, double speed, int power, Color color, double targetx, double targety, double factor)
{
super(width, height, x, y, r, power, color);
this.factor = factor;
double angle = -Math.atan2(targetx-x, targety-y)+Math.PI/2;
this.dx = speed*Math.cos(angle);
this.dy = speed*Math.sin(angle);
try
{
image = ImageIO.read(new File("yelloworb.png"));
}
catch (IOException ex)
{
}
imgWidth = image.getWidth(null);
imgHeight = image.getHeight(null);
}
public void move()
{
x += dx;
y += dy;
dx *= factor;
dy *= factor;
}
public void draw(Graphics g)
{
double angle = Math.PI - Math.atan2(dx, dy);
Graphics2D g2d = (Graphics2D)g;
AffineTransform trans = new AffineTransform();
trans.setToIdentity();
trans.translate(x, y);
trans.rotate( angle );
trans.scale(2*r/imgWidth,2*r/imgHeight);
trans.translate(-imgWidth/2, -imgHeight/2);
g2d.drawImage(image, trans, null);
//g.drawImage(image, (int) (x - imgWidth/2), (int) (y - imgHeight/2), imgWidth, imgHeight, null);
}
}