Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
cfb67a76cThis file is pinned to the selected release. Download the original to open it in your design tool.
package com.marginallyclever.makelangelo.makeart.imageconverter;
import com.marginallyclever.makelangelo.turtle.Turtle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
public class ImageConverterThread extends SwingWorker<Turtle, Void> {
private static final Logger logger = LoggerFactory.getLogger(ImageConverterThread.class);
private ImageConverterIterative myConverter;
private String name;
private boolean paused=false;
private boolean enough=false;
private int iterations=0;
public ImageConverterThread(ImageConverterIterative converter) {
super();
this.myConverter = converter;
this.name = converter.getName();
myConverter.setThread(this);
setPaused(false);
}
public void setPaused(boolean paused) {
logger.debug("{} thread {}", paused ? "pausing" : "resuming", name);
this.paused = paused;
}
public boolean getPaused() {
return paused;
}
/**
* Will cause the thread to end gracefully at the start of the next iteration.
*/
public void endThreadGracefully() {
logger.debug("endThreadGracefully()");
this.enough = true;
}
@Override
protected Turtle doInBackground() throws Exception {
logger.debug("doInBackground() start {}", name);
Turtle turtle = new Turtle();
iterations=0;
int pauseCount=-1;
while(!enough && !isCancelled()) {
if(!paused) {
if(pauseCount==iterations) {
myConverter.resume();
}
myConverter.iterate();
iterations++;
} else {
if(pauseCount!=iterations) {
pauseCount=iterations;
myConverter.generateOutput();
}
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {}
}
logger.debug("doInBackground() ending {} after {} loop(s).", name, iterations);
return turtle;
}
@Override
public void done() {
logger.debug("{} thread {}", isCancelled() ? "cancelled" : "finished", name);
}
}