[refactor] Changed Dimension to Point display Figures

This commit is contained in:
Daniel_M_Williams 2020-07-03 16:51:01 -04:00
parent 6e2ab642e5
commit f0269b5d91
4 changed files with 28 additions and 34 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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 <code>ArrayList</code> containing all the shapes to draw.
*/
private static ArrayList<RocketComponentShape> 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);
}
}

View File

@ -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.
* <p>
* This class implements both <code>MouseListener</code> 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);
@ -149,11 +149,11 @@ public class ScaleScrollPane extends JScrollPane
if (shouldFit) {
validate();
Dimension view = viewport.getExtentSize();
figure.scaleTo(view);
this.firePropertyChange( USER_SCALE_PROPERTY, 1.0, figure.getUserScale());
Dimension view = viewport.getExtentSize();
figure.scaleTo(view);
revalidate();
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);
}
}