001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019
020package com.avpkit.core.demos;
021
022import java.awt.Dimension;
023import java.awt.Graphics;
024import java.awt.Image;
025import java.awt.image.BufferedImage;
026
027import javax.swing.JComponent;
028import javax.swing.JFrame;
029import javax.swing.SwingUtilities;
030
031/**
032 * This class just displays a 2d graphic on a Swing window.  It's
033 * only here so the video playback demos look simpler.  Please don't
034 * reuse this component; why?  Because I know next to nothing
035 * about Swing, and this is probably busted.
036 * <p>
037 * Of note though, is this class has NO XUGGLER dependencies.
038 * </p>
039 * @author aclarke
040 *
041 */
042public class VideoImage extends JFrame
043{
044
045  /**
046   * To avoid a warning... 
047   */
048
049  private static final long serialVersionUID = -4752966848100689153L;
050  private final ImageComponent mOnscreenPicture;
051
052
053  /**
054   * Create the frame
055   */
056
057  public VideoImage()
058  {
059    super();
060    mOnscreenPicture = new ImageComponent();
061    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
062    getContentPane().add(mOnscreenPicture);
063    this.setVisible(true);
064    this.pack();
065  }
066  
067  public void setImage(final BufferedImage aImage)
068  {
069    mOnscreenPicture.setImage(aImage);
070  }
071
072  public class ImageComponent extends JComponent
073  {
074    /**
075     * yeah... good idea to add this.
076     */
077    private static final long serialVersionUID = 5584422798735147930L;
078    private Image mImage;
079    private Dimension mSize;
080
081    public void setImage(Image image)
082    {
083      SwingUtilities.invokeLater(new ImageRunnable(image));
084    }
085    
086    public void setImageSize(Dimension newSize)
087    {
088    }
089    
090    private class ImageRunnable implements Runnable
091    {
092      private final Image newImage;
093
094      public ImageRunnable(Image newImage)
095      {
096        super();
097        this.newImage = newImage;
098      }
099  
100      public void run()
101      {
102        ImageComponent.this.mImage = newImage;
103        final Dimension newSize = new Dimension(mImage.getWidth(null), 
104          mImage.getHeight(null));
105        if (!newSize.equals(mSize))
106        {
107          ImageComponent.this.mSize = newSize;
108          VideoImage.this.setSize(mImage.getWidth(null), mImage.getHeight(null));
109          VideoImage.this.setVisible(true);
110        }
111        repaint();
112      }
113    }
114    
115    public ImageComponent()
116    {
117      mSize = new Dimension(0, 0);
118      setSize(mSize);
119    }
120
121    public synchronized void paint(Graphics g)
122    {
123      if (mImage != null)
124        g.drawImage(mImage, 0, 0, this);
125    }
126  }
127}