From 7a49772540f28c5e7c7e8428693456bd821cb40f Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 21 Feb 2021 15:40:17 +0100 Subject: [PATCH] Some improvements! --- .../control/actions/ExportToImageAction.java | 3 +++ .../control/actions/edits/AddRelationAction.java | 4 +++- .../nl/andrewlalis/erme/model/MappingModel.java | 16 ++++++++++++++++ .../nl/andrewlalis/erme/view/DiagramPanel.java | 10 +++++++--- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java index a9774be..8fb316b 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java @@ -3,6 +3,7 @@ package nl.andrewlalis.erme.control.actions; import lombok.Setter; import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.Relation; +import nl.andrewlalis.erme.view.DiagramPanel; import nl.andrewlalis.erme.view.view_models.MappingModelViewModel; import javax.imageio.ImageIO; @@ -84,6 +85,7 @@ public class ExportToImageAction extends AbstractAction { private BufferedImage renderModel() { BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bufferedImage.createGraphics(); + DiagramPanel.prepareGraphics(g2d); final Rectangle bounds = this.model.getViewModel().getBounds(g2d); BufferedImage outputImage = new BufferedImage(bounds.width, bounds.height + 20, BufferedImage.TYPE_INT_RGB); g2d = outputImage.createGraphics(); @@ -91,6 +93,7 @@ public class ExportToImageAction extends AbstractAction { g2d.fillRect(outputImage.getMinX(), outputImage.getMinY(), outputImage.getWidth(), outputImage.getHeight()); AffineTransform originalTransform = g2d.getTransform(); g2d.setTransform(AffineTransform.getTranslateInstance(-bounds.x, -bounds.y)); + DiagramPanel.prepareGraphics(g2d); List selectedRelations = this.model.getSelectedRelations(); this.model.getSelectedRelations().forEach(r -> r.setSelected(false)); diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java index 9f7c904..6e92623 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java @@ -39,7 +39,9 @@ public class AddRelationAction extends AbstractAction { JOptionPane.PLAIN_MESSAGE ); if (name != null) { - this.model.addRelation(new Relation(this.model, new Point(0, 0), name)); + Rectangle bounds = this.model.getRelationBounds(); + Point center = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); + this.model.addRelation(new Relation(this.model, center, name)); } } } diff --git a/src/main/java/nl/andrewlalis/erme/model/MappingModel.java b/src/main/java/nl/andrewlalis/erme/model/MappingModel.java index 328f9ad..3a37643 100644 --- a/src/main/java/nl/andrewlalis/erme/model/MappingModel.java +++ b/src/main/java/nl/andrewlalis/erme/model/MappingModel.java @@ -22,6 +22,8 @@ public class MappingModel implements Serializable, Viewable { private transient Set changeListeners; + private final static long serialVersionUID = 6153776381873250304L; + public MappingModel() { this.relations = new HashSet<>(); this.changeListeners = new HashSet<>(); @@ -70,6 +72,20 @@ public class MappingModel implements Serializable, Viewable { } } + public Rectangle getRelationBounds() { + int minX = Integer.MAX_VALUE; + int minY = Integer.MAX_VALUE; + int maxX = Integer.MIN_VALUE; + int maxY = Integer.MIN_VALUE; + for (Relation r : this.getRelations()) { + minX = Math.min(minX, r.getPosition().x); + minY = Math.min(minY, r.getPosition().y); + maxX = Math.max(maxX, r.getPosition().x); + maxY = Math.max(maxY, r.getPosition().y); + } + return new Rectangle(minX, minY, maxX - minX, maxY - minY); + } + public void addChangeListener(ModelChangeListener listener) { if (this.changeListeners == null) { this.changeListeners = new HashSet<>(); diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java index b5f10e8..8e805e2 100644 --- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java +++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java @@ -100,9 +100,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { public Graphics2D getGraphics2D(Graphics g) { Graphics2D g2d = (Graphics2D) g; - g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g.setFont(g.getFont().deriveFont(14.0f)); + prepareGraphics(g2d); return g2d; } @@ -130,4 +128,10 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { RemoveAttributeAction.getInstance().setModel(this.model); LoadSampleModelAction.getInstance().setDiagramPanel(this); } + + public static void prepareGraphics(Graphics2D g) { + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setFont(g.getFont().deriveFont(14.0f)); + } }