diff --git a/src/actions/ZoomAction.java b/src/actions/ZoomAction.java new file mode 100644 index 0000000..190302d --- /dev/null +++ b/src/actions/ZoomAction.java @@ -0,0 +1,54 @@ +package actions; + +import paintcomponents.PaintComponent; +import ui.PaintPanel; + +public abstract class ZoomAction extends PaintAction{ + + private double zoomValue; + private int centerX, centerY; + + public ZoomAction(PaintPanel panel) { + super(panel); + centerX = panel.getWidth() / 2; + centerY = panel.getHeight() / 2; + } + + public boolean canPerformAction() { + return true; + } + + public void performAction() { + for ( PaintComponent com: panel.getPaintComponents() ) { + int xDifference = com.getX() - centerX; + int yDifference = com.getY() - centerY; + com.translate((int)(xDifference * getZoomValue()), (int)(yDifference * getZoomValue())); + } + panel.repaint(); + } + + public int getCenterX() { + return centerX; + } + + public void setCenterX(int centerX) { + this.centerX = centerX; + } + + public int getCenterY() { + return centerY; + } + + public void setCenterY(int centerY) { + this.centerY = centerY; + } + + public double getZoomValue() { + return zoomValue; + } + + public void setZoomValue(double value) { + zoomValue = value; + } + +} diff --git a/src/actions/ZoomInAction.java b/src/actions/ZoomInAction.java new file mode 100644 index 0000000..7780679 --- /dev/null +++ b/src/actions/ZoomInAction.java @@ -0,0 +1,22 @@ +package actions; + +import actions.menu.ActionsMenuBarTitles; +import ui.PaintPanel; + +public class ZoomInAction extends ZoomAction { + + public ZoomInAction (PaintPanel panel) { + super(panel); + this.setZoomValue(0.5); + } + + @Override + public void performAction() { + super.performAction(); + } + + @Override + public String locationString() { + return ActionsMenuBarTitles.Edit().Zoom_In().toString(); + } +} diff --git a/src/actions/ZoomOutAction.java b/src/actions/ZoomOutAction.java new file mode 100644 index 0000000..68af0c4 --- /dev/null +++ b/src/actions/ZoomOutAction.java @@ -0,0 +1,23 @@ +package actions; + +import ui.PaintPanel; +import actions.menu.ActionsMenuBarTitles; + +public class ZoomOutAction extends ZoomAction { + + public ZoomOutAction (PaintPanel panel) { + super(panel); + this.setZoomValue(-0.5); + } + + @Override + public void performAction() { + super.performAction(); + } + + @Override + public String locationString() { + return ActionsMenuBarTitles.Edit().Zoom_Out().toString(); + } + +} diff --git a/src/actions/menu/ActionsMenuBar.java b/src/actions/menu/ActionsMenuBar.java index df36428..3a47952 100644 --- a/src/actions/menu/ActionsMenuBar.java +++ b/src/actions/menu/ActionsMenuBar.java @@ -29,6 +29,8 @@ import actions.PaintAction; import actions.RemovePaintComponent; import actions.UpdateDataDisplayBoxAction; +import actions.ZoomInAction; +import actions.ZoomOutAction; public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{ @@ -55,6 +57,8 @@ public ActionsMenuBar(PaintPanel panel){ //edit addAction(new EditRedoAction(panel)); addAction(new EditUndoAction(panel)); + addAction(new ZoomInAction(panel)); + addAction(new ZoomOutAction(panel)); //haskell addAction(new AddHaskellComponent(panel)); @@ -66,6 +70,7 @@ public ActionsMenuBar(PaintPanel panel){ // remove addAction(new RemovePaintComponent(panel)); + } diff --git a/src/actions/menu/ActionsMenuBarTitles.java b/src/actions/menu/ActionsMenuBarTitles.java index 574d78f..3c27919 100644 --- a/src/actions/menu/ActionsMenuBarTitles.java +++ b/src/actions/menu/ActionsMenuBarTitles.java @@ -117,6 +117,15 @@ public Object Open() { append("Open..."); return this; } + + public ActionsMenuBarTitles Zoom_In() { + append("Zoom In"); + return this; + } + public ActionsMenuBarTitles Zoom_Out() { + append("Zoom Out"); + return this; + } }