001    package ui.renderer;
002    
003    import util.*;
004    import java.awt.Component;
005    import java.awt.Color;
006    import java.awt.Graphics;
007    import java.util.*;
008    import ui.recognizer.MonochromaticColor;
009    import ui.recognizer.Units;
010    import javax.swing.Icon;
011    
012    public class SpectraIcon implements Icon {
013    
014        int width = 1024;
015        int height = 10;
016        Star star;
017    
018        double min = 3000 * Units.ANGSTROMS;
019        double max = 7000 * Units.ANGSTROMS;
020    
021        public SpectraIcon(Star aStar) {
022            star = aStar;
023        }
024    
025        public int getIconHeight() {
026            return height;
027        }
028    
029        public int getIconWidth() {
030            return width;
031        }
032    
033        /** Draw the icon at the specified location.
034         * Icon implementations may use the Component argument to get properties useful for painting, e.g.
035         * the foreground or background color
036         */
037        public void paintIcon(Component c, Graphics g, int x, int y) {
038            //TODO: Double buffer if flicker becomes noticable
039    
040            //Rectangle bounds = c.getBounds();
041            //int w = bounds.getWidth();
042            //int h = bounds.getHeight();
043    
044            // Paint spectra background black
045            g.setColor(Color.white);
046            g.fillRect(x, y, width, height);
047    
048            List<Line> lines = star.getEmissionLines();
049            for (Line line : lines) {
050                double wavelength = line.getWavelength() * Units.ANGSTROMS;
051                int xoffset = (int) (((double) width)*(wavelength - min)/(max - min));
052                if (xoffset < 0 || xoffset >= width) continue;
053    
054                if (wavelength < MonochromaticColor.MINIMUM_WAVELENGTH)
055                    wavelength = MonochromaticColor.MINIMUM_WAVELENGTH;
056                else if (wavelength > MonochromaticColor.MAXIMUM_WAVELENGTH)
057                    wavelength= MonochromaticColor.MAXIMUM_WAVELENGTH;
058    
059                // Plot emission line using appropriate color
060                Color color = new MonochromaticColor(wavelength);
061    
062                g.setColor(color);
063                g.drawLine(x + xoffset, y, x + xoffset, y + height);
064            }
065        }
066    }