CIT 594 Drawing the "Bug"
Spring 2015, David Matuszek
angle, where
angle is measured in degrees counterclockwise from East.
You will have to adapt this code slightly. The methods
/** * Paints a triangle to represent this Bug. * * @param g Where to paint this Bug. */ public void paint(Graphics g) { if (color == null) return; g.setColor(color); int x1 = (int) (scaleX(x) + computeDeltaX(12, (int)angle)); int x2 = (int) (scaleX(x) + computeDeltaX(6, (int)angle - 135)); int x3 = (int) (scaleX(x) + computeDeltaX(6, (int)angle + 135)); int y1 = (int) (scaleY(y) + computeDeltaY(12, (int)angle)); int y2 = (int) (scaleY(y) + computeDeltaY(6, (int)angle - 135)); int y3 = (int) (scaleY(y) + computeDeltaY(6, (int)angle + 135)); g.fillPolygon(new int[] { x1, x2, x3 }, new int[] { y1, y2, y3 }, 3); } /** * Computes how much to move to add to this Bug's x-coordinate, * in order to displace the Bug by "distance" pixels in * direction "degrees". * * @param distance The distance to move. * @param degrees The direction in which to move. * @return The amount to be added to the x-coordinate. */ private static double computeDeltaX(int distance, int degrees) { double radians = Math.toRadians(degrees); return distance * Math.cos(radians); } /** * Computes how much to move to add to this Bug's y-coordinate, * in order to displace the Bug by "distance" pixels in * direction "degrees. * * @param distance The distance to move. * @param degrees The direction in which to move. * @return The amount to be added to the y-coordinate. */ private static double computeDeltaY(int distance, int degrees) { double radians = Math.toRadians(degrees); return distance * Math.sin(-radians); }
scaleX and scaleY convert from "Bugs
coordinates" (0.0 to 100.0) to the Java coordinates used in the actual
JPanel (0 to panel width in pixels, 0 to panel height in pixels).