ExchangeCmd.java
上传用户:sdzznc
上传日期:2022-07-23
资源大小:51k
文件大小:1k
- // Copyright by Scot Drysdale
- package cn.edu.nju.software.grapheditor.cmd;
- import java.awt.Point;
- import cn.edu.nju.software.grapheditor.Drawing;
- import cn.edu.nju.software.grapheditor.shape.Shape;
- public class ExchangeCmd extends Command {
- private Shape firstShape; // the first Shape clicked
- /**
- * When the mouse is clicked, find the frontmost Shape in the drawing that
- * contains the mouse position. If there is such a Shape, then if this is
- * the first click in the pair of clicks (indicated by firstShape being
- * null), save it in firstShape. Otherwise, exchange the centers of this
- * newly clicked Shape and firstShape.
- */
- public void executeClick(Point p, Drawing dwg) {
- // Find the frontmost shape containing p.
- Shape s = dwg.getFrontmostContainer(p);
- if (s != null) { // was there a Shape containing p?
- if (firstShape == null)
- firstShape = s; // save this Shape for when there's another
- // click
- else {
- // We have two Shapes to exchange. Get their centers.
- Point firstCenter = firstShape.getCenter();
- Point secondCenter = s.getCenter();
- // Exchange their centers.
- firstShape.setCenter(secondCenter);
- s.setCenter(firstCenter);
- // Now we get to start all over.
- firstShape = null;
- }
- }
- }
- }