WorldRectangle demonstrates how to draw an object using a drawing panel's world coordinates. @author Wolfgang Christian, Jan Tobochnik, Harvey Gould @version 1.0 05/16/05
| 16 | * @version 1.0 05/16/05 |
| 17 | */ |
| 18 | public class WorldRectangle implements Drawable { |
| 19 | double left, top; // position of rectangle in world coordinates |
| 20 | double width, height; // size of rectangle in world units |
| 21 | |
| 22 | /** |
| 23 | * Constructs a WorldRectangle with position and dimensions values given in world coordinates. |
| 24 | * |
| 25 | * @param left double |
| 26 | * @param top double |
| 27 | * @param width double |
| 28 | * @param height double |
| 29 | */ |
| 30 | public WorldRectangle(double left, double top, double width, double height) { |
| 31 | this.left = left; // location of left edge |
| 32 | this.top = top; // location of top edge |
| 33 | this.width = width; |
| 34 | this.height = height; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Draws this rectangle using a drawing panel's world coordinates. |
| 39 | * Required to implement the Drawable interface. |
| 40 | * |
| 41 | * @param panel DrawingPanel |
| 42 | * @param g Graphics |
| 43 | */ |
| 44 | @Override |
| 45 | public void draw(DrawingPanel panel, Graphics g) { |
| 46 | // This method implements the Drawable interface |
| 47 | g.setColor(Color.RED); // set drawing color to red |
| 48 | // convert from world to pixel coordinates |
| 49 | int leftPixels = panel.xToPix(left); |
| 50 | int topPixels = panel.yToPix(top); |
| 51 | int widthPixels = (int) (panel.getXPixPerUnit()*width); |
| 52 | int heightPixels = (int) (panel.getYPixPerUnit()*height); |
| 53 | g.fillRect(leftPixels, topPixels, widthPixels, heightPixels); // draws rectangle |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Open Source Physics software is free software; you can redistribute |
nothing calls this directly
no outgoing calls
no test coverage detected