-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainThread.java
63 lines (55 loc) · 1.56 KB
/
MainThread.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
package uoft.csc207.fishtank;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
/** Hacky way to manage threading and updates. */
public class MainThread extends Thread {
/** Where the fish tank items are drawn. */
private FishTankView fishTankView;
/** The canvas container. */
private SurfaceHolder surfaceHolder;
/** Whether the thread is running. */
private boolean isRunning;
/** The canvas on which to draw the fish tank. */
public static Canvas canvas;
/**
* Construct the thread.
*
* @param surfaceHolder the canvas container.
* @param view where the fish tank items are drawn.
*/
public MainThread(SurfaceHolder surfaceHolder, FishTankView view) {
this.surfaceHolder = surfaceHolder;
this.fishTankView = view;
}
@Override
public void run() {
while (isRunning) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.fishTankView.update();
this.fishTankView.draw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
try {
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
this.sleep(300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
}