/** * BasicVehicle.java * * @author Zuo Yan * @version 1.0 */ package vehicle; import java.awt.Graphics2D; import java.awt.Color; import java.awt.Shape; import java.awt.Polygon; import java.awt.geom.Rectangle2D; import java.awt.geom.Ellipse2D; import world.World; /** * Basic Vehicle implementation.

* * This is a very simple vehicle that demostrates how to create * and draw shapes onto the world. In addition, movement controls * are handled by the keyboard arrows.
* Left arrow key - Turn counterclockwise
* Right arrow key - Turn clockwise
* Up arrow key - Move forward
* Down arrow key - Move backward */ public class BasicVehicle extends AbstractVehicle { // shapes used to draw the vehicle private Shape body; private Shape head; private Shape tail; /** * Create a very simple movement vehicle * * @param world the World this vehicle is in */ public BasicVehicle(World aWorld) { super(aWorld); createShapes(); } /** * Called by World when it's time to update. * Checks for button presses, rotating and moving appropriately */ public void update() { if (this.theWorld.getKeyPressed(World.KEY_RIGHT)) rotateBy(.1); if (this.theWorld.getKeyPressed(World.KEY_LEFT)) rotateBy(-.1); if (this.theWorld.getKeyPressed(World.KEY_UP)) { double x = 2.0*Math.cos(mvTrack.getOrientation()); double y = 2.0*Math.sin(mvTrack.getOrientation()); moveBy(x,y); } if (this.theWorld.getKeyPressed(World.KEY_DOWN)) { double x = 2.0*Math.cos(mvTrack.getOrientation()); double y = 2.0*Math.sin(mvTrack.getOrientation()); moveBy(-x,-y); } } /** * Create all our shapes representing this vehicle. * This method is calleteh constructor. */ public void createShapes() { // create a rectangle for the body body = new Rectangle2D.Double(0, 0, 16, 6); // a circle at the front head = new Ellipse2D.Double(13, 0, 6, 6); // a triangle at the end Polygon t = new Polygon(); t.addPoint(2, 2); t.addPoint(8, 2); t.addPoint(2, -7); tail = t; // need to register all shapes with the tracker mvTrack.registerShape(body); mvTrack.registerShape(head); mvTrack.registerShape(tail); } /** * Use the graphics system to actually paint the shapes * we have created. * * @param g the graphics system to use */ public void draw(Graphics2D g) { // Color in body and front to be blue g.setPaint(Color.blue); g.fill(body); g.fill(head); // Make the outline of the body and front black g.setPaint(Color.black); g.draw(body); g.draw(head); // The end triangle is all red g.setPaint(Color.red); g.fill(tail); g.draw(tail); } }