Some improvements!

This commit is contained in:
Andrew Lalis 2021-02-21 15:40:17 +01:00
parent a0a3b269b0
commit 7a49772540
4 changed files with 29 additions and 4 deletions

View File

@ -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<Relation> selectedRelations = this.model.getSelectedRelations();
this.model.getSelectedRelations().forEach(r -> r.setSelected(false));

View File

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

View File

@ -22,6 +22,8 @@ public class MappingModel implements Serializable, Viewable {
private transient Set<ModelChangeListener> 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<>();

View File

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