-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlower.java
More file actions
114 lines (102 loc) · 2.69 KB
/
Flower.java
File metadata and controls
114 lines (102 loc) · 2.69 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Point;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Flower extends JPanel {
Image img;//花的图片
BufferedImage bufImage; //用于显示的缓冲区图像
BufferedImage originalBufImage; //原始缓冲区图像
Graphics2D bufImageG; //缓冲区图像的图形环境
/**花的坐标*/
private int posX;
private int posY;
private int volumn;
/**花的生成时间*/
private int time;
/**花所在的面板*/
private JPanel pGround;
/**花的构造函数
* @param x,y 花中心的坐标
* @param volumn 花蜜容量
* @param img 花的图像
*/
public Flower(int time,int x, int y, int volumn,Image img,JPanel pGround){
this.pGround = pGround;
this.time = time;
this.posX = x;
this.posY = y;
this.img = img;
this.volumn = volumn;
setOpaque(false);//设置JPanel透明,只显示图片,不显示底板
//设置JPanel的大小,很重要,否则画布上看不到任何东西
setSize(64,64);
//设置JPanel在画布中的位置
setLocation(posX-32,posY-32);
originalBufImage = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB); //创建原始缓冲区图像
bufImage = originalBufImage;
bufImageG = bufImage.createGraphics(); //创建bufImage的图形环境
bufImageG.drawImage(img,(56-img.getWidth(null))/2,(56-img.getHeight(null))/2, this); //传输源图像数据到缓冲区图像中
}
/**重载容器的paintComponent()方法*/
public void paint(Graphics g) {
super.paintComponent(g);
//只有花蜜数量大于0,才绘制花
if (bufImage != null&&volumn>0) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(bufImage,3,3,this); //绘制图片
Color c = g2.getColor();
Font f = g2.getFont();
//g2.setColor(new Color(255,255,255));
//g2.drawRect(0,0,63,63);
g2.setColor(new Color(255,255,0));
g2.fillOval(28,28,8,8);
//g2.setFont(new Font("Arial",Font.PLAIN,10));
//g2.drawString(volumn+"("+posX+","+posY+")", 5, 12);
g2.setColor(c);
g2.setFont(f);
}
}
/**每次被蜜蜂消耗的蜂蜜,如果蜂蜜为0,花就不显示
* @param sub 蜜蜂一次采蜜的数量
* @return 是否可以再采蜜(true-还有蜜,false-最后一定采完了)
*/
public boolean consume(int sub){
volumn -= sub;
if(volumn<=0)
{
setVisible(false);
return false;
}
repaint();
return true;
}
/**获得剩余花蜜的容量
* @return 返回花蜜的量
*/
public int getVolumn(){
return volumn;
}
/**获得花的位置
* @return 返回花蜜的坐标
*/
public Point getPosition(){
return new Point(posX,posY);
}
/**获得花的生成时间
* @return 返回花的轮数
*/
public int getTime(){
return time;
}
/**获得面板
* @return 返回pGround
*/
public JPanel getPGround(){
return pGround;
}
}