REFACTOR: position
This commit is contained in:
parent
ce7a41c535
commit
e91d1eadde
@ -26,7 +26,7 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final int P95 = (int) round(0.95 * RASTER);
|
public static final int P95 = (int) round(0.95 * RASTER);
|
||||||
|
|
||||||
public static final int JUNCTION_RADIUS = (int) round(0.05 * RASTER);
|
public static final int JUNCTION_RADIUS = (int) round(0.09 * RASTER);
|
||||||
|
|
||||||
public static final int JUNCTION_RADIUS_HOVER = (int) round(1.5 * JUNCTION_RADIUS);
|
public static final int JUNCTION_RADIUS_HOVER = (int) round(1.5 * JUNCTION_RADIUS);
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final Color PART_HOVER_COLOR = new Color(192, 192, 192, 128);
|
public static final Color PART_HOVER_COLOR = new Color(192, 192, 192, 128);
|
||||||
|
|
||||||
public static final Color HOVER_BORDER_COLOR = Color.BLACK;
|
public static final Color JUNCTION_HOVER_BORDER_COLOR = Color.BLACK;
|
||||||
|
|
||||||
public static final Color RASTER_COLOR = Color.gray;
|
public static final Color RASTER_COLOR = Color.gray;
|
||||||
|
|
||||||
@ -44,6 +44,8 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final Color VOLTAGE_LOW_COLOR = new Color(0, 128, 255);
|
public static final Color VOLTAGE_LOW_COLOR = new Color(0, 128, 255);
|
||||||
|
|
||||||
|
public static final Color WIRE_HOVER_COLOR_BACK = Color.BLACK;
|
||||||
|
|
||||||
public static final Font LABEL_FONT = new Font("", Font.PLAIN, 20);
|
public static final Font LABEL_FONT = new Font("", Font.PLAIN, 20);
|
||||||
|
|
||||||
public static final BasicStroke RASTER_STROKE = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{5}, 0);
|
public static final BasicStroke RASTER_STROKE = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{5}, 0);
|
||||||
|
|||||||
@ -1,24 +1,18 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.PartDto;
|
import de.ph87.electro.circuit.part.PartDto;
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
import de.ph87.electro.circuit.part.Position;
|
||||||
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
|
||||||
import static de.ph87.electro.common.MathHelpers.div;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class Circuit {
|
public class Circuit {
|
||||||
|
|
||||||
@ -41,36 +35,6 @@ public class Circuit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartBattery addBattery(final String name, final int x, final int y, final Orientation orientation, final double voltage) {
|
|
||||||
final PartBattery battery = new PartBattery(name, new Point(x, y), orientation, voltage);
|
|
||||||
partAdd(battery);
|
|
||||||
return battery;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartLight addLight(final String name, final int x, final int y, final Orientation orientation, final double minVoltage, final double maxVoltage) {
|
|
||||||
final PartLight light = new PartLight(name, new Point(x, y), orientation, minVoltage, maxVoltage);
|
|
||||||
partAdd(light);
|
|
||||||
return light;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
|
||||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, new Point(x, y), orientation, state);
|
|
||||||
partAdd(switch1x1);
|
|
||||||
return switch1x1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
|
||||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, new Point(x, y), orientation, state);
|
|
||||||
partAdd(switch1x2);
|
|
||||||
return switch1x2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
|
||||||
final PartSwitchCross switchCross = new PartSwitchCross(name, new Point(x, y), orientation, state);
|
|
||||||
partAdd(switchCross);
|
|
||||||
return switchCross;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(final Junction a, final Junction b) {
|
public void connect(final Junction a, final Junction b) {
|
||||||
wires.add(new Wire(a, b));
|
wires.add(new Wire(a, b));
|
||||||
evaluateAndRender();
|
evaluateAndRender();
|
||||||
@ -90,7 +54,7 @@ public class Circuit {
|
|||||||
return wires.stream();
|
return wires.stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void partAdd(final Part part) {
|
public <T extends Part> T addPart(final T part) {
|
||||||
if (parts.contains(part)) {
|
if (parts.contains(part)) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
@ -98,18 +62,19 @@ public class Circuit {
|
|||||||
parts.add(part);
|
parts.add(part);
|
||||||
part.render();
|
part.render();
|
||||||
}
|
}
|
||||||
|
return part;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void partMove(final Part part, final Point newPosition) {
|
public void movePart(final Part part, final Position position) {
|
||||||
if (!parts.contains(part)) {
|
if (!parts.contains(part)) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
if (isFree(newPosition)) {
|
if (isFree(position)) {
|
||||||
part.setPosition(newPosition);
|
part.setPosition(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFree(final Point position) {
|
public boolean isFree(final Position position) {
|
||||||
return parts.stream().noneMatch(part -> part.getPosition().equals(position));
|
return parts.stream().noneMatch(part -> part.getPosition().equals(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,21 +86,20 @@ public class Circuit {
|
|||||||
return parts.size();
|
return parts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Part> findPart(final Point point) {
|
public Optional<Part> findPartByPosition(final Position position) {
|
||||||
final Point cell = div(point, RASTER);
|
return streamParts().filter(p -> p.getPosition().raster.equals(position.raster)).findFirst();
|
||||||
return streamParts().filter(p -> p.getPosition().equals(cell)).findFirst();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByPoint(final Point point) {
|
public Optional<Junction> findJunctionByAbsolute(final Position position) {
|
||||||
return findPart(point).flatMap(part -> part.findJunction(point));
|
return findPartByPosition(position).flatMap(part -> part.findJunctionByPosition(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByUuid(@NonNull final String junctionUuid) {
|
public Optional<Junction> findJunctionByUuid(@NonNull final String junctionUuid) {
|
||||||
return parts.stream().map(part -> part.findJunctionByUuid(junctionUuid)).filter(Optional::isPresent).map(Optional::get).findFirst();
|
return parts.stream().map(part -> part.findJunctionByUuid(junctionUuid)).filter(Optional::isPresent).map(Optional::get).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Wire> findWireByPoint(final Point point) {
|
public Optional<Wire> findWireByPosition(final Position position) {
|
||||||
return wires.stream().filter(wire -> wire.intersects(point)).findFirst();
|
return wires.stream().filter(wire -> wire.intersects(position)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeWire(final Wire wire) {
|
public void removeWire(final Wire wire) {
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import de.ph87.electro.circuit.part.Position;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class CircuitPanel extends JPanel {
|
public class CircuitPanel extends JPanel {
|
||||||
|
|
||||||
private final Circuit circuit = new Circuit();
|
private final Circuit circuit = new Circuit();
|
||||||
@ -27,6 +26,7 @@ public class CircuitPanel extends JPanel {
|
|||||||
drawParts(g);
|
drawParts(g);
|
||||||
drawRaster(g, w, h);
|
drawRaster(g, w, h);
|
||||||
drawWires(g);
|
drawWires(g);
|
||||||
|
mouseAdapter.drawHover(g);
|
||||||
mouseAdapter.drawDrag(g);
|
mouseAdapter.drawDrag(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,12 +52,11 @@ public class CircuitPanel extends JPanel {
|
|||||||
|
|
||||||
private void drawWires(final Graphics2D g) {
|
private void drawWires(final Graphics2D g) {
|
||||||
circuit.streamWires().forEach(wire -> {
|
circuit.streamWires().forEach(wire -> {
|
||||||
final Point a = wire.getA().getAbsolute();
|
final Position a = wire.getA().getPosition();
|
||||||
final Point b = wire.getB().getAbsolute();
|
final Position b = wire.getB().getPosition();
|
||||||
|
|
||||||
g.setColor(wire.getA().getColor());
|
g.setColor(wire.getA().getColor());
|
||||||
g.setStroke(WIRE_STROKE);
|
g.setStroke(WIRE_STROKE);
|
||||||
g.drawLine(a.x, a.y, b.x, b.y);
|
g.drawLine(a.absolute.x, a.absolute.y, b.absolute.x, b.absolute.y);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,12 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
import de.ph87.electro.circuit.part.parts.*;
|
||||||
import de.ph87.electro.common.AbstractDropTarget;
|
import de.ph87.electro.common.AbstractDropTarget;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
|
||||||
import static de.ph87.electro.common.MathHelpers.div;
|
|
||||||
|
|
||||||
public class CircuitPanelDropTarget extends AbstractDropTarget {
|
public class CircuitPanelDropTarget extends AbstractDropTarget {
|
||||||
|
|
||||||
private final CircuitPanel circuitPanel;
|
private final CircuitPanel circuitPanel;
|
||||||
@ -24,23 +22,23 @@ public class CircuitPanelDropTarget extends AbstractDropTarget {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean drop(final Point point, final String data) {
|
protected boolean drop(final Point point, final String data) {
|
||||||
final Point raster = div(point, RASTER);
|
final Position position = new Position(point);
|
||||||
if (data.equals(PartBattery.class.getSimpleName())) {
|
if (data.equals(PartBattery.class.getSimpleName())) {
|
||||||
addPart(new PartBattery(raster));
|
addPart(new PartBattery(position));
|
||||||
} else if (data.equals(PartJunctionCorner.class.getSimpleName())) {
|
} else if (data.equals(PartJunctionCorner.class.getSimpleName())) {
|
||||||
addPart(new PartJunctionCorner(raster));
|
addPart(new PartJunctionCorner(position));
|
||||||
} else if (data.equals(PartJunctionEdge.class.getSimpleName())) {
|
} else if (data.equals(PartJunctionEdge.class.getSimpleName())) {
|
||||||
addPart(new PartJunctionEdge(raster));
|
addPart(new PartJunctionEdge(position));
|
||||||
} else if (data.equals(PartJunctionMiddle.class.getSimpleName())) {
|
} else if (data.equals(PartJunctionMiddle.class.getSimpleName())) {
|
||||||
addPart(new PartJunctionMiddle(raster));
|
addPart(new PartJunctionMiddle(position));
|
||||||
} else if (data.equals(PartLight.class.getSimpleName())) {
|
} else if (data.equals(PartLight.class.getSimpleName())) {
|
||||||
addPart(new PartLight(raster));
|
addPart(new PartLight(position));
|
||||||
} else if (data.equals(PartSwitch1x1.class.getSimpleName())) {
|
} else if (data.equals(PartSwitch1x1.class.getSimpleName())) {
|
||||||
addPart(new PartSwitch1x1(raster));
|
addPart(new PartSwitch1x1(position));
|
||||||
} else if (data.equals(PartSwitch1x2.class.getSimpleName())) {
|
} else if (data.equals(PartSwitch1x2.class.getSimpleName())) {
|
||||||
addPart(new PartSwitch1x2(raster));
|
addPart(new PartSwitch1x2(position));
|
||||||
} else if (data.equals(PartSwitchCross.class.getSimpleName())) {
|
} else if (data.equals(PartSwitchCross.class.getSimpleName())) {
|
||||||
addPart(new PartSwitchCross(raster));
|
addPart(new PartSwitchCross(position));
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
@ -49,7 +47,7 @@ public class CircuitPanelDropTarget extends AbstractDropTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addPart(final Part part) {
|
public void addPart(final Part part) {
|
||||||
circuit.partAdd(part);
|
circuit.addPart(part);
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package de.ph87.electro.circuit;
|
|||||||
|
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import de.ph87.electro.circuit.part.Position;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
@ -10,11 +10,9 @@ import java.awt.event.MouseEvent;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static de.ph87.electro.common.MathHelpers.div;
|
|
||||||
import static java.awt.event.MouseEvent.BUTTON1;
|
import static java.awt.event.MouseEvent.BUTTON1;
|
||||||
import static java.awt.event.MouseEvent.BUTTON3;
|
import static java.awt.event.MouseEvent.BUTTON3;
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
class CircuitPanelMouseAdapter extends MouseAdapter {
|
class CircuitPanelMouseAdapter extends MouseAdapter {
|
||||||
|
|
||||||
private final CircuitPanel circuitPanel;
|
private final CircuitPanel circuitPanel;
|
||||||
@ -31,7 +29,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
|
|
||||||
private Junction junctionDrag = null;
|
private Junction junctionDrag = null;
|
||||||
|
|
||||||
private Point dragPosition = null;
|
private Position dragPosition = null;
|
||||||
|
|
||||||
CircuitPanelMouseAdapter(final CircuitPanel circuitPanel, final Circuit circuit) {
|
CircuitPanelMouseAdapter(final CircuitPanel circuitPanel, final Circuit circuit) {
|
||||||
this.circuitPanel = circuitPanel;
|
this.circuitPanel = circuitPanel;
|
||||||
@ -41,9 +39,11 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(final MouseEvent e) {
|
public void mouseClicked(final MouseEvent event) {
|
||||||
if (e.getButton() == BUTTON3) {
|
final Position position = new Position(event);
|
||||||
final Optional<Wire> wireOptional = circuit.findWireByPoint(e.getPoint());
|
|
||||||
|
if (event.getButton() == BUTTON3) {
|
||||||
|
final Optional<Wire> wireOptional = circuit.findWireByPosition(position);
|
||||||
if (wireOptional.isPresent()) {
|
if (wireOptional.isPresent()) {
|
||||||
circuit.removeWire(wireOptional.get());
|
circuit.removeWire(wireOptional.get());
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
@ -51,9 +51,8 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final Point cell = div(e.getPoint(), RASTER);
|
final Optional<Part> partOptional = circuit.findPartByPosition(position);
|
||||||
final Optional<Part> partOptional = circuit.streamParts().filter(p -> p.getPosition().equals(cell)).findFirst();
|
switch (event.getButton()) {
|
||||||
switch (e.getButton()) {
|
|
||||||
case BUTTON1:
|
case BUTTON1:
|
||||||
partOptional.ifPresent(Part::action);
|
partOptional.ifPresent(Part::action);
|
||||||
circuit.evaluateAndRender();
|
circuit.evaluateAndRender();
|
||||||
@ -67,18 +66,19 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(final MouseEvent e) {
|
public void mouseMoved(final MouseEvent event) {
|
||||||
findHover(e.getPoint());
|
findHover(new Position(event));
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(final MouseEvent event) {
|
public void mousePressed(final MouseEvent event) {
|
||||||
circuit.findPart(event.getPoint()).ifPresent(part -> startPartOrJunction(part, event.getPoint()));
|
final Position position = new Position(event);
|
||||||
|
circuit.findPartByPosition(position).ifPresent(part -> startPartOrJunction(part, position));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startPartOrJunction(final Part part, final Point point) {
|
private void startPartOrJunction(final Part part, final Position position) {
|
||||||
part.findJunction(point).ifPresentOrElse(junction -> {
|
part.findJunctionByPosition(position).ifPresentOrElse(junction -> {
|
||||||
partDrag = null;
|
partDrag = null;
|
||||||
junctionDrag = junction;
|
junctionDrag = junction;
|
||||||
}, () -> {
|
}, () -> {
|
||||||
@ -88,23 +88,24 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(final MouseEvent e) {
|
public void mouseDragged(final MouseEvent event) {
|
||||||
findHover(e.getPoint());
|
final Position position = new Position(event);
|
||||||
|
findHover(position);
|
||||||
if (partDrag != null || junctionDrag != null) {
|
if (partDrag != null || junctionDrag != null) {
|
||||||
dragPosition = e.getPoint();
|
dragPosition = position;
|
||||||
}
|
}
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findHover(final Point point) {
|
private void findHover(final Position position) {
|
||||||
partHover = circuit.findPart(point).orElse(null);
|
partHover = circuit.findPartByPosition(position).orElse(null);
|
||||||
junctionHover = partHover != null ? partHover.findJunction(point).orElse(null) : null;
|
junctionHover = partHover != null ? partHover.findJunctionByPosition(position).orElse(null) : null;
|
||||||
if (junctionHover != null) {
|
if (junctionHover != null) {
|
||||||
partHover = null;
|
partHover = null;
|
||||||
wireHover = null;
|
wireHover = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
wireHover = circuit.findWireByPoint(point).orElse(null);
|
wireHover = circuit.findWireByPosition(position).orElse(null);
|
||||||
if (wireHover != null) {
|
if (wireHover != null) {
|
||||||
partHover = null;
|
partHover = null;
|
||||||
}
|
}
|
||||||
@ -112,19 +113,13 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseReleased(final MouseEvent event) {
|
public void mouseReleased(final MouseEvent event) {
|
||||||
if (partDrag != null && junctionDrag != null) {
|
final Position position = new Position(event);
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if ((partDrag != null || junctionDrag != null) == (dragPosition == null)) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (partDrag != null) {
|
if (partDrag != null) {
|
||||||
circuit.partMove(partDrag, div(event.getPoint(), RASTER));
|
circuit.movePart(partDrag, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (junctionDrag != null) {
|
if (junctionDrag != null) {
|
||||||
circuit.findJunctionByPoint(event.getPoint()).filter(destination -> destination != junctionDrag).ifPresent(destination -> circuit.connect(junctionDrag, destination));
|
circuit.findJunctionByAbsolute(position).filter(destination -> destination != junctionDrag).ifPresent(destination -> circuit.connect(junctionDrag, destination));
|
||||||
}
|
}
|
||||||
|
|
||||||
partDrag = null;
|
partDrag = null;
|
||||||
@ -133,26 +128,51 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawDrag(final Graphics2D g) {
|
public void drawHover(final Graphics2D g) {
|
||||||
if (partHover != null) {
|
if (partHover != null) {
|
||||||
final Point zero = partHover.translate(new Point());
|
g.setColor(PART_HOVER_COLOR);
|
||||||
partHover.rect(g, zero.x, zero.y, RASTER, RASTER, HOVER_BORDER_COLOR, HOVER_STROKE, null);
|
g.setStroke(HOVER_STROKE);
|
||||||
|
g.drawRect(partHover.getPosition().absolute.x, partHover.getPosition().absolute.y, RASTER, RASTER);
|
||||||
}
|
}
|
||||||
if (junctionHover != null) {
|
if (junctionHover != null) {
|
||||||
junctionHover.getOwner().circle(g, junctionHover.getAbsolute().x, junctionHover.getAbsolute().y, JUNCTION_RADIUS_HOVER, HOVER_BORDER_COLOR, HOVER_STROKE, junctionHover.getColor());
|
g.setColor(junctionHover.getColor());
|
||||||
|
g.fillArc(junctionHover.getPosition().absolute.x - JUNCTION_RADIUS_HOVER, junctionHover.getPosition().absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
||||||
|
|
||||||
|
g.setColor(JUNCTION_HOVER_BORDER_COLOR);
|
||||||
|
g.setStroke(HOVER_STROKE);
|
||||||
|
g.drawArc(junctionHover.getPosition().absolute.x - JUNCTION_RADIUS_HOVER, junctionHover.getPosition().absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
||||||
}
|
}
|
||||||
if (wireHover != null) {
|
if (wireHover != null) {
|
||||||
wireHover.getA().getOwner().line(g, wireHover.getA().getAbsolute(), wireHover.getB().getAbsolute(), HOVER_BORDER_COLOR, WIRE_HOVER_STROKE_BACK);
|
g.setColor(WIRE_HOVER_COLOR_BACK);
|
||||||
wireHover.getA().getOwner().line(g, wireHover.getA().getAbsolute(), wireHover.getB().getAbsolute(), wireHover.getA().getColor(), WIRE_HOVER_STROKE);
|
g.setStroke(WIRE_HOVER_STROKE_BACK);
|
||||||
|
g.drawLine(
|
||||||
|
wireHover.getA().getPosition().absolute.x,
|
||||||
|
wireHover.getA().getPosition().absolute.y,
|
||||||
|
wireHover.getB().getPosition().absolute.x,
|
||||||
|
wireHover.getB().getPosition().absolute.y
|
||||||
|
);
|
||||||
|
|
||||||
|
g.setColor(wireHover.getA().getColor());
|
||||||
|
g.setStroke(WIRE_HOVER_STROKE);
|
||||||
|
g.drawLine(
|
||||||
|
wireHover.getA().getPosition().absolute.x,
|
||||||
|
wireHover.getA().getPosition().absolute.y,
|
||||||
|
wireHover.getB().getPosition().absolute.x,
|
||||||
|
wireHover.getB().getPosition().absolute.y
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void drawDrag(final Graphics2D g) {
|
||||||
if (dragPosition != null) {
|
if (dragPosition != null) {
|
||||||
if (partDrag != null) {
|
if (partDrag != null) {
|
||||||
g.setColor(PART_HOVER_COLOR);
|
g.setColor(PART_HOVER_COLOR);
|
||||||
g.fillRect(dragPosition.x - P50, dragPosition.y - P50, RASTER, RASTER);
|
g.fillRect(dragPosition.absolute.x - P50, dragPosition.absolute.y - P50, RASTER, RASTER);
|
||||||
}
|
}
|
||||||
if (junctionDrag != null) {
|
if (junctionDrag != null) {
|
||||||
junctionDrag.getOwner().line(g, junctionDrag.getAbsolute(), dragPosition, junctionDrag.getColor(), WIRE_STROKE);
|
g.setColor(junctionDrag.getColor());
|
||||||
|
g.setStroke(WIRE_STROKE);
|
||||||
|
g.drawLine(junctionDrag.getPosition().absolute.x, junctionDrag.getPosition().absolute.y, dragPosition.absolute.x, dragPosition.absolute.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,10 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.WIRE_HOVER_STROKE_BACK;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Wire {
|
public class Wire {
|
||||||
|
|
||||||
@ -24,21 +21,8 @@ public class Wire {
|
|||||||
b.getWires().add(this);
|
b.getWires().add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean intersects(final Point p) {
|
public boolean intersects(final Position position) {
|
||||||
final Point start = a.getAbsolute();
|
return position.distanceToLine(a.getPosition(), b.getPosition());
|
||||||
final Point end = b.getAbsolute();
|
|
||||||
double dx = end.x - start.x;
|
|
||||||
double dy = end.y - start.y;
|
|
||||||
double length = dx * dx + dy * dy;
|
|
||||||
if (length == 0) {
|
|
||||||
return p.distance(start) <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
|
||||||
}
|
|
||||||
|
|
||||||
double t = ((p.x - start.x) * dx + (p.y - start.y) * dy) / length;
|
|
||||||
t = Math.max(0, Math.min(1, t));
|
|
||||||
double closestX = start.x + t * dx;
|
|
||||||
double closestY = start.y + t * dy;
|
|
||||||
return p.distance(closestX, closestY) <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction getOpposite(final Junction junction) {
|
public Junction getOpposite(final Junction junction) {
|
||||||
|
|||||||
@ -26,7 +26,9 @@ public class Junction {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private final Point position;
|
private final Point relative;
|
||||||
|
|
||||||
|
private Position position;
|
||||||
|
|
||||||
private final Set<Wire> wires = new HashSet<>();
|
private final Set<Wire> wires = new HashSet<>();
|
||||||
|
|
||||||
@ -42,18 +44,20 @@ public class Junction {
|
|||||||
return wires.stream().map(wire -> wire.getOpposite(this)).map(Junction::getUuid).toList();
|
return wires.stream().map(wire -> wire.getOpposite(this)).map(Junction::getUuid).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final String name, final Point position) {
|
public Junction(final Part owner, final String name, final Point relative) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.position = position;
|
this.relative = relative;
|
||||||
|
updatePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final JunctionDto dto, final Point position) {
|
public Junction(final Part owner, final JunctionDto dto, final Point relative) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.position = position;
|
this.relative = relative;
|
||||||
|
updatePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
@ -83,20 +87,16 @@ public class Junction {
|
|||||||
throw new ShortCircuit();
|
throw new ShortCircuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(final Graphics2D g) {
|
public void render(final Render g) {
|
||||||
owner.circle(g, position.x, position.y, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color);
|
g.circle(position, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getAbsolute() {
|
public void updatePosition() {
|
||||||
return owner.translate(getRotated());
|
position = owner.getPosition().plus(owner.getOrientation().rotate(relative));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Point getRotated() {
|
public boolean intersects(final Position position) {
|
||||||
return owner.getOrientation().rotate(position);
|
return this.position.distance(position) <= JUNCTION_RADIUS_HOVER;
|
||||||
}
|
|
||||||
|
|
||||||
public boolean intersects(final Point absolute) {
|
|
||||||
return getAbsolute().distance(absolute) <= JUNCTION_RADIUS_HOVER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +1,27 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static java.lang.Math.PI;
|
|
||||||
|
|
||||||
@SuppressWarnings("SuspiciousNameCombination")
|
@SuppressWarnings("SuspiciousNameCombination")
|
||||||
public enum Orientation {
|
public enum Orientation {
|
||||||
R0(0, p -> new Point(p.x, p.y)),
|
R0(0, p -> new Point(p.x, p.y)),
|
||||||
R90(1, p -> new Point(RASTER - p.y, p.x)),
|
R90(90, p -> new Point(RASTER - p.y, p.x)),
|
||||||
R180(2, p -> new Point(RASTER - p.x, RASTER - p.y)),
|
R180(180, p -> new Point(RASTER - p.x, RASTER - p.y)),
|
||||||
R270(3, p -> new Point(p.y, RASTER - p.x)),
|
R270(270, p -> new Point(p.y, RASTER - p.x)),
|
||||||
;
|
;
|
||||||
|
|
||||||
private final Function<Point, Point> map;
|
private final Function<Point, Point> map;
|
||||||
|
|
||||||
private final int deg90;
|
@Getter
|
||||||
|
private final int degrees;
|
||||||
|
|
||||||
Orientation(final int deg90, final Function<Point, Point> rotate) {
|
Orientation(final int degrees, final Function<Point, Point> rotate) {
|
||||||
this.deg90 = deg90;
|
this.degrees = degrees;
|
||||||
this.map = rotate;
|
this.map = rotate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,8 +33,8 @@ public enum Orientation {
|
|||||||
return values()[(ordinal() + 1) % values().length];
|
return values()[(ordinal() + 1) % values().length];
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getTheta() {
|
public double getRadians() {
|
||||||
return PI / 2 * deg90;
|
return degrees * Math.PI / 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,15 +7,14 @@ import lombok.Setter;
|
|||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.geom.Rectangle2D;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.PART_BACKGROUND;
|
||||||
import static java.lang.Math.round;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
import static de.ph87.electro.circuit.part.Position.ZERO;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(onlyExplicitlyIncluded = true)
|
@ToString(onlyExplicitlyIncluded = true)
|
||||||
@ -28,26 +27,25 @@ public abstract class Part {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Setter
|
private Position position;
|
||||||
private Point position;
|
|
||||||
|
|
||||||
private Orientation orientation;
|
private Orientation orientation;
|
||||||
|
|
||||||
private final List<Junction> junctions = new ArrayList<>();
|
private final List<Junction> junctions = new ArrayList<>();
|
||||||
|
|
||||||
private final BufferedImage render = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
protected final Render render = new Render();
|
||||||
|
|
||||||
protected Part(final String name, final Point position, final Orientation orientation) {
|
protected Part(final String name, final Position position, final Orientation orientation) {
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.position = position;
|
setPosition(position);
|
||||||
this.orientation = orientation;
|
this.orientation = orientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Part(final PartDto dto) {
|
protected Part(final PartDto dto) {
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.position = dto.getPosition();
|
setPosition(dto.getPosition());
|
||||||
this.orientation = dto.getOrientation();
|
this.orientation = dto.getOrientation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,107 +62,44 @@ public abstract class Part {
|
|||||||
return junction;
|
return junction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPosition(final Position position) {
|
||||||
|
this.position = position.alignToRaster();
|
||||||
|
junctions.forEach(Junction::updatePosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clockwise() {
|
||||||
|
orientation = orientation.clockwise();
|
||||||
|
junctions.forEach(Junction::updatePosition);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
junctions.forEach(Junction::reset);
|
junctions.forEach(Junction::reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render() {
|
public void render() {
|
||||||
final Graphics2D g = (Graphics2D) render.getGraphics();
|
render.rect(ZERO, RASTER, RASTER, null, null, PART_BACKGROUND);
|
||||||
g.rotate(orientation.getTheta(), P50, P50);
|
|
||||||
rect(g, 0, 0, RASTER, RASTER, null, null, PART_BACKGROUND);
|
_labels();
|
||||||
if (getOrientation() == Orientation.R180) {
|
render.resetTransform();
|
||||||
g.rotate(getOrientation().getTheta(), P50, P50);
|
|
||||||
}
|
_render();
|
||||||
_labels(g);
|
render.resetTransform();
|
||||||
if (getOrientation() == Orientation.R180) {
|
|
||||||
g.rotate(-getOrientation().getTheta(), P50, P50);
|
junctions.forEach(junction -> junction.render(render));
|
||||||
}
|
render.resetTransform();
|
||||||
_render(g);
|
|
||||||
junctions.forEach(junction -> junction.render(g));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(final Graphics2D g) {
|
public void paint(final Graphics2D g) {
|
||||||
g.drawImage(render, position.x * RASTER, position.y * RASTER, null);
|
g.drawImage(render.getImage(), position.absolute.x, position.absolute.y, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clockwise() {
|
public Optional<Junction> findJunctionByUuid(final String junctionUuid) {
|
||||||
orientation = orientation.clockwise();
|
return junctions.stream().filter(junction -> junction.getUuid().equals(junctionUuid)).findFirst();
|
||||||
render();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void line(final Graphics2D g, final Junction junction0, final Junction junction1, final Color color, final BasicStroke stroke) {
|
public Optional<Junction> findJunctionByPosition(final Position position) {
|
||||||
line(g, junction0.getPosition().x, junction0.getPosition().y, junction1.getPosition().x, junction1.getPosition().y, color, stroke);
|
return junctions.stream().filter(junction -> junction.intersects(position)).findFirst();
|
||||||
}
|
|
||||||
|
|
||||||
public void line(final Graphics2D g, final Point p0, final Point p1, final Color color, final BasicStroke stroke) {
|
|
||||||
line(g, p0.x, p0.y, p1.x, p1.y, color, stroke);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void line(final Graphics2D g, final Junction junction0, final double x1, final double y1, final Color color, final BasicStroke stroke) {
|
|
||||||
line(g, junction0.getPosition().x, junction0.getPosition().y, x1, y1, color, stroke);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void line(final Graphics2D g, final double x0, final double y0, final double x1, final double y1, final Color color, final Stroke stroke) {
|
|
||||||
final int _x0 = (int) round(position.x + x0);
|
|
||||||
final int _y0 = (int) round(position.y + y0);
|
|
||||||
final int _x1 = (int) round(position.x + x1);
|
|
||||||
final int _y1 = (int) round(position.y + y1);
|
|
||||||
if (color != null) {
|
|
||||||
g.setColor(color);
|
|
||||||
}
|
|
||||||
if (stroke != null) {
|
|
||||||
g.setStroke(stroke);
|
|
||||||
}
|
|
||||||
g.drawLine(_x0, _y0, _x1, _y1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rect(final Graphics2D g, final double x, final double y, final double w, final double h, final Color border, final Stroke stroke, final Color fill) {
|
|
||||||
final int _x = (int) round(position.x + x);
|
|
||||||
final int _y = (int) round(position.y + y);
|
|
||||||
final int _w = (int) round(w);
|
|
||||||
final int _h = (int) round(h);
|
|
||||||
if (fill != null) {
|
|
||||||
g.setColor(fill);
|
|
||||||
g.fillRect(_x, _y, _w, _h);
|
|
||||||
}
|
|
||||||
if (border != null && stroke != null) {
|
|
||||||
g.setColor(border);
|
|
||||||
g.setStroke(stroke);
|
|
||||||
g.drawRect(_x, _y, _w, _h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void circle(final Graphics2D g, final double x, final double y, final double radius, final Color border, final Stroke stroke, final Color fill) {
|
|
||||||
final int _x = (int) round(position.x + x - radius);
|
|
||||||
final int _y = (int) round(position.y + y - radius);
|
|
||||||
final int diameter = (int) round(radius * 2);
|
|
||||||
if (fill != null) {
|
|
||||||
g.setColor(fill);
|
|
||||||
g.fillArc(_x, _y, diameter, diameter, 0, 360);
|
|
||||||
}
|
|
||||||
if (border != null && stroke != null) {
|
|
||||||
g.setColor(border);
|
|
||||||
g.setStroke(stroke);
|
|
||||||
g.drawArc(_x, _y, diameter, diameter, 0, 360);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void textCenter(final Graphics2D g, final Font font, final String string, final int y, final Color color) {
|
|
||||||
g.setFont(font);
|
|
||||||
g.setColor(color);
|
|
||||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
|
||||||
g.drawString(string, (int) round(RASTER - bounds.getWidth()) / 2, y + g.getFont().getSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point translate(final Point p) {
|
|
||||||
return new Point(
|
|
||||||
p.x + position.x * RASTER,
|
|
||||||
p.y + position.y * RASTER
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Junction> findJunction(final Point absolute) {
|
|
||||||
return junctions.stream().filter(junction -> junction.intersects(absolute)).findFirst();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void action() {
|
public void action() {
|
||||||
@ -175,11 +110,11 @@ public abstract class Part {
|
|||||||
// may be overwritten
|
// may be overwritten
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
// may be overwritten
|
// may be overwritten
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void _labels(final Graphics2D g) {
|
protected void _labels() {
|
||||||
// may be overwritten
|
// may be overwritten
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +132,4 @@ public abstract class Part {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByUuid(final String junctionUuid) {
|
|
||||||
return junctions.stream().filter(junction -> junction.getUuid().equals(junctionUuid)).findFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import lombok.Getter;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -19,7 +18,7 @@ public class PartDto {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private Point position;
|
private Position position;
|
||||||
|
|
||||||
private Orientation orientation;
|
private Orientation orientation;
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public abstract class PartOther extends Part {
|
public abstract class PartOther extends Part {
|
||||||
|
|
||||||
protected PartOther(final String name, final Point position, final Orientation orientation) {
|
protected PartOther(final String name, final Position position, final Orientation orientation) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
77
src/main/java/de/ph87/electro/circuit/part/Position.java
Normal file
77
src/main/java/de/ph87/electro/circuit/part/Position.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
import static de.ph87.electro.CONFIG.WIRE_HOVER_STROKE_BACK;
|
||||||
|
import static java.lang.Math.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
public final class Position {
|
||||||
|
|
||||||
|
public static final Position ZERO = new Position(0, 0);
|
||||||
|
|
||||||
|
public final Point absolute;
|
||||||
|
|
||||||
|
public final Point inside;
|
||||||
|
|
||||||
|
public final Point raster;
|
||||||
|
|
||||||
|
public Position(final int x, final int y) {
|
||||||
|
this.absolute = new Point(x, y);
|
||||||
|
this.raster = new Point(x / RASTER, y / RASTER);
|
||||||
|
this.inside = new Point(x % RASTER, y % RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return absolute.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position(final double x, final double y) {
|
||||||
|
this((int) round(x), (int) round(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position(final Point point) {
|
||||||
|
this(point.x, point.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position(final MouseEvent event) {
|
||||||
|
this(event.getX(), event.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position plus(final Point vector) {
|
||||||
|
return new Position(absolute.x + vector.x, absolute.y + vector.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double distance(final Position position) {
|
||||||
|
return sqrt(pow(position.absolute.x - absolute.x, 2) + pow(position.absolute.y - absolute.y, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean distanceToLine(final Position lineStart, final Position lineEnd) {
|
||||||
|
double dx = lineEnd.absolute.x - lineStart.absolute.x;
|
||||||
|
double dy = lineEnd.absolute.y - lineStart.absolute.y;
|
||||||
|
double lineLength = dx * dx + dy * dy;
|
||||||
|
if (lineLength == 0) {
|
||||||
|
return distance(lineStart) <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
double t = ((absolute.x - lineStart.absolute.x) * dx + (absolute.y - lineStart.absolute.y) * dy) / lineLength;
|
||||||
|
t = Math.max(0, Math.min(1, t));
|
||||||
|
|
||||||
|
double closestX = lineStart.absolute.x + t * dx;
|
||||||
|
double closestY = lineStart.absolute.y + t * dy;
|
||||||
|
final double distance = sqrt(pow(closestX - absolute.x, 2) + pow(closestY - absolute.y, 2));
|
||||||
|
return distance <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position alignToRaster() {
|
||||||
|
return new Position(raster.x * RASTER, raster.y * RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
79
src/main/java/de/ph87/electro/circuit/part/Render.java
Normal file
79
src/main/java/de/ph87/electro/circuit/part/Render.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.awt.geom.Rectangle2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.P50;
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
|
public class Render {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final BufferedImage image = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
|
||||||
|
private final Graphics2D g = image.createGraphics();
|
||||||
|
|
||||||
|
public void line(final Junction junction0, final Junction junction1, final Color color, final BasicStroke stroke) {
|
||||||
|
line(junction0.getPosition(), junction1.getPosition(), color, stroke);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void line(final Position p0, final Position p1, final Color color, final BasicStroke stroke) {
|
||||||
|
if (color != null) {
|
||||||
|
g.setColor(color);
|
||||||
|
}
|
||||||
|
if (stroke != null) {
|
||||||
|
g.setStroke(stroke);
|
||||||
|
}
|
||||||
|
g.drawLine(p0.inside.x, p0.inside.y, p1.inside.x, p1.inside.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rect(final Position p, final double w, final double h, final Color border, final Stroke stroke, final Color fill) {
|
||||||
|
final int _w = (int) round(w);
|
||||||
|
final int _h = (int) round(h);
|
||||||
|
if (fill != null) {
|
||||||
|
g.setColor(fill);
|
||||||
|
g.fillRect(p.inside.x, p.inside.y, _w, _h);
|
||||||
|
}
|
||||||
|
if (border != null && stroke != null) {
|
||||||
|
g.setColor(border);
|
||||||
|
g.setStroke(stroke);
|
||||||
|
g.drawRect(p.inside.x, p.inside.y, _w, _h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void circle(final Position center, final double radius, final Color border, final Stroke stroke, final Color fill) {
|
||||||
|
final int _x = (int) round(center.inside.x - radius);
|
||||||
|
final int _y = (int) round(center.inside.y - radius);
|
||||||
|
final int diameter = (int) round(radius * 2);
|
||||||
|
if (fill != null) {
|
||||||
|
g.setColor(fill);
|
||||||
|
g.fillArc(_x, _y, diameter, diameter, 0, 360);
|
||||||
|
}
|
||||||
|
if (border != null && stroke != null) {
|
||||||
|
g.setColor(border);
|
||||||
|
g.setStroke(stroke);
|
||||||
|
g.drawArc(_x, _y, diameter, diameter, 0, 360);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void textCenter(final Font font, final String string, final int y, final Color color) {
|
||||||
|
g.setFont(font);
|
||||||
|
g.setColor(color);
|
||||||
|
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||||
|
g.drawString(string, (int) round(RASTER - bounds.getWidth()) / 2, y + g.getFont().getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clockwise(final Orientation orientation) {
|
||||||
|
g.rotate(orientation.getRadians(), P50, P50);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetTransform() {
|
||||||
|
g.setTransform(new AffineTransform());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import de.ph87.electro.circuit.ShortCircuit;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -35,11 +36,15 @@ public class PartBattery extends Part {
|
|||||||
|
|
||||||
private ShortCircuit shortCircuit = null;
|
private ShortCircuit shortCircuit = null;
|
||||||
|
|
||||||
public PartBattery(final Point position) {
|
public PartBattery() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartBattery(final Position position) {
|
||||||
this("Batterie", position, Orientation.R0, VOLTAGE);
|
this("Batterie", position, Orientation.R0, VOLTAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartBattery(final String name, final Point position, final Orientation orientation, final double initialVoltage) {
|
public PartBattery(final String name, final Position position, final Orientation orientation, final double initialVoltage) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
minus = newJunction(this, "-", P10, P50);
|
minus = newJunction(this, "-", P10, P50);
|
||||||
plus = newJunction(this, "+", P90, P50);
|
plus = newJunction(this, "+", P90, P50);
|
||||||
@ -66,27 +71,29 @@ public class PartBattery extends Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
line(g, P10, P50, P50 - GAP / 2 - MINUS_W / 2, P50, Color.BLACK, SYMBOL_STROKE);
|
render.clockwise(getOrientation());
|
||||||
line(g, P50 + GAP / 2 + PLUS_W / 2, P50, P90, P50, Color.BLACK, SYMBOL_STROKE);
|
render.line(new Position(P10, P50), new Position(P50 - GAP / 2 - MINUS_W / 2, P50), Color.BLACK, SYMBOL_STROKE);
|
||||||
rect(g, P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
|
render.line(new Position(P50 + GAP / 2 + PLUS_W / 2, P50), new Position(P90, P50), Color.BLACK, SYMBOL_STROKE);
|
||||||
rect(g, P50 + GAP / 2, P50 - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
|
render.rect(new Position(P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2), MINUS_W, MINUS_H, null, null, Color.BLACK);
|
||||||
|
render.rect(new Position(P50 + GAP / 2, P50 - PLUS_H / 2), PLUS_W, PLUS_H, null, null, Color.BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _labels(final Graphics2D g) {
|
protected void _labels() {
|
||||||
final int y0;
|
final int y0;
|
||||||
final int y1;
|
final int y1;
|
||||||
if (getOrientation() == Orientation.R180) {
|
if (getOrientation() == Orientation.R180) {
|
||||||
y0 = P95 - LABEL_FONT.getSize();
|
y0 = P95 - LABEL_FONT.getSize();
|
||||||
y1 = P05;
|
y1 = P05;
|
||||||
} else {
|
} else {
|
||||||
|
render.clockwise(getOrientation());
|
||||||
y0 = P05;
|
y0 = P05;
|
||||||
y1 = P95 - LABEL_FONT.getSize();
|
y1 = P95 - LABEL_FONT.getSize();
|
||||||
}
|
}
|
||||||
textCenter(g, LABEL_FONT, "%.1fV".formatted(voltage), y0, Color.BLACK);
|
render.textCenter(LABEL_FONT, "%.1fV".formatted(voltage), y0, Color.BLACK);
|
||||||
if (shortCircuit != null) {
|
if (shortCircuit != null) {
|
||||||
textCenter(g, LABEL_FONT, "KURZSCHLUSS", y1, Color.RED);
|
render.textCenter(LABEL_FONT, "KURZSCHLUSS", y1, Color.RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,11 +3,10 @@ package de.ph87.electro.circuit.part.parts;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -18,11 +17,15 @@ public class PartJunctionCorner extends PartOther {
|
|||||||
|
|
||||||
private final Junction j1;
|
private final Junction j1;
|
||||||
|
|
||||||
public PartJunctionCorner(final Point position) {
|
public PartJunctionCorner() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartJunctionCorner(final Position position) {
|
||||||
this("", position, Orientation.R0);
|
this("", position, Orientation.R0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartJunctionCorner(final String name, final Point position, final Orientation orientation) {
|
public PartJunctionCorner(final String name, final Position position, final Orientation orientation) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
j0 = newJunction(this, "", P10, P10);
|
j0 = newJunction(this, "", P10, P10);
|
||||||
j1 = newJunction(this, "", P90, P90);
|
j1 = newJunction(this, "", P90, P90);
|
||||||
|
|||||||
@ -3,11 +3,10 @@ package de.ph87.electro.circuit.part.parts;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P50;
|
import static de.ph87.electro.CONFIG.P50;
|
||||||
import static de.ph87.electro.CONFIG.P10;
|
import static de.ph87.electro.CONFIG.P10;
|
||||||
|
|
||||||
@ -19,11 +18,15 @@ public class PartJunctionEdge extends PartOther {
|
|||||||
|
|
||||||
private final Junction j1;
|
private final Junction j1;
|
||||||
|
|
||||||
public PartJunctionEdge(final Point position) {
|
public PartJunctionEdge() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartJunctionEdge(final Position position) {
|
||||||
this("", position, Orientation.R0);
|
this("", position, Orientation.R0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartJunctionEdge(final String name, final Point position, final Orientation orientation) {
|
public PartJunctionEdge(final String name, final Position position, final Orientation orientation) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
j0 = newJunction(this, "", P10, P50);
|
j0 = newJunction(this, "", P10, P50);
|
||||||
j1 = newJunction(this, "", P50, P10);
|
j1 = newJunction(this, "", P50, P10);
|
||||||
|
|||||||
@ -3,11 +3,10 @@ package de.ph87.electro.circuit.part.parts;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P50;
|
import static de.ph87.electro.CONFIG.P50;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -16,11 +15,15 @@ public class PartJunctionMiddle extends PartOther {
|
|||||||
|
|
||||||
private final Junction junction;
|
private final Junction junction;
|
||||||
|
|
||||||
public PartJunctionMiddle(final Point position) {
|
public PartJunctionMiddle() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartJunctionMiddle(final Position position) {
|
||||||
this("", position, Orientation.R0);
|
this("", position, Orientation.R0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartJunctionMiddle(final String name, final Point position, final Orientation orientation) {
|
public PartJunctionMiddle(final String name, final Position position, final Orientation orientation) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
junction = newJunction(this, "", P50, P50);
|
junction = newJunction(this, "", P50, P50);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.parts;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
@ -46,11 +47,15 @@ public class PartLight extends PartOther {
|
|||||||
|
|
||||||
private Color color = BULB_OFF_COLOR;
|
private Color color = BULB_OFF_COLOR;
|
||||||
|
|
||||||
public PartLight(final Point position) {
|
public PartLight() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartLight(final Position position) {
|
||||||
this("Licht", position, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
this("Licht", position, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartLight(final String name, final Point position, final Orientation orientation, final double minVoltage, final double maxVoltage) {
|
public PartLight(final String name, final Position position, final Orientation orientation, final double minVoltage, final double maxVoltage) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
this.minVoltage = minVoltage;
|
this.minVoltage = minVoltage;
|
||||||
this.maxVoltage = maxVoltage;
|
this.maxVoltage = maxVoltage;
|
||||||
@ -91,29 +96,30 @@ public class PartLight extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
line(g, pin0, pin1, Color.BLACK, SYMBOL_STROKE);
|
render.line(pin0, pin1, Color.BLACK, SYMBOL_STROKE);
|
||||||
circle(g, P50, P50, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
|
render.circle(new Position(P50, P50), BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
|
||||||
|
|
||||||
final double diag = 0.33 * RASTER;
|
final double diag = 0.33 * RASTER;
|
||||||
line(g, diag, diag, RASTER - diag, RASTER - diag, Color.BLACK, SYMBOL_STROKE);
|
render.line(new Position(diag, diag), new Position(RASTER - diag, RASTER - diag), Color.BLACK, SYMBOL_STROKE);
|
||||||
line(g, diag, RASTER - diag, RASTER - diag, diag, Color.BLACK, SYMBOL_STROKE);
|
render.line(new Position(diag, RASTER - diag), new Position(RASTER - diag, diag), Color.BLACK, SYMBOL_STROKE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _labels(final Graphics2D g) {
|
protected void _labels() {
|
||||||
final int y0;
|
final int y0;
|
||||||
final int y1;
|
final int y1;
|
||||||
if (getOrientation() == Orientation.R180) {
|
if (getOrientation() == Orientation.R180) {
|
||||||
y0 = P95 - LABEL_FONT.getSize();
|
y0 = P95 - LABEL_FONT.getSize();
|
||||||
y1 = P05;
|
y1 = P05;
|
||||||
} else {
|
} else {
|
||||||
|
render.clockwise(getOrientation());
|
||||||
y0 = P05;
|
y0 = P05;
|
||||||
y1 = P95 - LABEL_FONT.getSize();
|
y1 = P95 - LABEL_FONT.getSize();
|
||||||
}
|
}
|
||||||
textCenter(g, LABEL_FONT, "%.1fV (max)".formatted(maxVoltage), y0, Color.BLACK);
|
render.textCenter(LABEL_FONT, "%.1fV (max)".formatted(maxVoltage), y0, Color.BLACK);
|
||||||
if (defect) {
|
if (defect) {
|
||||||
textCenter(g, LABEL_FONT, "DEFEKT", y1, Color.RED);
|
render.textCenter(LABEL_FONT, "DEFEKT", y1, Color.RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import de.ph87.electro.circuit.ShortCircuit;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -23,11 +24,15 @@ public class PartSwitch1x1 extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitch1x1(final Point position) {
|
public PartSwitch1x1() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitch1x1(final Position position) {
|
||||||
this("Ausschalter", position, Orientation.R0, false);
|
this("Ausschalter", position, Orientation.R0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x1(final String name, final Point position, final Orientation orientation, final boolean state) {
|
public PartSwitch1x1(final String name, final Position position, final Orientation orientation, final boolean state) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
common = newJunction(this, "", P10, P50);
|
common = newJunction(this, "", P10, P50);
|
||||||
output = newJunction(this, "", P90, P50);
|
output = newJunction(this, "", P90, P50);
|
||||||
@ -60,11 +65,12 @@ public class PartSwitch1x1 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common, P90, P25, common.getColor(), SWITCH_STROKE);
|
final Point end = getOrientation().rotate(new Point(P90, P25));
|
||||||
|
render.line(common.getPosition(), new Position(end), common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
line(g, common, output, common.getColor(), SWITCH_STROKE);
|
render.line(common, output, common.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,12 +4,11 @@ import de.ph87.electro.circuit.ShortCircuit;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -25,11 +24,15 @@ public class PartSwitch1x2 extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitch1x2(final Point position) {
|
public PartSwitch1x2() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitch1x2(final Position position) {
|
||||||
this("Wechselschalter", position, Orientation.R0, false);
|
this("Wechselschalter", position, Orientation.R0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x2(final String name, final Point position, final Orientation orientation, final boolean state) {
|
public PartSwitch1x2(final String name, final Position position, final Orientation orientation, final boolean state) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
common = newJunction(this, "", P10, P50);
|
common = newJunction(this, "", P10, P50);
|
||||||
output0 = newJunction(this, "", P90, P25);
|
output0 = newJunction(this, "", P90, P25);
|
||||||
@ -70,11 +73,11 @@ public class PartSwitch1x2 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common, output0, common.getColor(), SWITCH_STROKE);
|
render.line(common, output0, common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
line(g, common, output1, common.getColor(), SWITCH_STROKE);
|
render.line(common, output1, common.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,12 +4,11 @@ import de.ph87.electro.circuit.ShortCircuit;
|
|||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
|
import de.ph87.electro.circuit.part.Position;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -27,11 +26,15 @@ public class PartSwitchCross extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitchCross(final Point position) {
|
public PartSwitchCross() {
|
||||||
|
this(Position.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitchCross(final Position position) {
|
||||||
this("Kreuzschalter", position, Orientation.R0, false);
|
this("Kreuzschalter", position, Orientation.R0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitchCross(final String name, final Point position, final Orientation orientation, final boolean state) {
|
public PartSwitchCross(final String name, final Position position, final Orientation orientation, final boolean state) {
|
||||||
super(name, position, orientation);
|
super(name, position, orientation);
|
||||||
common0 = newJunction(this, "", P10, P25);
|
common0 = newJunction(this, "", P10, P25);
|
||||||
common1 = newJunction(this, "", P10, P75);
|
common1 = newJunction(this, "", P10, P75);
|
||||||
@ -84,13 +87,13 @@ public class PartSwitchCross extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render(final Graphics2D g) {
|
protected void _render() {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common0, output0, common0.getColor(), SWITCH_STROKE);
|
render.line(common0, output0, common0.getColor(), SWITCH_STROKE);
|
||||||
line(g, common1, output1, common1.getColor(), SWITCH_STROKE);
|
render.line(common1, output1, common1.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
line(g, common0, output1, common0.getColor(), SWITCH_STROKE);
|
render.line(common0, output1, common0.getColor(), SWITCH_STROKE);
|
||||||
line(g, common1, output0, common1.getColor(), SWITCH_STROKE);
|
render.line(common1, output0, common1.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +0,0 @@
|
|||||||
package de.ph87.electro.common;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class MathHelpers {
|
|
||||||
|
|
||||||
public static Point div(final Point p, final int div) {
|
|
||||||
return new Point(p.x / div, p.y / div);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
package de.ph87.electro.demo;
|
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static de.ph87.electro.CONFIG.VOLTAGE;
|
|
||||||
|
|
||||||
public class DemoAll {
|
|
||||||
|
|
||||||
public static Circuit complexTrippleAndSimple() {
|
|
||||||
final Circuit circuit = new Circuit();
|
|
||||||
|
|
||||||
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R180, VOLTAGE);
|
|
||||||
final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ausschalter", 2, 0, Orientation.R0, false);
|
|
||||||
final PartLight light = circuit.addLight("Licht", 4, 0, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
|
||||||
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 2, Orientation.R0, false);
|
|
||||||
final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 2, 2, Orientation.R0, false);
|
|
||||||
final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 4, 2, Orientation.R180, true);
|
|
||||||
|
|
||||||
circuit.connect(battery.getMinus(), switcher.getCommon());
|
|
||||||
circuit.connect(switcher.getOutput(), light.getPin0());
|
|
||||||
|
|
||||||
circuit.connect(battery.getPlus(), switcher0.getCommon());
|
|
||||||
circuit.connect(switcher0.getOutput0(), switcherX.getCommon0());
|
|
||||||
circuit.connect(switcher0.getOutput1(), switcherX.getCommon1());
|
|
||||||
circuit.connect(switcherX.getOutput0(), switcher1.getOutput1());
|
|
||||||
circuit.connect(switcherX.getOutput1(), switcher1.getOutput0());
|
|
||||||
circuit.connect(switcher1.getCommon(), light.getPin1());
|
|
||||||
|
|
||||||
return circuit;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Circuit simpleAlternative() {
|
|
||||||
final Circuit circuit = new Circuit();
|
|
||||||
|
|
||||||
final PartBattery battery = circuit.addBattery("Batterie", 2, 0, Orientation.R90, VOLTAGE);
|
|
||||||
final PartLight light0 = circuit.addLight("Licht 0", 4, 2, Orientation.R90, BULB_VOLTAGE_MIN, VOLTAGE);
|
|
||||||
final PartLight light1 = circuit.addLight("Licht 1", 0, 2, Orientation.R90, BULB_VOLTAGE_MIN, VOLTAGE);
|
|
||||||
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 2, 2, Orientation.R90, false);
|
|
||||||
|
|
||||||
circuit.connect(battery.getMinus(), light0.getPin0());
|
|
||||||
circuit.connect(battery.getMinus(), light1.getPin0());
|
|
||||||
|
|
||||||
circuit.connect(battery.getPlus(), switcher0.getCommon());
|
|
||||||
circuit.connect(switcher0.getOutput0(), light0.getPin1());
|
|
||||||
circuit.connect(switcher0.getOutput1(), light1.getPin1());
|
|
||||||
|
|
||||||
return circuit;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -9,14 +9,14 @@ import java.awt.*;
|
|||||||
public class Sidebar extends JPanel {
|
public class Sidebar extends JPanel {
|
||||||
|
|
||||||
public Sidebar() {
|
public Sidebar() {
|
||||||
add(new PartBattery(new Point(0, 0)));
|
add(new PartBattery());
|
||||||
add(new PartJunctionCorner(new Point(0, 0)));
|
add(new PartJunctionCorner());
|
||||||
add(new PartJunctionEdge(new Point(0, 0)));
|
add(new PartJunctionEdge());
|
||||||
add(new PartJunctionMiddle(new Point(0, 0)));
|
add(new PartJunctionMiddle());
|
||||||
add(new PartLight(new Point(0, 0)));
|
add(new PartLight());
|
||||||
add(new PartSwitch1x1(new Point(0, 0)));
|
add(new PartSwitch1x1());
|
||||||
add(new PartSwitch1x2(new Point(0, 0)));
|
add(new PartSwitch1x2());
|
||||||
add(new PartSwitchCross(new Point(0, 0)));
|
add(new PartSwitchCross());
|
||||||
setPreferredSize(new Dimension(0, 200));
|
setPreferredSize(new Dimension(0, 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package de.ph87.electro.sidebar;
|
|||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -12,7 +11,6 @@ import java.awt.dnd.DragSource;
|
|||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Getter
|
@Getter
|
||||||
public class SidebarPart extends JPanel {
|
public class SidebarPart extends JPanel {
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -16,9 +14,9 @@ public class BatteryLightTest {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
private static final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
import de.ph87.electro.circuit.part.parts.PartSwitch1x1;
|
import de.ph87.electro.circuit.part.parts.PartSwitch1x1;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -17,11 +15,11 @@ public class BatterySwitcher1x1Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
private static final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
|
|
||||||
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, Orientation.R0, false);
|
private static final PartSwitch1x1 switcher = circuit.addPart(new PartSwitch1x1());
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -17,13 +15,13 @@ public class BatterySwitcher1x2Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
private static final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, Orientation.R0, false);
|
private static final PartSwitch1x2 switcher = circuit.addPart(new PartSwitch1x2());
|
||||||
|
|
||||||
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light0 = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light1 = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -17,13 +15,13 @@ public class BatterySwitcher2x2Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
|
private static final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
|
private static final PartSwitch1x2 switcher0 = circuit.addPart(new PartSwitch1x2());
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, Orientation.R0, false);
|
private static final PartSwitch1x2 switcher1 = circuit.addPart(new PartSwitch1x2());
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 0, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.parts.PartSwitch1x2;
|
||||||
@ -8,7 +7,6 @@ import de.ph87.electro.circuit.part.parts.PartSwitchCross;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -18,15 +16,15 @@ public class BatterySwitcherCrossTest {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
|
private static final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
|
private static final PartSwitch1x2 switcher0 = circuit.addPart(new PartSwitch1x2());
|
||||||
|
|
||||||
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, Orientation.R0, false);
|
private static final PartSwitchCross switcherX = circuit.addPart(new PartSwitchCross());
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, Orientation.R0, false);
|
private static final PartSwitch1x2 switcher1 = circuit.addPart(new PartSwitch1x2());
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 2, 0, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
private static final PartLight light = circuit.addPart(new PartLight());
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.parts.PartBattery;
|
import de.ph87.electro.circuit.part.parts.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.parts.PartLight;
|
import de.ph87.electro.circuit.part.parts.PartLight;
|
||||||
@ -12,8 +11,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.BULB_VOLTAGE_MIN;
|
|
||||||
import static de.ph87.electro.CONFIG.VOLTAGE;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class CircuitServiceTest {
|
class CircuitServiceTest {
|
||||||
@ -21,8 +18,8 @@ class CircuitServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void serialization() throws IOException {
|
void serialization() throws IOException {
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R90, VOLTAGE);
|
final PartBattery battery = circuit.addPart(new PartBattery());
|
||||||
final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
|
final PartLight light = circuit.addPart(new PartLight());
|
||||||
circuit.connect(battery.getPlus(), light.getPin1());
|
circuit.connect(battery.getPlus(), light.getPin1());
|
||||||
circuit.connect(light.getPin0(), battery.getMinus());
|
circuit.connect(light.getPin0(), battery.getMinus());
|
||||||
check(circuit);
|
check(circuit);
|
||||||
@ -53,6 +50,7 @@ class CircuitServiceTest {
|
|||||||
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
||||||
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
||||||
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
||||||
|
assertEquals(originalJunction.getRelative(), reloadedJunction.getRelative());
|
||||||
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
||||||
assertEquals(originalJunction.getWires().size(), reloadedJunction.getWires().size());
|
assertEquals(originalJunction.getWires().size(), reloadedJunction.getWires().size());
|
||||||
originalJunction.getWires().stream()
|
originalJunction.getWires().stream()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user