Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0449c96

Browse files
committed
SortCanvas, VisualSortArray, LaunchFrame: Changed logic so that SortCanvas has its own thread to perodically repaint itself, instead of VisualSortArray driving the repainting.
1 parent 071a97e commit 0449c96

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

‎src/io/nayuki/sortalgodemo/visual/LaunchFrame.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ public void actionPerformed(ActionEvent ev) {
212212
return;
213213

214214
// Initialize objects and worker thread
215-
final var array = new VisualSortArray(size, scale, speed);
215+
final var array = new VisualSortArray(size, speed);
216216
setInitialOrder(array, initialOrder.getSelectedIndex());
217217
array.finishInitialization();
218218
final SortAlgorithm algorithm = algorithms.get(algorithmInput.getSelectedIndex());
219+
SortCanvas canvas = new SortCanvas(array, scale, 60);
219220
final int startDelayMs = 1000;
220221
new Thread() {
221222
public Thread thread = this;
@@ -228,7 +229,7 @@ public void run() {
228229
private void initFrame() {
229230
// Do component layout
230231
final var sortFrame = new Frame(algorithm.getName());
231-
sortFrame.add(array.canvas);
232+
sortFrame.add(canvas);
232233
sortFrame.setResizable(false);
233234
sortFrame.pack();
234235

@@ -246,7 +247,6 @@ public void windowClosing(WindowEvent e) {
246247
(rect.width - sortFrame.getWidth()) / 8,
247248
(rect.height - sortFrame.getHeight()) / 8);
248249
sortFrame.setVisible(true);
249-
array.canvas.repaint();
250250
}
251251

252252
private void doSort() {
@@ -256,6 +256,7 @@ private void doSort() {
256256
algorithm.sort(array);
257257
array.setRange(0, array.length(), SortArray.ElementState.DONE);
258258
} catch (StopException|InterruptedException e) {
259+
canvas.stop();
259260
return;
260261
}
261262

‎src/io/nayuki/sortalgodemo/visual/SortCanvas.java‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,35 @@ final class SortCanvas extends Canvas {
4040
private final int scale;
4141
private BufferedImage buffer;
4242
private Graphics bufGfx;
43-
publicvolatileintsynchronizer;
43+
privateThreadworker;
4444

4545

4646

4747
/*---- Constructor ----*/
4848

49-
public SortCanvas(VisualSortArray array, int scale) {
49+
public SortCanvas(VisualSortArray array, int scale, doubleframeRateHz) {
5050
this.array = Objects.requireNonNull(array);
5151
if (scale <= 0)
5252
throw new IllegalArgumentException();
5353
this.scale = scale;
54+
if (frameRateHz <= 0)
55+
throw new IllegalArgumentException();
5456

5557
int size = Math.multiplyExact(array.length(), scale);
5658
buffer = new BufferedImage(size, size, BufferedImage.TYPE_INT_BGR);
5759
bufGfx = buffer.getGraphics();
5860
this.setSize(size, size);
61+
62+
worker = new Thread(() -> {
63+
try {
64+
while (!array.isDone()) {
65+
SortCanvas.this.repaint();
66+
Thread.sleep((int)(1000 / frameRateHz));
67+
}
68+
SortCanvas.this.repaint();
69+
} catch (InterruptedException e) {}
70+
});
71+
worker.start();
5972
}
6073

6174

@@ -80,11 +93,15 @@ public void update(Graphics g) {
8093

8194
// Called by the AWT event loop, not by user code.
8295
public void paint(Graphics g) {
83-
synchronizer = synchronizer;
8496
g.drawImage(buffer, 0, 0, this);
8597
}
8698

8799

100+
public void stop() {
101+
worker.interrupt();
102+
}
103+
104+
88105

89106
/*---- Color constants ----*/
90107

‎src/io/nayuki/sortalgodemo/visual/VisualSortArray.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ final class VisualSortArray extends AbstractSortArray {
4141
// Visual state per element: 0=active, 1=inactive, 2=comparing, 3=done
4242
private int[] state;
4343

44-
// Graphics
45-
public final SortCanvas canvas;
44+
private volatile boolean isDone;
4645

4746
// Statistics
4847
private volatile long comparisonCount;
@@ -58,10 +57,10 @@ final class VisualSortArray extends AbstractSortArray {
5857

5958
/*---- Constructors ----*/
6059

61-
public VisualSortArray(int size, intscale, double speed) {
60+
public VisualSortArray(int size, double speed) {
6261
// Check arguments and initialize arrays
6362
super(size);
64-
if (scale <= 0 || speed <= 0 || Double.isInfinite(speed) || Double.isNaN(speed))
63+
if (speed <= 0 || Double.isInfinite(speed) || Double.isNaN(speed))
6564
throw new IllegalArgumentException();
6665
state = new int[size];
6766

@@ -71,17 +70,14 @@ public VisualSortArray(int size, int scale, double speed) {
7170
stepsPerFrame = speed / targetFrameRate;
7271
remainingStepsAllowed = 0;
7372
nextRepaintTime = System.nanoTime();
74-
75-
// Initialize graphics
76-
canvas = new SortCanvas(this, scale);
7773
}
7874

7975

8076
public void finishInitialization() {
8177
if (isInitialized)
8278
throw new IllegalStateException();
8379
isInitialized = true;
84-
canvas.repaint();
80+
isDone = false;
8581
}
8682

8783

@@ -161,7 +157,12 @@ public void assertSorted() {
161157
if (values[i - 1] > values[i])
162158
throw new AssertionError();
163159
}
164-
canvas.repaint();
160+
isDone = true;
161+
}
162+
163+
164+
public boolean isDone() {
165+
return isDone;
165166
}
166167

167168

@@ -181,11 +182,8 @@ private void beforeStep() {
181182
throw new StopException();
182183
}
183184
}
184-
if (first) {
185-
canvas.synchronizer = canvas.synchronizer;
186-
canvas.repaint();
185+
if (first)
187186
first = false;
188-
}
189187
nextRepaintTime += Math.round(1e9 / targetFrameRate);
190188
if (nextRepaintTime <= currentTime)
191189
nextRepaintTime = currentTime + Math.round(1e9 / targetFrameRate);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /