001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.acmmm11.presentation.slides.tutorial;
031
032import java.awt.Dimension;
033import java.awt.GridBagLayout;
034import java.awt.Insets;
035import java.awt.image.BufferedImage;
036
037import javax.swing.BorderFactory;
038import javax.swing.JPanel;
039
040import org.openimaj.image.DisplayUtilities.ScalingImageComponent;
041import org.openimaj.image.ImageUtilities;
042import org.openimaj.image.MBFImage;
043import org.openimaj.video.Video;
044import org.openimaj.video.VideoDisplay;
045import org.openimaj.video.VideoDisplayListener;
046
047/**
048 * Panel for displaying a video as part of a larger slide
049 * 
050 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
051 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
052 *
053 */
054public abstract class TutorialPanel extends JPanel implements VideoDisplayListener<MBFImage>{
055        private static final long serialVersionUID = 2105054613577879944L;
056        
057        private MBFImage toDraw;
058        private BufferedImage bimg;
059        private ScalingImageComponent comp;
060        
061        /**
062         * Default constructor
063         * 
064         * @param name 
065         * @param capture
066         * @param width
067         * @param height
068         */
069        public TutorialPanel(String name, Video<MBFImage> capture, int width, int height) {
070                this.setOpaque( false );
071                
072                this.setBorder( BorderFactory.createTitledBorder( name ) );
073                
074                this.setLayout(new GridBagLayout());
075                
076                this.comp = new ScalingImageComponent();
077                this.add(comp);
078                
079                toDraw = new MBFImage(width,height,3);
080        }
081
082        @Override
083        public void afterUpdate(VideoDisplay<MBFImage> display) {
084                
085        }
086
087        @Override
088        public void beforeUpdate(MBFImage frame) {
089                if (this.comp.getWidth() <= 10) {
090                        Insets insets = this.getInsets();
091                        int width = this.getWidth() - insets.left - insets.right;
092                        int height = (int) (((float)width / (float)frame.getWidth())*frame.getHeight());
093                                                
094                        this.comp.setSize(width, height);
095                        this.comp.setPreferredSize(new Dimension(width,height));
096                        this.validate();
097                }
098                
099                toDraw.internalCopy(frame);
100                doTutorial(toDraw);
101                this.comp.setImage(bimg = ImageUtilities.createBufferedImageForDisplay( toDraw, bimg ));
102        }
103
104        /**
105         * Update the frame
106         * @param toDraw
107         */
108        public abstract void doTutorial(MBFImage toDraw);
109}