/home/wpollock1/public_html/restricted/Java1/TempCon2.java

// Temperature Conversion Chart
// Displays a chart of Fahrenheit to Celsius temperatures.  This version
// also show how to line up columns, using the FontMetrics class.
// Written 2020 by Wayne Pollock, Tampa Florida USA.

import java.awt.*;
import java.awt.event.*;

public class TempCon2 extends Frame {
    public TempCon2 () {
        setTitle( "Temperature Conversion" );
        setSize( 320, 580 );
        setLocationRelativeTo( null );  // Center window on the screen
        setVisible( true );

        addWindowListener(
            new WindowAdapter() {
                public void windowClosing( WindowEvent e )
                {  System.exit( 0 );
                }
            }
        );
    }

    public static void main ( String [] args ) {
        TempCon2 me = new TempCon2();
    }

    public void paint( Graphics g ) {
        final Color SHADING = Color.LIGHT_GRAY;
        final Font TITLE_FONT = new Font( "SansSerif", Font.BOLD, 14 );
        final Font TABLE_FONT = new Font( "SansSerif", Font.PLAIN, 12 );

        // Determine the the visible part of the frame:
        int top = getInsets().top;
        int borderWidth = getInsets().left + getInsets().right;
        int yCoord = top + TITLE_FONT.getSize();  // Initial Y coordinate.
        boolean isOddRow = false;  // To apply shading to odd rows.

        // Calculate the position of the first column, by calculating the
        // window width and subtracting the width of the two colums and the
        // borders.  The result is the amout of blank space.  With two columns,
        // divide the blank space by three to evenly space out the columns.
        // Finally, add back the width of the column to find the X coordinate
        // of the right end of the column (so numbers line up right).  The
        // calculation of the X coordinate of the right-side column is computed
        // simularly.

        // Determine column positions:

        int frameWidth = getWidth();
        g.setFont( TABLE_FONT );
        FontMetrics fm = g.getFontMetrics();
        int rowHeight = fm.getHeight() + 2;  // 2 pixels between rows
        int colWidth = fm.stringWidth( "-000\u2103" );  // Widest possible value
        int space = ( frameWidth - borderWidth - (2 * colWidth) ) / 3 ;
        int col1X =  space + colWidth;
        int col2X = frameWidth - space;

        // Center the titles over the columns:
        g.setFont( TITLE_FONT );
        int fTitleWidth = fm.stringWidth( " Fahrenheit " );
        int cTitleWidth = fm.stringWidth( " Celsius " );
        space = ( frameWidth - borderWidth  - fTitleWidth - cTitleWidth ) / 3 ;
        g.drawString( " Fahrenheit ", space, yCoord );
        g.drawString(" Celsius ", 2*space + cTitleWidth + borderWidth, yCoord);
        yCoord += rowHeight;
        g.setFont( TABLE_FONT );

        for ( int fahrenheit = 0; fahrenheit <= 250; fahrenheit += 10 ) {
            double celsius = 5 * ( fahrenheit - 32 ) / 9.0;

            // Shade the background of alternate rows:
            if ( isOddRow ) {
                g.setColor( SHADING );
                g.fillRect(0, (yCoord - rowHeight + 4), frameWidth, rowHeight );
            }

            // Set text color:
            if ( fahrenheit <= 32 )
                g.setColor( Color.BLUE );
            else if ( fahrenheit < 212 )
                g.setColor( Color.BLACK );
            else
                g.setColor( Color.RED );

           // Draw row with columns nicely lined up on the right:
           String cString = String.format( "%3.0f\u2103", celsius);
           String fString = String.format( "%3d\u2109", fahrenheit );

           colWidth = fm.stringWidth( fString );
           g.drawString( fString, col1X - colWidth, yCoord );

           colWidth = fm.stringWidth( cString );
           g.drawString( cString, col2X - colWidth, yCoord );

           yCoord += rowHeight;
           isOddRow = ! isOddRow;  // Shade every other row.
        }

        // Add a footer with a copyright notice ("\u00A9" is (c) symbol):
        g.setColor( Color.BLACK );
        String title = "Temperature Conversion Chart";
        colWidth = fm.stringWidth( title );
        g.drawString( title, (frameWidth - colWidth) / 2, yCoord+6 );
        title = "\u00A9 2020 by Wayne Pollock";
        colWidth = fm.stringWidth( title );
        g.drawString( title, (frameWidth - colWidth) / 2, yCoord+18 );
    }
}  // End of TempCon2 class