File size: 1,425 Bytes
e49b994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JButton;

/**
 * ��ԪͼƬ
 */
public class Cell extends JButton {
    //private static final long serialVersionUID = 718623114650657819L;
    public static final int IMAGEWIDTH = 117;// ͼƬ����
    private int place;// ͼƬλ��
    
    public Cell(Icon icon, int place) {
        this.setSize(IMAGEWIDTH, IMAGEWIDTH);// ��ԪͼƬ�Ĵ�С
        this.setIcon(icon);// ��ԪͼƬ��ͼ��
        this.place = place;// ��ԪͼƬ��λ��
    }
    
    public void move(Direction dir) {// �ƶ���ԪͼƬ�ķ���
        Rectangle rec = this.getBounds();// ��ȡͼƬ��Rectangle����
        switch (dir) {// �жϷ���
            case UP:// �����ƶ�
                this.setLocation(rec.x, rec.y - IMAGEWIDTH);
                break;
            case DOWN:// �����ƶ�
                this.setLocation(rec.x, rec.y + IMAGEWIDTH);
                break;
            case LEFT:// �����ƶ�
                this.setLocation(rec.x - IMAGEWIDTH, rec.y);
                break;
            case RIGHT:// �����ƶ�
                this.setLocation(rec.x + IMAGEWIDTH, rec.y);
                break;
        }
    }
    
    public int getX() {
        return this.getBounds().x;// ��ȡ��ԪͼƬ��x����
    }
    
    public int getY() {
        return this.getBounds().y;// ��ȡ��ԪͼƬ��y����
    }
    
    public int getPlace() {
        return place;// ��ȡ��ԪͼƬ��λ��
    }
}