package com.mindprod.example;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
/**
* Demonstrate use of java.swing.JPanel.
* <p/>
* Draw a rainbow triangle to illustrate Kohlberg's six stages of moral development.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-10-19 initial version
* @since 2010-01-19
*/
public final class TestDraw
{
/**
* size in pixels of the image
*/
private static final int SIZE = 1024;
/**
* Debugging harness
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
SwingUtilities.invokeLater( new Runnable()
{
/**
* do all swing work on the swing thread.
*/
public void run()
{
TriangleCanvas canvas = new TriangleCanvas();
JFrame.setDefaultLookAndFeelDecorated( true );
final JFrame frame = new JFrame( "Kohlberg's Six Stages of Moral Development" );
frame.setResizable( false );
frame.setUndecorated( false );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( SIZE + 24, SIZE + 64 );
frame.getContentPane().add( canvas );
frame.validate();
frame.setVisible( true );
}
}
);
}
}
/**
* draws rainbow triangle with Kohlberg's Summary.
*/
final class TriangleCanvas extends JPanel
{
/**
* size in pixels of the image
*/
private static final int SIZE = 1024;
private static final String[] bandText =
{
"avoiding punishment",
"self-interest",
"good boy attitude",
"law and order morality",
"social contract",
"principle"
};
/**
* font for labelling
*/
private static final Font font = new Font( "Tiresias PCFont Z", Font.PLAIN, 40 );
private static final int[] bandBottoms = {
SIZE,
SIZE - 150,
SIZE - 150 * 2,
SIZE - 150 * 3,
SIZE - 150 * 4,
SIZE - 150 * 5,
0 };
private static final int[] bandColors = { 0x6a00ce, 0x005cff, 0x00ff80, 0xfff700, 0xff6200, 0xcc0000 };
/**
* does drawing
*
* @param g where to paint
*/
public void paintComponent( Graphics g )
{
Graphics2D g2d = ( Graphics2D ) g;
g2d.setBackground( Color.WHITE );
super.paintComponent( g );
g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON ) );
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
for ( int band = 0; band < 6; band++ )
{
g2d.setColor( new Color( bandColors[ band ] ) );
g2d.fillRect( 0,
bandBottoms[ band + 1 ],
SIZE
,
bandBottoms[ band ] - bandBottoms[ band + 1 ]
);
}
g2d.setColor( Color.WHITE );
g2d.fillPolygon( new int[] { 0, 0, SIZE / 2, }, new int[] { 0, SIZE, 0 }, 3 );
g2d.fillPolygon( new int[] { SIZE, SIZE, SIZE / 2, }, new int[] { 0, SIZE, 0 }, 3 );
g2d.setColor( Color.BLACK );
/** add top left border to triangle.*/
g2d.drawLine( SIZE / 2, 0, 0, SIZE );
/** add top right border to triangle.*/
g2d.drawLine( SIZE / 2, 0, SIZE, SIZE );
g2d.setColor( Color.BLACK );
g.setFont( font );
final FontMetrics fm = g.getFontMetrics( font );
for ( int band = 0; band < 6; band++ )
{
String wording = bandText[ band ];
final int xadj = fm.stringWidth( wording ) / 2;
g.drawString( wording, SIZE / 2 - xadj, bandBottoms[ band ] - 50 );
}
}
}