package com.mindprod.example;
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;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Demonstrate use of java.awt.Canvas. Draw a pixel ruler.
* <p/>
* Draw a pixel ruler with grid lines every green=5, red=10, blue=100 pixels
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-01-01 initial version
* @since 2009-01-01
*/
public final class TestCanvas
{
/**
* Debugging harness for a Frame
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
final Frame frame = new Frame();
TriangleCanvas canvas = new TriangleCanvas();
frame.add( canvas );
frame.setSize( 1000, 1000 );
frame.addWindowListener( new WindowAdapter()
{
/**
* Handle request to shutdown.
* @param e event giving details of closing.
*/
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
frame.validate();
frame.setVisible( true );
}
}
final class GridCanvas extends Canvas
{
/**
* size of square image we generate.
*/
private static final int size = 1000;
/**
* Background colour of the images.
*/
private static final Color background = Color.WHITE;
/**
* colour of minor lines, pale green
*/
private static final Color five = new Color( 128, 250, 128 );
/**
* colour of very major lines, dark blue.
*/
private static final Color hundred = new Color( 0, 0, 40 );
/**
* outline of whole grid
*/
private static final Color outline = Color.BLACK;
/**
* colour of major lines, pale red
*/
private static final Color ten = new Color( 250, 128, 128 );
/**
* font for labelling
*/
private static final Font font = new Font( "Tiresias PCFont Z", Font.PLAIN, 18 );
/**
* does drawing
*
* @param g where to paint
*/
public void paint( Graphics g )
{
Graphics2D g2d = ( Graphics2D ) g;
g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON ) );
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
g.setColor( background );
g.fillRect( 0, 0, size, size );
g.setColor( five );
for ( int i = 0; i <= size; i += 5 )
{
g.drawLine( i, 0, i, size );
}
for ( int i = 0; i <= size; i += 5 )
{
g.drawLine( 0, i, size, i );
}
g.setColor( ten );
for ( int i = 0; i <= size; i += 10 )
{
g.drawLine( i, 0, i, size );
}
for ( int i = 0; i <= size; i += 10 )
{
g.drawLine( 0, i, size, i );
}
g.setColor( hundred );
for ( int i = 0; i <= size; i += 100 )
{
g.drawLine( i, 0, i, size );
}
for ( int i = 0; i <= size; i += 100 )
{
g.drawLine( 0, i, size, i );
}
g.setColor( outline );
for ( int i = 0; i <= size; i += 100 )
{
g.drawLine( i, 0, i, size );
}
for ( int i = 0; i <= size; i += 1000 )
{
g.drawLine( 0, i, size, i );
}
g.setFont( font );
final FontMetrics fm = g.getFontMetrics( font );
for ( int i = 200; i <= size - 200; i += 100 )
{
final String wording = Integer.toString( i );
final int xadj = fm.stringWidth( wording ) / 2;
g.drawString( wording, i - xadj, 100 );
g.drawString( wording, i - xadj, 900 );
}
for ( int i = 200; i <= size - 200; i += 100 )
{
final String wording = Integer.toString( i );
final int xadj = fm.stringWidth( wording ) / 2;
g.drawString( wording, 100 - xadj, i );
g.drawString( wording, 900 - xadj, i );
}
}
}