/* * [Spectrum.java] * * Summary: Sample program to draw three spectra. * * Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1998-01-01 initial version */ package com.mindprod.wavelength; import com.mindprod.common18.FontFactory; import java.awt.Canvas; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; /** * Sample program to draw three spectra. *

* Uses the com.mindprod.wavelength package. *

* The classes Spectrum, Display, FCanvas, HCanvas and WCanvas * are just for demonstrating the Wavelength class. They are not * needed to use it in your own applications. * * @author Roedy Green, Canadian Mind Products * @version 1.0 1998-01-01 initial version * @since 1998-01-01 */ public final class Spectrum { public static void main( String[] args ) { Frame frame = new Frame( "Spectrum" ); frame.setSize( 500, 400 ); frame.setLayout( null ); frame.setBackground( Color.WHITE ); // frequency spectrum FCanvas t = new FCanvas(); t.setBackground( Color.WHITE ); t.setForeground( Color.BLACK ); t.setLocation( 0, 50 ); t.setSize( 500, 50 ); frame.add( t ); // wavelength spectrum WCanvas w = new WCanvas(); w.setBackground( Color.WHITE ); w.setForeground( Color.BLACK ); w.setLocation( 0, 150 ); w.setSize( 500, 50 ); frame.add( w ); // hue spectrum HCanvas h = new HCanvas(); h.setBackground( Color.WHITE ); h.setForeground( Color.BLACK ); h.setLocation( 0, 250 ); h.setSize( 500, 50 ); frame.add( h ); frame.validate(); frame.setVisible( true ); frame.addWindowListener( new java.awt.event.WindowAdapter() { /** * Handle request to shutdown. * @param e event giving details of closing. */ public void windowClosing( java.awt.event.WindowEvent e ) { System.exit( 0 ); } // end WindowClosing } // end anonymous class );// end addWindowListener line } // end main } // end Spectrum /** * draw a spectrum by frequency, a realistic rainbow */ final class FCanvas extends Canvas { private static final int BORDER = 25; private static final int DATAPOINTS = 400; // range we plot wavelengths over private static final int FREQUENCY_START = 380; private static final int FREQUENCY_STOP = 790; public void paint( Graphics g ) { // for text Font little = FontFactory.build( "Dialog", Font.PLAIN, 10 ); FontMetrics littlefm = getFontMetrics( little ); Font big = FontFactory.build( "Dialog", Font.BOLD, 14 ); FontMetrics bigfm = getFontMetrics( big ); // draw the Light spectrum for ( int i = 0; i < DATAPOINTS; i++ ) { float freq = ( FREQUENCY_STOP - FREQUENCY_START ) * i / ( float ) DATAPOINTS + FREQUENCY_START; Color shade = Wavelength.fColor( freq, 1.0f ); g.setColor( shade ); g.drawLine( i + BORDER, 0, i + BORDER, 50 ); } // end for // must do legends in a second pass to avoid spectrum overlaying. // title for nm g.setFont( big ); g.setColor( Color.BLACK ); String title = "Frequency (Terahertz)"; // x,y is bottom left corner of text g.drawString( title, DATAPOINTS / 2 + BORDER - bigfm.stringWidth( title ) / 2, 40 ); g.setFont( little ); g.setColor( Color.GRAY ); // draw nm ticks and numbering every 50 final int span = FREQUENCY_STOP - FREQUENCY_START; for ( int tick = FREQUENCY_START; tick <= FREQUENCY_STOP; tick += 50 ) { int tickPix = ( ( tick - FREQUENCY_START ) * DATAPOINTS ) / span + BORDER; g.drawLine( tickPix, 0, tickPix, 5 ); String tickString = String.valueOf( tick ); g.drawString( tickString, tickPix - littlefm.stringWidth( tickString ) / 2, 20 ); } // end for } // end paint } // end FCanvas /** * draw a wavelength spectrum, a realistic rainbow */ final class WCanvas extends Canvas { private static final int BORDER = 25; private static final int DATAPOINTS = 400; // range we plot wavelenths over private static final int WAVELENGTH_START = 380; private static final int WAVELENGTH_STOP = 780; public void paint( Graphics g ) { Graphics2D g2d = ( Graphics2D ) g; g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); // if wanted to smooth geometric shapes too // g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, // RenderingHints.VALUE_ANTIALIAS_ON ); // for text Font little = FontFactory.build( "Dialog", Font.PLAIN, 10 ); FontMetrics littlefm = getFontMetrics( little ); Font big = FontFactory.build( "Dialog", Font.BOLD, 14 ); FontMetrics bigfm = getFontMetrics( big ); // draw the Light spectrum for ( int i = 0; i < DATAPOINTS; i++ ) { float wl = ( WAVELENGTH_STOP - WAVELENGTH_START ) * i / ( float ) DATAPOINTS + WAVELENGTH_START; Color shade = Wavelength.wvColor( wl, 1.0f ); g.setColor( shade ); g.drawLine( i + BORDER, 0, i + BORDER, 50 ); } // end for // must do legends in a second pass to avoid spectrum overlaying. // title for nm g.setFont( big ); g.setColor( Color.BLACK ); String title = "Wavelength (nm)"; g.drawString( title, DATAPOINTS / 2 + BORDER - bigfm.stringWidth( title ) / 2, 40 ); g.setFont( little ); g.setColor( Color.GRAY ); // draw nm ticks and numbering every 50 final int span = WAVELENGTH_STOP - WAVELENGTH_START; for ( int tick = WAVELENGTH_START; tick <= WAVELENGTH_STOP; tick += 50 ) { int tickPix = ( ( tick - WAVELENGTH_START ) * DATAPOINTS ) / span + BORDER; g.drawLine( tickPix, 0, tickPix, 5 ); String tickString = String.valueOf( tick ); g.drawString( tickString, tickPix - littlefm.stringWidth( tickString ) / 2, 20 ); } // end for } // end paint } // end WCanvas /** * draw a hue Spectrum */ final class HCanvas extends Canvas { private static final int BORDER = 25; private static final int DATAPOINTS = 400; public void paint( Graphics g ) { // for text Font little = FontFactory.build( "Dialog", Font.PLAIN, 10 ); FontMetrics littlefm = getFontMetrics( little ); Font big = FontFactory.build( "Dialog", Font.BOLD, 14 ); FontMetrics bigfm = getFontMetrics( big ); // draw the Hue spectrum for ( int i = 0; i < DATAPOINTS; i++ ) { float hue = i / ( float ) DATAPOINTS; Color shade = new Color( Color.HSBtoRGB( hue, 1, 1 ) ); g.setColor( shade ); g.drawLine( i + BORDER, 0, i + BORDER, 50 ); } // end for // must do legends in a second pass to avoid spectrum overlaying. // title for nm g.setFont( big ); g.setColor( Color.BLACK ); String title = "Java Hue"; g.drawString( title, DATAPOINTS / 2 + BORDER - bigfm.stringWidth( title ) / 2, 40 ); g.setFont( little ); g.setColor( Color.BLACK ); // draw hue ticks and numbering every 0.1 for ( int tick = 0; tick <= 10; tick++ ) { int tickPix = tick * DATAPOINTS / 10 + BORDER; g.drawLine( tickPix, 0, tickPix, 5 ); String tickString = Display.oneDec( tick ); g.drawString( tickString, tickPix - littlefm.stringWidth( tickString ) / 2, 20 ); } // end for } // end paint } // end HCanvas final class Display { public static String oneDec( int i ) { String s = Integer.toString( i ); switch ( s.length() ) { case 0: s = "0.0"; break; case 1: s = "0." + s; break; default: s = s.substring( 0, s.length() - 1 ) + '.' + s.substring( s.length() - 1 ); break; } // end switch return s; } // end oneDec } // end HDisplay