/** * Keeps track of all objects/UML components that need to be represented and * draws them. Will also be responsible for allowing dragging of UML components. * * @author Eric Liebowitz * @version 22jun11 */ package simple_uml.view.canvas; import javax.swing.*; import java.awt.*; import java.awt.event.*; import simple_uml.model.MainModel; import simple_uml.model.SumlClassModel; import java.util.Vector; public class SumlCanvas extends Canvas { /** * Width of rectangle used for clicking on objects to select them. If you * change this, be sure to make it an even number. */ private static final int SELECT_WIDTH = 4; /** * Height of rectangle used for clicking on objects to select them. If you * change this, be sure to make it an even number. */ private static final int SELECT_HEIGHT = 4; /** * Rectangle used for making selections based on intersection w/ it */ private static final Rectangle rSelector = new Rectangle(SELECT_WIDTH, SELECT_HEIGHT); /** * Default, monospace font text can use to draw */ protected static final Font MONO_FONT = new Font ("Monospaced", Font.BOLD, 12); /** * Reference to the main model. */ private MainModel mm; /** * List of shapes on this canvas that we'll be drawing. */ private Vector shapes = new Vector(); public SumlCanvas (MainModel mm) { super (); this.mm = mm; shapes.add(new SumlClassView(this, MainModel.ANIMAL)); shapes.add(new SumlClassView(this, MainModel.HORSE)); shapes.add(new SumlClassView(this, MainModel.ZOMBIE_HORSE)); MovingListener ml = new MovingListener(this); addMouseListener(ml); addMouseMotionListener(ml); } /** * Gives you the shape under a given coordinate. Creates a * SELECT_WIDTH x SELECT_HEIGHT rectangle centered around the given point * and tests all the shapes on this canvas for intersection. * * @param x The x-coord of your selection * @param y the y-coord of your selection * * @return The shape under your selection. If no shape is there, you'll get * null. */ public SumlShape getSelectionAt (int x, int y)//==> { rSelector.setLocation(x - 2, y - 2); SumlShape r = null; for (SumlShape s: this.shapes) { if (s.intersects(rSelector)) { r = s; } } return r; }//<== /** * Looks through all our shapes for one with a particular model backing it. * TODO: I assume everything is a SumlClassModel right now. I have to create * a generic class which all shape models can extend. * * @param cm The model to look for in our views/Shapes. */ public SumlShape getClassShape (SumlClassModel cm)//==> { SumlShape r = null; for (SumlShape s: this.shapes) { if (s.getModel().equals(cm)) { r = s; } } return r; }//<== /** * Goes through all the shapes in this canvas and paints each one. * * @param g Graphics object to paint stuff with. We'll cast it to a * Graphics2D object before passing it to the shapes to draw. */ public void paint (Graphics g)//==> { Graphics2D g2d = (Graphics2D)g; for (SumlShape s: this.shapes) { s.paint(g2d); } }//<== }