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

View File

@ -927,7 +927,19 @@ public class SimControlApplication extends TrickApplication implements PropertyC
trickLogoName = UIUtils.getTrickLogo(); 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()); logoImagePanel.setToolTipText("Trick Version " + UIUtils.getTrickVersion());
JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, litePanel, logoImagePanel); JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, litePanel, logoImagePanel);