/****************************************************************** * @(#) Linear.java 1.1 * * Copyright (c) 2005 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.1, 28 Sep 2005 * @author John Miller */ import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; /****************************************************************** * The Linear class displays a line based upon inputs from the * command-line and then moves a ball parametrically along the line. */ public class Linear extends JFrame implements Runnable { private static final int STEP = 5; private static final Dimension DIM = new Dimension (600, 600); private Line2D line; private Ellipse2D ball; private Point2D pos; private int nSteps; private double dx; private double dy; private final Thread displayer; /************************************************************** * 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; gr2.setColor (Color.red); gr2.draw (line); gr2.setColor (Color.blue); ball.setFrame (pos.getX () - 10, pos.getY () - 10, 20, 20); gr2.fill (ball); } // paintComponent } // DisplayPanel inner class /************************************************************** * Construct a frame. * @param title Title of frame * @param x1 Point 1's x coordinate * @param y1 Point 1's y coordinate * @param x2 Point 2's x coordinate * @param y2 Point 2's y coordinate */ public Linear (String title, double x1, double y1, double x2, double y2) { super (title); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); line = new Line2D.Double (x1, y1, x2, y2); double distX = x2 - x1; double distY = y2 - y1; //double dist = Math.sqrt (distX * distX + distY * distY); double dist = Math.max (Math.abs (distX), Math.abs (distY)); nSteps = (int) dist / STEP; dx = distX / (double) nSteps; dy = distY / (double) nSteps; ball = new Ellipse2D.Double (); pos = new Point2D.Double (x1, y1); add (new DisplayPanel ()); setLocation (100, 100); setSize (DIM); setVisible (true); displayer = new Thread (this, "displayer"); displayer.start (); } // Linear /************************************************************** * Run method for the display thread. * Repeatedly update coordinates, sleep and repaint. */ public void run () { for (int i = 0; i < nSteps; i++) { pos.setLocation (pos.getX () + dx, pos.getY () + dy); System.out.println (" ( " + pos.getX () + " , " + pos.getY () + " ) "); try { displayer.sleep (75); } catch (InterruptedException ex) { System.err.println ("Linear.run: sleep failed"); }; // try repaint (); }; // for } // run /************************************************************** * Main method for invoking the application. * @param args Command-line arguments */ public static void main (String [] args) { if (args.length != 4) { System.out.println ("Linear.main: usage: java Linear x1 y1 x2 y2"); System.exit (0); }; // if Linear frame = new Linear ("Linear", Double.parseDouble (args [0]), Double.parseDouble (args [1]), Double.parseDouble (args [2]), Double.parseDouble (args [3])); } // main } // Linear class