-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQMCharacter.java
More file actions
93 lines (82 loc) · 1.99 KB
/
QMCharacter.java
File metadata and controls
93 lines (82 loc) · 1.99 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
/**
* @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;
public class QMCharacter extends QMControls
{
private BufferedImage image;
private Color color;
/**
* Constructor for the character, originally contains no image,
* and color is set to black
*/
public QMCharacter()
{
super();
image = null;
color = Color.BLACK;
}
/**
* Sets the color of the character to whatever you want
* @param color what color the new character will be
*/
public void setColor(Color color)
{
this.color = color;
}
/**
* Returns the color to which this character is set to
* @return color of this character
*/
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 character's image
* @return image of this character
*/
public BufferedImage getImage()
{
return this.image;
}
/**
* Draws image for the character 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());
}
}