/** * Paints all the objects. * * <p>Must be synchronized on the World.lock. */ public void paintObjects(Graphics2D g) { Set<Actor> objects = WorldVisitor.getObjectsListInPaintOrder(world); int paintSeq = 0; for (Iterator<Actor> iter = objects.iterator(); iter.hasNext(); ) { Actor thing = iter.next(); int cellSize = WorldVisitor.getCellSize(world); GreenfootImage image = ActorVisitor.getDisplayImage(thing); if (image != null) { ActorVisitor.setLastPaintSeqNum(thing, paintSeq++); double halfWidth = image.getWidth() / 2.; double halfHeight = image.getHeight() / 2.; AffineTransform oldTx = null; try { int ax = ActorVisitor.getX(thing); int ay = ActorVisitor.getY(thing); double xCenter = ax * cellSize + cellSize / 2.; int paintX = (int) Math.floor(xCenter - halfWidth); double yCenter = ay * cellSize + cellSize / 2.; int paintY = (int) Math.floor(yCenter - halfHeight); int rotation = ActorVisitor.getRotation(thing); if (rotation != 0) { // don't bother transforming if it is not rotated at // all. oldTx = g.getTransform(); g.rotate(Math.toRadians(rotation), xCenter, yCenter); } ImageVisitor.drawImage(image, g, paintX, paintY, this, true); } catch (IllegalStateException e) { // We get this if the object has been removed from the // world. That can happen when interactively invoking a // method that removes an object from the world, while the // scenario is executing. } // Restore the old state of the graphics if (oldTx != null) { g.setTransform(oldTx); } } } }
/** * If it is a new actor, that has not been added to the world yet, the dragging is handled here. */ public boolean drag(Object o, Point p) { Insets insets = getInsets(); Point p2 = new Point(p.x - insets.left, p.y - insets.top); if (o instanceof Actor && ActorVisitor.getWorld((Actor) o) == null) { if (!getVisibleRect().contains(p)) { return false; } if (o != dragActor) { // It is the first time we are dragging this actor. Create the drag image. dragActor = (Actor) o; dragImage = GreenfootUtil.createDragShadow(ActorVisitor.getDragImage(dragActor).getAwtImage()); } dragLocation = p2; repaint(); return true; } else if (dropTargetListener != null) { return dropTargetListener.drag(o, p2); } else { return false; } }