forked from visan/animi
/
Visualizer.java
140 lines (114 loc) · 3.09 KB
/
Visualizer.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2012 The Animo Project
* http://animotron.org
*
* This file is part of Animi.
*
* Animotron is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Animotron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of
* the GNU Affero General Public License along with Animotron.
* If not, see <http://www.gnu.org/licenses/>.
*/
package org.animotron.animi.gui;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import org.animotron.animi.Imageable;
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
public class Visualizer extends JInternalFrame {
private static final long serialVersionUID = -592610047528698167L;
private final Repainter repainter;
private ImageCanvas canvas = new ImageCanvas();
private BufferedImage image = null;
public Visualizer(Imageable simulator, String imageID) {
super(simulator.getImageName(imageID),
true, //resizable
true, //closable
false, //maximizable
true);//iconifiable
setLocation(100, 100);
BufferedImage img = simulator.getImage(imageID);
setSize(img.getWidth()+10, img.getHeight()+10);
// setOpaque(true);
// setDoubleBuffered(true);
getContentPane().add(canvas);
repainter = new Repainter(canvas, simulator, imageID);
repainter.start();
}
public Visualizer(Imageable simulator) {
this(simulator, null);
}
private volatile boolean paused = false;
/**
* Pause rendering.
*/
public void pause() {
if (paused) {
return;
}
paused = true;
System.out.println("paused");
}
/**
* Resume rendering.
*/
public void resume() {
if (!paused) {
return;
}
synchronized (repainter) {
repainter.notifyAll();
}
paused = false;
System.out.println("resumed");
}
private class ImageCanvas extends JComponent {
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
private class Repainter extends Thread {
int frequency = 2;
Imageable simulator;
String imageID;
JComponent component;
public Repainter(JComponent comp, Imageable sim, String imageId) {
component = comp;
simulator = sim;
imageID = imageId;
setDaemon(true);
}
@Override
public void run() {
//super.run();
while (simulator != null) {
try {
if (paused) {
synchronized (this) {
this.wait();
}
}
if (simulator != null) {
image = simulator.getImage(imageID);
component.repaint();
}
Thread.sleep(1000 / frequency);
} catch (Throwable e) {
}
}
}
}
}