/home/wpollock1/public_html/restricted/Java1/TempConv.java
// Temperature Conversion Chart GUI application.
// Demonstrates simple AWT graphics, loops, and if statements.
// A "creative extra" used here is a technique to line-up
// columns. This only works for "monospaced" fonts.
//
// Written 2016 by Wayne Pollock, Tampa Florida USA.
import java.awt.*;
import java.awt.event.*;
public class TempConv extends Frame {
public TempConv () {
setTitle( "Temperature Conversion" );
setSize( 350, 520 );
setLocationRelativeTo( null ); // Center on the screen
setVisible( true );
addWindowListener(
new WindowAdapter()
{ public void windowClosing( WindowEvent e )
{ System.exit( 0 );
}
}
);
}
public static void main ( String [] args )
{
TempConv me = new TempConv();
}
public void paint( Graphics g )
{
// Determine the top of the visible part of the frame, with a little extra:
int top = getInsets().top + 10;
int position = top + 32; // The Y-coord of the first table row to draw
// Set the background color (see also "setBackground method")
g.setColor( Color.CYAN );
g.fillRect( 0, 0, getWidth(), getHeight() );
// Set a monospaced font:
g.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
// Draw column labels:
g.setColor( Color.BLACK );
g.drawString( "F C", 90, top + 15 );
// For each Fahrenheit temp from 0 to 250 by 10 degrees:
for ( int fahrenheit = 0; fahrenheit <= 250; fahrenheit += 10 )
{
// Calculate Celsius from Fahrenheit:
double celsius = 5.0 * ( fahrenheit - 32.0 ) / 9.0;
// Format one row of output, including rounding, to align
// numbers on the right, using Java5 Formatter method:
String tableRow = String.format("%5d %5.0f",
fahrenheit, celsius );
// Set color of text:
if ( fahrenheit <= 32 )
g.setColor( Color.BLUE );
else if ( fahrenheit < 212 )
g.setColor( Color.BLACK );
else
g.setColor( Color.RED );
// Draw one row of the table:
g.drawString( tableRow, 50, position );
// Adjust the vertical position to draw the next row:
position += 15;
}
// Draw chart titles:
g.setColor( Color.BLACK );
g.drawString( "Temperature Conversion Chart", 25, position+6 );
// \u00A9 is copyright symbol; \u2013 is an en dash:
g.drawString( "\u00A9 2005\u20132016 by Wayne Pollock", 25, position+22 );
}
}