/** * PROJECT 2 ROBOT DISTANCE * Solution * * @Munsee, Ron * @version 1.00 04/05/11 */ import java.io.*; // used for console import java.text.*; // used for formatting numberic data import javax.swing.*; import java.awt.*; public class Proj2Robot extends JApplet { double northDist; // IN holds users first robot move north (0-100) double eastDist; // IN holds users first robot move east (0-100) public void init() { double distFromStart; // OUT holds calculated distance value String str; // temp variable to capture users input //--------------------- Preliminaries ---------------- // Display Welcome Screen JOptionPane.showMessageDialog( null, "\n\n\t Welcome to the Robot Distance Program"+ "\n\n\t You will be prompted for two ROBOT MOVES "+ "\n\t The first MOVE is NORTH and the second is EAST"+ "\n\t The program will calculate and display the distance "+ "\n\t from the starting position."+ "\n\n\t You may enter floating point numbers between 0 and 100 for"+ "\n\t distance traveled." ); //------------------ Get the North and East Moves for the ROBOT ---------------- str = JOptionPane.showInputDialog( "\n\n\t Enter the distance the Robot traveled North : "); northDist = Double.parseDouble(str); str = JOptionPane.showInputDialog( "\n\n\t Enter the distance the Robot traveled East : "); eastDist = Double.parseDouble(str); // ------------------- Calculations ----------------------- distFromStart = Math.sqrt(Math.pow(northDist,2.0)+Math.pow(eastDist,2.0)); } public void paint( Graphics g ) { //using (400,400) as center of applet. //these casts stink... g.drawLine( 400, 400, 400, 400-(int)northDist ); g.drawLine( 400, 400-(int)northDist, 400+(int)eastDist, 400-(int)northDist ); g.setColor( Color.RED ); g.drawLine( 400, 400, 400+(int)eastDist, 400-(int)northDist ); } }