-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQPlatform.java
More file actions
96 lines (85 loc) · 2 KB
/
QPlatform.java
File metadata and controls
96 lines (85 loc) · 2 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
95
96
/**
* @author Edgar Quillion <edgarquill@gmail.com>
* @version Version 1
* @since 1.6
*/
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
/**
* Platform with bounding box
*/
public class QPlatform extends QBox
{
private BufferedImage image;
private Color color;
/**
* Constructor for the platform, originally contains no image,
* and color is set to black
*/
public QPlatform()
{
super();
image = null;
color = Color.BLACK;
}
/**
* Sets the color of the platform to whatever you want
* @param color what color the new platform will be
*/
public void setColor(Color color)
{
this.color = color;
}
/**
* Returns the color to which this platform is set to
* @return color of this platform
*/
public Color getColor()
{
return this.color;
}
/**
* Load the image from a given path,
* @return true if image loaded successfully, false otherwise
*/
public boolean loadImage(String path)
{
URL url = this.getClass().getResource(path);
this.image = null;
try
{
this.image = ImageIO.read(url);
}
catch (Exception e)
{
return false;
}
super.setWidth(image.getWidth());
super.setHeight(image.getHeight());
return true;
}
/**
* Returns this platform's image
* @return image of this platform
*/
public BufferedImage getImage()
{
return this.image;
}
/**
* Draws image for the platform to the specified canvas
*/
public void draw(Graphics2D g)
{
if(image == null)
{
g.setColor(color);
g.fillRect(super.getX(), super.getY(), super.getWidth(), super.getHeight());
}
else
g.drawImage(this.image, null, this.getX(), this.getY());
}
}