From f0269b5d91c2edc21a1aca78bbb72dbab7903129 Mon Sep 17 00:00:00 2001 From: Daniel_M_Williams Date: Fri, 3 Jul 2020 16:51:01 -0400 Subject: [PATCH] [refactor] Changed Dimension to Point display Figures --- .../gui/scalefigure/AbstractScaleFigure.java | 7 ++--- .../gui/scalefigure/FinPointFigure.java | 12 ++++----- .../gui/scalefigure/RocketFigure.java | 17 ++++-------- .../gui/scalefigure/ScaleScrollPane.java | 26 +++++++++---------- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java b/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java index b06b96668..780505be5 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import java.awt.Point; import java.util.EventListener; import java.util.EventObject; import java.util.LinkedList; @@ -40,7 +41,7 @@ public abstract class AbstractScaleFigure extends JPanel { protected static final Dimension borderThickness_px = new Dimension(DEFAULT_BORDER_PIXELS_WIDTH, DEFAULT_BORDER_PIXELS_HEIGHT); // pixel offset from the the subject's origin to the canvas's upper-left-corner. - protected Dimension originLocation_px = new Dimension(0,0); + protected Point originLocation_px = new Point(0,0); // size of the visible region protected Dimension visibleBounds_px = new Dimension(0,0); @@ -81,7 +82,7 @@ public abstract class AbstractScaleFigure extends JPanel { return scale; } - public Dimension getSubjectOrigin() { + public Point getSubjectOrigin() { return originLocation_px; } @@ -162,7 +163,7 @@ public abstract class AbstractScaleFigure extends JPanel { // Calculate and store the transformation used // (inverse is used in detecting clicks on objects) projection = new AffineTransform(); - projection.translate(this.originLocation_px.width, originLocation_px.height); + projection.translate(this.originLocation_px.x, originLocation_px.y); // Mirror position Y-axis upwards projection.scale(scale, -scale); } diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java b/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java index f5a522b10..8bdd0d224 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java @@ -2,9 +2,9 @@ package net.sf.openrocket.gui.scalefigure; import java.awt.BasicStroke; import java.awt.Color; -import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.Line2D; @@ -347,12 +347,12 @@ public class FinPointFigure extends AbstractScaleFigure { return p; } - public Dimension getSubjectOrigin() { + public Point getSubjectOrigin() { if (modID != finset.getRocket().getAerodynamicModID()) { modID = finset.getRocket().getAerodynamicModID(); updateTransform(); } - return new Dimension(originLocation_px.width, originLocation_px.height); + return new Point(originLocation_px.x, originLocation_px.y); } @Override @@ -383,12 +383,12 @@ public class FinPointFigure extends AbstractScaleFigure { final int finFrontPx = (int)(subjectBounds_m.getX()*scale); final int subjectHeight = (int)(subjectBounds_m.getHeight()*scale); - originLocation_px.width = borderThickness_px.width - finFrontPx; + originLocation_px.x = borderThickness_px.width - finFrontPx; if( visibleBounds_px.height > (subjectHeight+ 2*borderThickness_px.height)) { - originLocation_px.height = getHeight() - mountHeight - borderThickness_px.height; + originLocation_px.y = getHeight() - mountHeight - borderThickness_px.height; }else { - originLocation_px.height = borderThickness_px.height + finHeight; + originLocation_px.y = borderThickness_px.height + finHeight; } } diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java b/swing/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java index 306136219..80cdbbb4b 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java @@ -1,14 +1,7 @@ package net.sf.openrocket.gui.scalefigure; -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Rectangle; -import java.awt.RenderingHints; -import java.awt.Shape; +import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import java.awt.geom.NoninvertibleTransformException; @@ -361,7 +354,7 @@ public class RocketFigure extends AbstractScaleFigure { * Gets the shapes required to draw the component. * * @param component - * @param params + * * @return the ArrayList containing all the shapes to draw. */ private static ArrayList addThisShape( @@ -425,6 +418,7 @@ public class RocketFigure extends AbstractScaleFigure { final double maxR = Math.max(Math.hypot(newBounds.min.y, newBounds.min.z), Math.hypot(newBounds.max.y, newBounds.max.z)); + switch (currentViewType) { case SideView: subjectBounds_m = new Rectangle2D.Double(newBounds.min.x, -maxR, newBounds.span().x, 2 * maxR); @@ -451,12 +445,11 @@ public class RocketFigure extends AbstractScaleFigure { final int newOriginX = borderThickness_px.width + Math.max(getWidth(), subjectWidth + 2*borderThickness_px.width)/ 2; final int newOriginY = borderThickness_px.height + getHeight() / 2; - originLocation_px = new Dimension(newOriginX, newOriginY); + originLocation_px = new Point(newOriginX, newOriginY); }else if (currentViewType == RocketPanel.VIEW_TYPE.SideView){ final int newOriginX = borderThickness_px.width - subjectFront; final int newOriginY = Math.max(getHeight(), subjectHeight + 2*borderThickness_px.height )/ 2; - - originLocation_px = new Dimension(newOriginX, newOriginY); + originLocation_px = new Point(newOriginX, newOriginY); } } diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java b/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java index 3a16cf6e9..89da49ec3 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java @@ -6,6 +6,7 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.ComponentAdapter; @@ -34,7 +35,7 @@ import net.sf.openrocket.util.StateChangeListener; /** - * A scroll pane that holds a {@link ScaleFigure} and includes rulers that show + * A scroll pane that holds a {@link AbstractScaleFigure} and includes rulers that show * natural units. The figure can be moved by dragging on the figure. *

* This class implements both MouseListener and @@ -68,7 +69,6 @@ public class ScaleScrollPane extends JScrollPane * Create a scale scroll pane. * * @param component the component to contain (must implement ScaleFigure) - * @param allowFit whether automatic fitting of the figure is allowed */ public ScaleScrollPane(final JComponent component) { super(component); @@ -148,12 +148,12 @@ public class ScaleScrollPane extends JScrollPane this.fit = shouldFit; if (shouldFit) { validate(); - - Dimension view = viewport.getExtentSize(); - figure.scaleTo(view); - this.firePropertyChange( USER_SCALE_PROPERTY, 1.0, figure.getUserScale()); - revalidate(); + Dimension view = viewport.getExtentSize(); + figure.scaleTo(view); + + this.firePropertyChange( USER_SCALE_PROPERTY, 1.0, figure.getUserScale()); + revalidate(); } } @@ -286,23 +286,23 @@ public class ScaleScrollPane extends JScrollPane } private double fromPx(final int px) { - Dimension origin = figure.getSubjectOrigin(); + final Point origin = figure.getSubjectOrigin(); double realValue = Double.NaN; if (orientation == HORIZONTAL) { - realValue = px - origin.width; + realValue = px - origin.x; } else { - realValue = origin.height - px; + realValue = origin.y - px; } return realValue / figure.getAbsoluteScale(); } private int toPx(final double value) { - final Dimension origin = figure.getSubjectOrigin(); + final Point origin = figure.getSubjectOrigin(); final int px = (int) (value * figure.getAbsoluteScale() + 0.5); if (orientation == HORIZONTAL) { - return (px + origin.width); + return (px + origin.x); } else { - return (origin.height - px); + return (origin.y - px); } }