mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-13 06:49:36 -04:00
circuit is centered if opened
This commit is contained in:
parent
486224e2f0
commit
c4ebd114f4
@ -52,7 +52,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
|
||||
circuitComponent = new CircuitComponent(cr, library);
|
||||
String name = prefs.get("name", null);
|
||||
if (name != null) {
|
||||
loadFile(new File(name));
|
||||
SwingUtilities.invokeLater(() -> loadFile(new File(name)));
|
||||
}
|
||||
|
||||
getContentPane().add(circuitComponent);
|
||||
@ -199,6 +199,8 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
|
||||
|
||||
private void createAndStartModel() {
|
||||
try {
|
||||
if (modelDescription != null)
|
||||
modelDescription.highLight(null);
|
||||
circuitComponent.setModeAndReset(CircuitComponent.Mode.running);
|
||||
modelDescription = new ModelDescription(circuitComponent.getCircuit(), library);
|
||||
model = modelDescription.createModel();
|
||||
@ -228,7 +230,8 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
|
||||
private void loadFile(File filename) {
|
||||
XStream xStream = getxStream();
|
||||
try (FileReader in = new FileReader(filename)) {
|
||||
circuitComponent.setCircuit((Circuit) xStream.fromXML(in));
|
||||
Circuit circuit = (Circuit) xStream.fromXML(in);
|
||||
circuitComponent.setCircuit(circuit);
|
||||
setFilename(filename);
|
||||
} catch (Exception e) {
|
||||
new ErrorMessage("error writing a file").addCause(e).show();
|
||||
|
@ -33,9 +33,8 @@ public class CircuitComponent extends JComponent implements Observer {
|
||||
private Observer callOnManualChange;
|
||||
|
||||
public CircuitComponent(Circuit aCircuit, PartLibrary library) {
|
||||
this.circuit = aCircuit;
|
||||
this.library = library;
|
||||
setModeAndReset(Mode.part);
|
||||
setCircuit(aCircuit);
|
||||
|
||||
KeyStroke delKey = KeyStroke.getKeyStroke("DELETE");
|
||||
getInputMap().put(delKey, delAction);
|
||||
@ -151,6 +150,26 @@ public class CircuitComponent extends JComponent implements Observer {
|
||||
|
||||
public void setCircuit(Circuit circuit) {
|
||||
this.circuit = circuit;
|
||||
|
||||
GraphicMinMax gr = new GraphicMinMax();
|
||||
circuit.drawTo(gr);
|
||||
if (gr.getMin() != null) {
|
||||
|
||||
Vector delta = gr.getMax().sub(gr.getMin());
|
||||
double sx = ((double) getWidth()) / delta.x;
|
||||
double sy = ((double) getHeight()) / delta.y;
|
||||
double s = Math.min(sx, sy) * 0.8;
|
||||
|
||||
transform.setToScale(s, s); // set Scaling
|
||||
|
||||
Vector center = gr.getMin().add(gr.getMax()).div(2);
|
||||
transform.translate(-center.x, -center.y); // move drawing center to (0,0)
|
||||
|
||||
Vector dif = new Vector(getWidth(), getHeight()).div(2);
|
||||
transform.translate(dif.x / s, dif.y / s); // move drawing center to frame center
|
||||
} else
|
||||
transform = new AffineTransform();
|
||||
|
||||
setModeAndReset(Mode.part);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user