/****************************************************************** * @(#) PolygonTest.java 1.0 * * Copyright (c) 2002 John Miller * All Right Reserved *----------------------------------------------------------------- * Permission to use, copy, modify and distribute this software and * its documentation without fee is hereby granted provided that * this copyright notice appears in all copies. * WE MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WE SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY ANY USER AS A RESULT OF USING, * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. *----------------------------------------------------------------- * * @version 1.0, 6 Mar 2002 * @author John Miller */ import java.awt.*; import java.awt.geom.*; import javax.swing.*; import static java.lang.Math.PI; import static java.lang.System.*; /****************************************************************** * The PolygonTest class tests the Polygon class by moving/walking * a polygon and alternatively drawing and filling it. */ public class PolygonTest extends JFrame implements Runnable { private static final int STEP = 10; private static final Dimension DIM = new Dimension (600, 600); private final Thread displayer; private boolean fillFlag; private int sign; private Point2D pos; private Polygon poly; /************************************************************** * The DisplayPanel inner class is used to place shapes in the * drawing region. */ public class DisplayPanel extends JPanel { /********************************************************** * Paint the display panel component. * @param gr Graphics context. */ public void paintComponent (Graphics gr) { super.paintComponent (gr); Graphics2D gr2 = (Graphics2D) gr; if (fillFlag) { gr2.translate (pos.getX (), pos.getY ()); gr2.setPaint (Color.red); gr2.fill (poly); } else { sign *= -1; gr2.rotate (sign * PI / 12., pos.getX () + 50, pos.getY () + 50); gr2.translate (pos.getX (), pos.getY ()); gr2.setPaint (Color.blue); gr2.draw (poly); } // if } // paintComponent } // DisplayPanel inner class /************************************************************** * Construct a frame. * @param title Title of frame */ public PolygonTest (String title) { super (title); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Point2D [] corner = { new Point2D.Double (0, 0), new Point2D.Double (100, 0), new Point2D.Double (0, 100) }; fillFlag = false; sign = 1; pos = new Point2D.Double (100, 100); poly = new Polygon (corner); getContentPane ().add (new DisplayPanel ()); setLocation (100, 100); setSize (DIM); setVisible (true); displayer = new Thread (this, "displayer"); displayer.start (); } // PolygonTest /************************************************************** * Run method for the display thread. * Repeatedly update coordinates, sleep and repaint. */ public void run () { for (int i = 0; i < 40; i++) { fillFlag = ! fillFlag; pos.setLocation (pos.getX () + STEP, pos.getY () + STEP); out.println (" pos = ( " + pos.getX () + " , " + pos.getY () + " ) "); try { displayer.sleep (1000); } catch (InterruptedException ex) { err.println ("PolygonTest.run: sleep failed"); } // try repaint (); } // for } // run /************************************************************** * Main method for invoking the application. * @param args Command-line arguments */ public static void main (String [] args) { PolygonTest frame = new PolygonTest ("PolygonTest"); } // main } // PolygonTest class