Made 2 frame gifs work (#1852)

* Improved animation player

* Cleanup

* Acquiesced to Sean's frivolous complaints

* More complaints

---------

Co-authored-by: plherrin <plherrin@dickinson.ndc.nasa.gov>
This commit is contained in:
Pherring04 2025-03-11 11:04:00 -05:00 committed by GitHub
parent fd6df3cf33
commit 9ae0b35110
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 1040 additions and 897 deletions

File diff suppressed because it is too large Load Diff

View File

@ -30,160 +30,214 @@ import trick.common.ui.UIUtils;
*
* @since Trick 10
*/
public class AnimationPlayer extends JPanel {
//========================================
// Public data
//========================================
//========================================
// Protected data
//========================================
//========================================
// Private Data
//========================================
private boolean paused;
private boolean finished;
private String animationFile;
private JLabel animationLabel;
private PlayAnimationTask animationTask;
private static final long serialVersionUID = 3705588596523798631L;
//========================================
// Constructors
//========================================
/**
* Default constructor.
public class AnimationPlayer extends JPanel
{
//========================================
// Public data
//========================================
//========================================
// Protected data
//========================================
//========================================
// Private Data
//========================================
private boolean paused;
private boolean finished;
private String animationFile;
private int animationTimeStep;
private JLabel animationLabel;
private PlayAnimationTask animationTask;
private static final long serialVersionUID = 3705588596523798631L;
//========================================
// Constructors
//========================================
/**
* Default constructor.
* @param fileName name of file
*/
public AnimationPlayer(String fileName) {
animationFile = fileName;
buildGUI();
if (animationTask == null) {
animationTask = new PlayAnimationTask();
}
}
//========================================
// Set/Get methods
//========================================
//========================================
// Methods
//========================================
/**
* Starts the animation task. This method has to be called
* in order for the animation to be played.
*/
public void start() {
if (animationTask == null) {
animationTask = new PlayAnimationTask();
}
animationTask.execute();
}
/**
* Pauses the animation play.
*/
public void pause() {
paused = true;
}
/**
* Resumes the animation play.
*/
public void resume() {
paused = false;
}
/**
* Stops the animation play.
*/
public void stop() {
finished = true;
}
/**
* Builds the player GUI.
*/
private void buildGUI() {
*/
public AnimationPlayer(String fileName)
{
animationFile = fileName;
animationTimeStep = 150;
buildGUI();
if (animationTask == null)
{
animationTask = new PlayAnimationTask();
}
}
/**
* Constructor with paramaterized time step.
* @param fileName name of file
* @param timeStep animation time step
*/
public AnimationPlayer(String fileName, int timeStep)
{
animationFile = fileName;
animationTimeStep = timeStep;
buildGUI();
if (animationTask == null)
{
animationTask = new PlayAnimationTask();
}
}
//========================================
// Set/Get methods
//========================================
//========================================
// Methods
//========================================
/**
* Starts the animation task. This method has to be called
* in order for the animation to be played.
*/
public void start()
{
if (animationTask == null)
{
animationTask = new PlayAnimationTask();
}
animationTask.execute();
}
/**
* Pauses the animation play.
*/
public void pause()
{
paused = true;
}
/**
* Resumes the animation play.
*/
public void resume()
{
paused = false;
}
/**
* Stops the animation play.
*/
public void stop()
{
finished = true;
}
/**
* Builds the player GUI.
*/
private void buildGUI()
{
setLayout(new BorderLayout());
animationLabel = new JLabel();
ImageIcon icon = UIUtils.createImageIcon(animationFile);
// set proper initial size for the label
if (icon != null) {
animationLabel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
if (icon != null)
{
animationLabel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
}
add(animationLabel, BorderLayout.CENTER);
}
//========================================
// Inner Class
//========================================
/**
//========================================
// Inner Class
//========================================
/**
* Inner class for playing an animation image.
*/
private class PlayAnimationTask extends SwingWorker<Void, Void> {
ImageInputStream stream;
@Override
public Void doInBackground() {
if (animationFile == null) {
return null;
}
try {
InputStream input = UIUtils.getInputStreamForFile(animationFile);
stream = ImageIO.createImageInputStream(input);
Iterator readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext()) {
throw new RuntimeException("no image reader found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(stream); // don't omit this line!
int n = reader.getNumImages(true); // don't use false!
for (int i = 0; i < n; i++) {
BufferedImage image = reader.read(i);
Image img = image;
animationLabel.setIcon(new ImageIcon(img));
do {
try {
Thread.sleep(150);
} catch (InterruptedException ie) {
}
} while (paused);
if (finished) {
break;
} else {
// rewind
if (i == n-1) {
i = 0;
}
}
}
} catch (IOException ioe) {
private class PlayAnimationTask extends SwingWorker<Void, Void>
{
ImageInputStream stream;
@Override
public Void doInBackground()
{
if (animationFile == null)
{
return null;
}
return null;
}
try
{
InputStream input = UIUtils.getInputStreamForFile(animationFile);
stream = ImageIO.createImageInputStream(input);
Iterator readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext())
{
throw new RuntimeException("no image reader found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(stream); // don't omit this line!
int n = reader.getNumImages(true); // don't use false!
int i = 0;
while(i < n)
{
Image img = reader.read(i);
animationLabel.setIcon(new ImageIcon(img));
do
{
try
{
Thread.sleep(animationTimeStep);
}
catch (InterruptedException ie)
{
}
} while (paused);
if (finished)
{
break;
}
else
{
// rewind
if (i >= n-1)
{
i = 0;
}
else
{
++i;
}
}
}
}
catch (IOException ioe)
{
}
@Override
public void done() {
if (stream != null) {
try {
return null;
}
@Override
public void done()
{
if (stream != null)
{
try
{
stream.close();
} catch (IOException ioe) { }
}
catch (IOException ioe)
{
}
}
}
}
}
}

View File

@ -927,7 +927,19 @@ public class SimControlApplication extends TrickApplication implements PropertyC
trickLogoName = UIUtils.getTrickLogo();
}
logoImagePanel = new AnimationPlayer(trickLogoName);
if (UIUtils.getTrickLogoStep() != null) {
try {
int timeStep = Integer.parseInt(UIUtils.getTrickLogoStep());
logoImagePanel = new AnimationPlayer(trickLogoName, timeStep);
}
catch (NumberFormatException e) {
logoImagePanel = new AnimationPlayer(trickLogoName);
}
}
else {
logoImagePanel = new AnimationPlayer(trickLogoName);
}
logoImagePanel.setToolTipText("Trick Version " + UIUtils.getTrickVersion());
JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, litePanel, logoImagePanel);