-
Notifications
You must be signed in to change notification settings - Fork 1
/
GMouseListener.java
68 lines (52 loc) · 1.29 KB
/
GMouseListener.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
/**
* @author mr.The
* @skype mr-the
* @twitter @mr_The
*/
package javalife;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.event.MouseInputAdapter;
/**
* Mouse Listener.
* Draw with mouse
*/
public class GMouseListener extends MouseInputAdapter implements MouseMotionListener {
int oldx = -1;
int oldy = -1;
JavaLife life;
int cell_size;
public void setCellSize(int cell_size) {
this.cell_size = cell_size;
}
public void setLife(JavaLife life) {
this.life = life;
}
@Override
public void mousePressed(MouseEvent e) {
updateSize(e);
}
@Override
public void mouseDragged(MouseEvent e) {
updateSize(e);
}
@Override
public void mouseReleased(MouseEvent e) {
oldx=-1;
oldy=-1;
}
void updateSize(MouseEvent e) {
int x=e.getX()/cell_size;
int y=e.getY()/cell_size;
if((x==oldx && y==oldy)) {
return;
}
oldx=x;
oldy=y;
if(life.data.getCell(x, y) == 1)
life.data.setCell(x, y, 0);
else
life.data.setCell(x, y, 1);
//System.out.println("Mouse pressed; x: " + x);
}
}