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 */
030/**
031 * 
032 */
033package org.openimaj.demos.campusview;
034
035import java.awt.Color;
036import java.awt.Font;
037import java.awt.GridBagConstraints;
038import java.awt.GridBagLayout;
039
040import javax.swing.BorderFactory;
041import javax.swing.JLabel;
042import javax.swing.JPanel;
043
044import org.openimaj.hardware.compass.CompassSerialReader;
045
046/**
047 *  @author David Dupplaw (dpd@ecs.soton.ac.uk)
048 *      
049 *      @created 13 Jul 2011
050 */
051public class CompassComponent extends JPanel
052{
053        /** */
054    private static final long serialVersionUID = 1L;
055
056    private CompassSerialReader compass = new CompassSerialReader( "/dev/cu.KeySerial1" );
057    
058    private JLabel compassLabel = null;
059    
060    /**
061     * Default constructor
062     */
063    public CompassComponent()
064    {
065        new Thread( compass ).start();
066        setLayout( new GridBagLayout() );
067        
068        compassLabel = new JLabel( "No Compass Data" );
069        compassLabel.setFont( new Font( "Courier", Font.BOLD, 16 ) );
070        compassLabel.setForeground( Color.white );
071        compassLabel.setBackground( Color.black );
072        compassLabel.setOpaque( true );
073        compassLabel.setBorder( BorderFactory.createEmptyBorder( 4,4,4,4 ) );
074        
075        GridBagConstraints gbc = new GridBagConstraints();
076        gbc.gridx = gbc.gridy = 0;
077        gbc.fill = GridBagConstraints.BOTH;
078        gbc.weightx = gbc.weighty = 1;
079        add( compassLabel, gbc );
080        
081        new Thread( new Runnable()
082                {
083                        @Override
084                        public void run()
085                        {
086                                while( true )
087                                {
088                                        try
089                    {
090                            Thread.sleep( 1000 );
091                    }
092                    catch( InterruptedException e )
093                    {
094                            e.printStackTrace();
095                    }
096                    
097                    if( compass.getCompassData() != null )
098                        compassLabel.setText( ""+compass.getCompassData().toString() );
099                                }
100                        }
101                }).start();
102
103    }
104    
105    /**
106     * @return the compass reader
107     */
108    public CompassSerialReader getCompass()
109    {
110        return compass;
111    }
112}