UI
This commit is contained in:
parent
1d780ce01e
commit
29fc0f1849
43
src/main/java/de/ph87/electro/CONFIG.java
Normal file
43
src/main/java/de/ph87/electro/CONFIG.java
Normal file
@ -0,0 +1,43 @@
|
||||
package de.ph87.electro;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class CONFIG {
|
||||
|
||||
public static final int RASTER = 200;
|
||||
|
||||
public static final double HALF = 0.5;
|
||||
|
||||
public static final double FOURTH1 = 0.25;
|
||||
|
||||
public static final double FOURTH3 = 0.75;
|
||||
|
||||
public static final double JUNCTION_LEFT = 0.1;
|
||||
|
||||
public static final double JUNCTION_RIGHT = 0.9;
|
||||
|
||||
public static final double JUNCTION_RADIUS = 0.05;
|
||||
|
||||
public static final Color PART_BACKGROUND = new Color(224, 224, 224);
|
||||
|
||||
public static final Color RASTER_COLOR = Color.gray;
|
||||
|
||||
public static final Color VOLTAGE_UNKNOWN_COLOR = Color.darkGray;
|
||||
|
||||
public static final Color VOLTAGE_HIGH_COLOR = Color.RED;
|
||||
|
||||
public static final Color VOLTAGE_LOW_COLOR = new Color(0, 128, 255);
|
||||
|
||||
public static final BasicStroke RASTER_STROKE = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{5}, 0);
|
||||
|
||||
public static final BasicStroke NORMAL_STROKE = new BasicStroke(1);
|
||||
|
||||
public static final BasicStroke SYMBOL_STROKE = new BasicStroke(3);
|
||||
|
||||
public static final BasicStroke WIRE_STROKE = new BasicStroke(5);
|
||||
|
||||
public static final BasicStroke WIRE_STROKE_BACK = new BasicStroke(0);
|
||||
|
||||
public static final BasicStroke SWITCH_STROKE = new BasicStroke(15);
|
||||
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package de.ph87.electro;
|
||||
|
||||
import de.ph87.electro.circuit.Circuit;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Circuit circuit = new Circuit();
|
||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 3);
|
||||
final PartLight light = circuit.addLight("Licht", 1, 1, 3);
|
||||
circuit.connect(battery.getMinus(), light.getMinus());
|
||||
circuit.connect(battery.getPlus(), light.getPlus());
|
||||
circuit.evaluate();
|
||||
circuit.getParts().forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
||||
24
src/main/java/de/ph87/electro/Window.java
Normal file
24
src/main/java/de/ph87/electro/Window.java
Normal file
@ -0,0 +1,24 @@
|
||||
package de.ph87.electro;
|
||||
|
||||
import de.ph87.electro.circuit.CircuitPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Window extends JFrame {
|
||||
|
||||
public Window() {
|
||||
add(new CircuitPanel());
|
||||
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setPreferredSize(new Dimension(1200, 900));
|
||||
setExtendedState(MAXIMIZED_BOTH);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Window();
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,11 +4,7 @@ import de.ph87.electro.circuit.dto.CircuitDto;
|
||||
import de.ph87.electro.circuit.dto.PartDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import de.ph87.electro.circuit.part.impl.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@ -32,33 +28,40 @@ public class Circuit {
|
||||
parts.forEach(part -> part.link(junctions));
|
||||
}
|
||||
|
||||
public PartBattery addBattery(final String name, final int x, final int y, final double voltage) {
|
||||
public PartBattery addBattery(final String name, final int x, final int y, final int rotate, final double voltage) {
|
||||
final PartBattery battery = new PartBattery(name, x, y, voltage);
|
||||
parts.add(battery);
|
||||
addAndRotate(battery, rotate);
|
||||
return battery;
|
||||
}
|
||||
|
||||
public PartLight addLight(final String name, final int x, final int y, final double maxVoltage) {
|
||||
private void addAndRotate(final Part part, final int rotate) {
|
||||
for (int i = 0; i < rotate % 4; i++) {
|
||||
part.rotate();
|
||||
}
|
||||
parts.add(part);
|
||||
}
|
||||
|
||||
public PartLight addLight(final String name, final int x, final int y, final int rotate, final double maxVoltage) {
|
||||
final PartLight light = new PartLight(name, x, y, maxVoltage);
|
||||
parts.add(light);
|
||||
addAndRotate(light, rotate);
|
||||
return light;
|
||||
}
|
||||
|
||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y) {
|
||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, x, y);
|
||||
parts.add(switch1x1);
|
||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y, final int rotate, final boolean state) {
|
||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, x, y, state);
|
||||
addAndRotate(switch1x1, rotate);
|
||||
return switch1x1;
|
||||
}
|
||||
|
||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y) {
|
||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, x, y);
|
||||
parts.add(switch1x2);
|
||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y, final int rotate, final boolean state) {
|
||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, x, y, state);
|
||||
addAndRotate(switch1x2, rotate);
|
||||
return switch1x2;
|
||||
}
|
||||
|
||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y) {
|
||||
final PartSwitchCross switchCross = new PartSwitchCross(name, x, y);
|
||||
parts.add(switchCross);
|
||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y, final int rotate, final boolean state) {
|
||||
final PartSwitchCross switchCross = new PartSwitchCross(name, x, y, state);
|
||||
addAndRotate(switchCross, rotate);
|
||||
return switchCross;
|
||||
}
|
||||
|
||||
@ -76,4 +79,18 @@ public class Circuit {
|
||||
return parts.stream().filter(part -> part instanceof PartBattery).map(part -> (PartBattery) part);
|
||||
}
|
||||
|
||||
public List<Wire> getWires() {
|
||||
final List<Wire> wires = new ArrayList<>();
|
||||
for (final Part part : parts) {
|
||||
for (final Junction junction : part.getJunctions()) {
|
||||
for (final Junction destination : junction.getDestinations()) {
|
||||
if (wires.stream().noneMatch(wire -> wire.matches(junction, destination))) {
|
||||
wires.add(new Wire(junction, destination));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return wires;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
97
src/main/java/de/ph87/electro/circuit/CircuitPanel.java
Normal file
97
src/main/java/de/ph87/electro/circuit/CircuitPanel.java
Normal file
@ -0,0 +1,97 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.demo.DemoAll;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static java.awt.event.MouseEvent.BUTTON1;
|
||||
import static java.awt.event.MouseEvent.BUTTON3;
|
||||
|
||||
public class CircuitPanel extends JPanel {
|
||||
|
||||
private Circuit circuit = new Circuit();
|
||||
|
||||
public CircuitPanel() {
|
||||
addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseReleased(final MouseEvent e) {
|
||||
final int x = e.getX() / RASTER;
|
||||
final int y = e.getY() / RASTER;
|
||||
final Optional<Part> partOptional = circuit.getParts().stream().filter(p -> p.getX() == x && p.getY() == y).findFirst();
|
||||
switch (e.getButton()) {
|
||||
case BUTTON1:
|
||||
partOptional.ifPresent(part -> {
|
||||
part.click();
|
||||
circuit.evaluate();
|
||||
repaint();
|
||||
});
|
||||
break;
|
||||
case BUTTON3:
|
||||
partOptional.ifPresent(part -> {
|
||||
part.rotate();
|
||||
repaint();
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
circuit = DemoAll.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(final Graphics _g) {
|
||||
final Graphics2D g = (Graphics2D) _g;
|
||||
final int w = getWidth();
|
||||
final int h = getHeight();
|
||||
drawBack(g, w, h);
|
||||
drawParts(g);
|
||||
drawRaster(g, w, h);
|
||||
drawWires(g);
|
||||
}
|
||||
|
||||
private void drawBack(final Graphics2D g, final int w, final int h) {
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, w, h);
|
||||
}
|
||||
|
||||
private void drawParts(final Graphics2D g) {
|
||||
circuit.getParts().forEach(part -> part.paint(g));
|
||||
}
|
||||
|
||||
private void drawRaster(final Graphics2D g, final int w, final int h) {
|
||||
g.setStroke(RASTER_STROKE);
|
||||
g.setColor(RASTER_COLOR);
|
||||
for (int x = 0; x < w; x += RASTER) {
|
||||
g.drawLine(x, 0, x, h);
|
||||
}
|
||||
for (int y = 0; y < h; y += RASTER) {
|
||||
g.drawLine(0, y, w, y);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawWires(final Graphics2D g) {
|
||||
for (final Wire wire : circuit.getWires()) {
|
||||
final int x0 = wire.getA().getAbsoluteX();
|
||||
final int y0 = wire.getA().getAbsoluteY();
|
||||
final int x1 = wire.getB().getAbsoluteX();
|
||||
final int y1 = wire.getB().getAbsoluteY();
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.setStroke(WIRE_STROKE_BACK);
|
||||
g.drawLine(x0, y0, x1, y1);
|
||||
|
||||
g.setColor(wire.getA().getColor());
|
||||
g.setStroke(WIRE_STROKE);
|
||||
g.drawLine(x0, y0, x1, y1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
22
src/main/java/de/ph87/electro/circuit/Wire.java
Normal file
22
src/main/java/de/ph87/electro/circuit/Wire.java
Normal file
@ -0,0 +1,22 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Wire {
|
||||
|
||||
private final Junction a;
|
||||
|
||||
private final Junction b;
|
||||
|
||||
public Wire(final Junction a, final Junction b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public boolean matches(final Junction x, final Junction y) {
|
||||
return (a == x && b == y) || (a == y && b == x);
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,7 +5,6 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ -17,14 +16,17 @@ public class JunctionDto {
|
||||
|
||||
private String name;
|
||||
|
||||
private Point position;
|
||||
private double x;
|
||||
|
||||
private double y;
|
||||
|
||||
private List<String> destinations;
|
||||
|
||||
public JunctionDto(final Junction junction) {
|
||||
this.uuid = junction.getUuid();
|
||||
this.name = junction.getName();
|
||||
this.position = junction.getPosition();
|
||||
this.x = junction.getX();
|
||||
this.y = junction.getY();
|
||||
this.destinations = junction.getDestinations().stream().map(Junction::getUuid).toList();
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@ -2,16 +2,11 @@ package de.ph87.electro.circuit.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import de.ph87.electro.circuit.part.impl.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ -24,14 +19,20 @@ public class PartDto {
|
||||
|
||||
private String name;
|
||||
|
||||
private Point position;
|
||||
private int x;
|
||||
|
||||
private int y;
|
||||
|
||||
private int rotation;
|
||||
|
||||
private List<JunctionDto> junctions;
|
||||
|
||||
protected PartDto(final Part part) {
|
||||
this.uuid = part.getUuid();
|
||||
this.name = part.getName();
|
||||
this.position = part.getPosition();
|
||||
this.x = part.getX();
|
||||
this.y = part.getY();
|
||||
this.rotation = part.getRotation();
|
||||
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
@ -20,8 +20,8 @@ public class PartLightDto extends PartDto {
|
||||
|
||||
public PartLightDto(final PartLight partLight) {
|
||||
super(partLight);
|
||||
this.plus = new JunctionDto(partLight.getPlus());
|
||||
this.minus = new JunctionDto(partLight.getMinus());
|
||||
this.plus = new JunctionDto(partLight.getPin1());
|
||||
this.minus = new JunctionDto(partLight.getPin0());
|
||||
this.defect = partLight.isDefect();
|
||||
this.maxVoltage = partLight.getMaxVoltage();
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x1;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@ -12,6 +12,9 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static java.lang.Math.round;
|
||||
|
||||
@Getter
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class Junction {
|
||||
@ -26,24 +29,30 @@ public class Junction {
|
||||
@ToString.Include
|
||||
private final String name;
|
||||
|
||||
private final Point position;
|
||||
private double x;
|
||||
|
||||
private double y;
|
||||
|
||||
private final Set<Junction> destinations = new HashSet<>();
|
||||
|
||||
@Setter
|
||||
@ToString.Include
|
||||
private double voltage = Double.NaN;
|
||||
|
||||
private final Set<Junction> destinations = new HashSet<>();
|
||||
private Color color = VOLTAGE_UNKNOWN_COLOR;
|
||||
|
||||
@ToString.Include
|
||||
@SuppressWarnings("unused") // lombok toString
|
||||
public List<String> destinations() {
|
||||
return destinations.stream().map(Junction::getUuid).toList();
|
||||
}
|
||||
|
||||
public Junction(final Part owner, final String name, final int x, final int y) {
|
||||
public Junction(final Part owner, final String name, final double x, final double y) {
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
this.owner = owner;
|
||||
this.name = name;
|
||||
this.position = new Point(x, y);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this._dto = null;
|
||||
}
|
||||
|
||||
@ -51,7 +60,8 @@ public class Junction {
|
||||
this.uuid = dto.getUuid();
|
||||
this.owner = owner;
|
||||
this.name = dto.getName();
|
||||
this.position = dto.getPosition();
|
||||
this.x = dto.getX();
|
||||
this.y = dto.getY();
|
||||
this._dto = dto;
|
||||
}
|
||||
|
||||
@ -65,6 +75,7 @@ public class Junction {
|
||||
|
||||
public void reset() {
|
||||
voltage = Double.NaN;
|
||||
color = VOLTAGE_UNKNOWN_COLOR;
|
||||
}
|
||||
|
||||
public void propagate(final double newVoltage) throws ShortCircuit {
|
||||
@ -73,6 +84,13 @@ public class Junction {
|
||||
}
|
||||
if (Double.isNaN(voltage)) {
|
||||
voltage = newVoltage;
|
||||
if (Double.isNaN(voltage)) {
|
||||
color = Color.GRAY;
|
||||
} else if (voltage > 0) {
|
||||
color = VOLTAGE_HIGH_COLOR;
|
||||
} else {
|
||||
color = VOLTAGE_LOW_COLOR;
|
||||
}
|
||||
owner.propagate(this);
|
||||
for (Junction junction : destinations) {
|
||||
junction.propagate(newVoltage);
|
||||
@ -82,4 +100,23 @@ public class Junction {
|
||||
throw new ShortCircuit();
|
||||
}
|
||||
|
||||
@SuppressWarnings("SuspiciousNameCombination")
|
||||
public void rotate() {
|
||||
final double oldX = x;
|
||||
x = 1 - y;
|
||||
y = oldX;
|
||||
}
|
||||
|
||||
public void paint(final Graphics2D g) {
|
||||
owner.circle(g, x, y, JUNCTION_RADIUS, Color.BLACK, NORMAL_STROKE, color);
|
||||
}
|
||||
|
||||
public int getAbsoluteX() {
|
||||
return (int) round((owner.x + x) * RASTER);
|
||||
}
|
||||
|
||||
public int getAbsoluteY() {
|
||||
return (int) round((owner.y + y) * RASTER);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,19 +2,21 @@ package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.*;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import de.ph87.electro.circuit.part.impl.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static de.ph87.electro.CONFIG.PART_BACKGROUND;
|
||||
import static de.ph87.electro.CONFIG.RASTER;
|
||||
import static java.lang.Math.round;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
@ -26,20 +28,27 @@ public abstract class Part {
|
||||
@ToString.Include
|
||||
private String name;
|
||||
|
||||
private Point position;
|
||||
protected int x;
|
||||
|
||||
protected int y;
|
||||
|
||||
protected int rotation = 0;
|
||||
|
||||
protected final List<Junction> junctions = new ArrayList<>();
|
||||
|
||||
protected Part(final String name, final int x, final int y) {
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
this.name = name;
|
||||
this.position = new Point(x, y);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
protected Part(final PartDto dto) {
|
||||
this.uuid = dto.getUuid();
|
||||
this.name = dto.getName();
|
||||
this.position = dto.getPosition();
|
||||
this.x = dto.getX();
|
||||
this.y = dto.getY();
|
||||
this.rotation = dto.getRotation();
|
||||
}
|
||||
|
||||
public void link(final List<Junction> junctions) {
|
||||
@ -50,8 +59,78 @@ public abstract class Part {
|
||||
junctions.forEach(Junction::reset);
|
||||
}
|
||||
|
||||
public void paint(final Graphics2D g) {
|
||||
rect(g, 0, 0, 1, 1, null, null, PART_BACKGROUND);
|
||||
_paint(g);
|
||||
junctions.forEach(junction -> junction.paint(g));
|
||||
}
|
||||
|
||||
public void rotate() {
|
||||
rotation = (rotation + 1) % 4;
|
||||
junctions.forEach(Junction::rotate);
|
||||
}
|
||||
|
||||
public void line(final Graphics2D g, final Junction common0, final Junction output0, final Color color, final BasicStroke stroke) {
|
||||
line(g, common0.getX(), common0.getY(), output0.getX(), output0.getY(), 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((x + x0) * RASTER);
|
||||
final int _y0 = (int) round((y + y0) * RASTER);
|
||||
final int _x1 = (int) round((x + x1) * RASTER);
|
||||
final int _y1 = (int) round((y + y1) * RASTER);
|
||||
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((this.x + x) * RASTER);
|
||||
final int _y = (int) round((this.y + y) * RASTER);
|
||||
final int _w = (int) round(w * RASTER);
|
||||
final int _h = (int) round(h * RASTER);
|
||||
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 img(final Graphics2D g, final BufferedImage img, final double x, final double y) {
|
||||
final int _x = (int) round((this.x + x) * RASTER);
|
||||
final int _y = (int) round((this.y + y) * RASTER);
|
||||
g.drawImage(img, _x, _y, null);
|
||||
}
|
||||
|
||||
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((this.x + x - radius) * RASTER);
|
||||
final int _y = (int) round((this.y + y - radius) * RASTER);
|
||||
final int diameter = (int) round(radius * RASTER * 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 abstract void click();
|
||||
|
||||
public abstract void propagate(final Junction source) throws ShortCircuit;
|
||||
|
||||
protected abstract void _paint(final Graphics2D g);
|
||||
|
||||
public static Part of(final PartDto abstractDto) {
|
||||
return switch (abstractDto) {
|
||||
case final PartBatteryDto dto -> new PartBattery(dto);
|
||||
|
||||
@ -1,55 +0,0 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartBatteryDto;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartBattery extends Part {
|
||||
|
||||
private final Junction minus;
|
||||
|
||||
private final Junction plus;
|
||||
|
||||
@Setter
|
||||
private double voltage;
|
||||
|
||||
private ShortCircuit shortCircuit = null;
|
||||
|
||||
public PartBattery(final String name, final int x, final int y, final double initialVoltage) {
|
||||
super(name, x, y);
|
||||
minus = new Junction(this, "-", 0, 1);
|
||||
plus = new Junction(this, "+", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = initialVoltage;
|
||||
}
|
||||
|
||||
public PartBattery(final PartBatteryDto dto) {
|
||||
super(dto);
|
||||
minus = new Junction(this, dto.getMinus());
|
||||
plus = new Junction(this, dto.getPlus());
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = dto.getVoltage();
|
||||
}
|
||||
|
||||
public void startPropagation() {
|
||||
try {
|
||||
shortCircuit = null;
|
||||
minus.propagate(0);
|
||||
plus.propagate(voltage);
|
||||
} catch (ShortCircuit e) {
|
||||
shortCircuit = e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,97 @@
|
||||
package de.ph87.electro.circuit.part.impl;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartBatteryDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static java.lang.Math.PI;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartBattery extends Part {
|
||||
|
||||
private static final double MINUS_W = 0.1;
|
||||
|
||||
private static final double MINUS_H = 0.3;
|
||||
|
||||
private static final double GAP = 0.05;
|
||||
|
||||
private static final double PLUS_W = 0.02;
|
||||
|
||||
private static final double PLUS_H = 0.6;
|
||||
|
||||
private final Junction minus;
|
||||
|
||||
private final Junction plus;
|
||||
|
||||
@Setter
|
||||
private double voltage;
|
||||
|
||||
private ShortCircuit shortCircuit = null;
|
||||
|
||||
public PartBattery(final String name, final int x, final int y, final double initialVoltage) {
|
||||
super(name, x, y);
|
||||
minus = new Junction(this, "-", JUNCTION_LEFT, HALF);
|
||||
plus = new Junction(this, "+", JUNCTION_RIGHT, HALF);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = initialVoltage;
|
||||
}
|
||||
|
||||
public PartBattery(final PartBatteryDto dto) {
|
||||
super(dto);
|
||||
minus = new Junction(this, dto.getMinus());
|
||||
plus = new Junction(this, dto.getPlus());
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = dto.getVoltage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rotate() {
|
||||
super.rotate();
|
||||
}
|
||||
|
||||
public void startPropagation() {
|
||||
try {
|
||||
shortCircuit = null;
|
||||
minus.propagate(0);
|
||||
plus.propagate(voltage);
|
||||
} catch (ShortCircuit e) {
|
||||
shortCircuit = e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _paint(final Graphics2D g) {
|
||||
final BufferedImage img = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
||||
final Graphics2D x = (Graphics2D) img.getGraphics();
|
||||
x.translate(RASTER / 2, RASTER / 2);
|
||||
x.rotate(PI / 2 * rotation);
|
||||
x.translate(-RASTER / 2, -RASTER / 2);
|
||||
line(x, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE);
|
||||
line(x, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE);
|
||||
rect(x, HALF - MINUS_W - GAP / 2, HALF - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
|
||||
rect(x, HALF + GAP / 2, HALF - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
|
||||
img(g, img, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,97 @@
|
||||
package de.ph87.electro.circuit.part.impl;
|
||||
|
||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static java.lang.Math.abs;
|
||||
import static java.lang.Math.round;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartLight extends PartOther {
|
||||
|
||||
private static final Color COLOR_DEFECT = new Color(255, 0, 234);
|
||||
|
||||
private static final double MINUS_W = 0.1;
|
||||
|
||||
private static final double MINUS_H = 0.3;
|
||||
|
||||
private static final double GAP = 0.05;
|
||||
|
||||
private static final double PLUS_W = 0.02;
|
||||
|
||||
private static final double PLUS_H = 0.6;
|
||||
|
||||
private static final double BULB_RADIUS = 0.25;
|
||||
|
||||
private final Junction pin0;
|
||||
|
||||
private final Junction pin1;
|
||||
|
||||
private final double maxVoltage;
|
||||
|
||||
private boolean defect = false;
|
||||
|
||||
private double voltage = 0;
|
||||
|
||||
private Color color;
|
||||
|
||||
public PartLight(final String name, final int x, final int y, final double maxVoltage) {
|
||||
super(name, x, y);
|
||||
pin0 = new Junction(this, "", JUNCTION_LEFT, HALF);
|
||||
pin1 = new Junction(this, "", JUNCTION_RIGHT, HALF);
|
||||
junctions.add(pin0);
|
||||
junctions.add(pin1);
|
||||
this.maxVoltage = maxVoltage;
|
||||
}
|
||||
|
||||
public PartLight(final PartLightDto dto) {
|
||||
super(dto);
|
||||
pin0 = new Junction(this, dto.getMinus());
|
||||
pin1 = new Junction(this, dto.getPlus());
|
||||
junctions.add(pin0);
|
||||
junctions.add(pin1);
|
||||
maxVoltage = dto.getMaxVoltage();
|
||||
defect = dto.isDefect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
voltage = abs(pin1.getVoltage() - pin0.getVoltage());
|
||||
if (voltage > maxVoltage) {
|
||||
defect = true;
|
||||
}
|
||||
if (defect) {
|
||||
color = COLOR_DEFECT;
|
||||
} else {
|
||||
final int v = (int) round(voltage / maxVoltage * 255);
|
||||
if (v < 10) {
|
||||
color = Color.DARK_GRAY;
|
||||
} else {
|
||||
color = new Color(v, v, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _paint(final Graphics2D g) {
|
||||
line(g, pin0, pin1, Color.BLACK, SYMBOL_STROKE);
|
||||
circle(g, HALF, HALF, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
|
||||
|
||||
final double diag = 0.33;
|
||||
line(g, diag, diag, 1 - diag, 1 - diag, Color.BLACK, SYMBOL_STROKE);
|
||||
line(g, diag, 1 - diag, 1 - diag, diag, Color.BLACK, SYMBOL_STROKE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
defect = false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
package de.ph87.electro.circuit.part.impl;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitch1x1Dto;
|
||||
@ -8,6 +8,10 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitch1x1 extends PartOther {
|
||||
@ -17,18 +21,19 @@ public class PartSwitch1x1 extends PartOther {
|
||||
private final Junction output;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
private boolean state;
|
||||
|
||||
public PartSwitch1x1(final String name, final int x, final int y) {
|
||||
public PartSwitch1x1(final String name, final int x, final int y, final boolean state) {
|
||||
super(name, x, y);
|
||||
common = new Junction(this, "", 0, 1);
|
||||
output = new Junction(this, "", 2, 1);
|
||||
common = new Junction(this, "", JUNCTION_LEFT, HALF);
|
||||
output = new Junction(this, "", JUNCTION_RIGHT, HALF);
|
||||
junctions.add(common);
|
||||
junctions.add(output);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
super(dto.getName(), dto.getX(), dto.getY());
|
||||
common = new Junction(this, dto.getCommon());
|
||||
output = new Junction(this, dto.getOutput());
|
||||
junctions.add(common);
|
||||
@ -36,10 +41,6 @@ public class PartSwitch1x1 extends PartOther {
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common) {
|
||||
@ -53,4 +54,18 @@ public class PartSwitch1x1 extends PartOther {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _paint(final Graphics2D g) {
|
||||
if (!state) {
|
||||
line(g, common.getX(), common.getY(), JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
|
||||
} else {
|
||||
line(g, common, output, common.getColor(), SWITCH_STROKE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
package de.ph87.electro.circuit.part.impl;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitch1x2Dto;
|
||||
@ -8,6 +8,10 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitch1x2 extends PartOther {
|
||||
@ -19,20 +23,21 @@ public class PartSwitch1x2 extends PartOther {
|
||||
private final Junction output1;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
private boolean state;
|
||||
|
||||
public PartSwitch1x2(final String name, final int x, final int y) {
|
||||
public PartSwitch1x2(final String name, final int x, final int y, final boolean state) {
|
||||
super(name, x, y);
|
||||
common = new Junction(this, "", 0, 1);
|
||||
output0 = new Junction(this, "", 2, 1);
|
||||
output1 = new Junction(this, "", 2, 1);
|
||||
common = new Junction(this, "", JUNCTION_LEFT, HALF);
|
||||
output0 = new Junction(this, "", JUNCTION_RIGHT, FOURTH1);
|
||||
output1 = new Junction(this, "", JUNCTION_RIGHT, FOURTH3);
|
||||
junctions.add(common);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
super(dto.getName(), dto.getX(), dto.getY());
|
||||
common = new Junction(this, dto.getCommon());
|
||||
output0 = new Junction(this, dto.getOutput0());
|
||||
output1 = new Junction(this, dto.getOutput1());
|
||||
@ -42,10 +47,6 @@ public class PartSwitch1x2 extends PartOther {
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common) {
|
||||
@ -65,4 +66,18 @@ public class PartSwitch1x2 extends PartOther {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _paint(final Graphics2D g) {
|
||||
if (!state) {
|
||||
line(g, common, output0, common.getColor(), SWITCH_STROKE);
|
||||
} else {
|
||||
line(g, common, output1, common.getColor(), SWITCH_STROKE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
package de.ph87.electro.circuit.part.impl;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitchCrossDto;
|
||||
@ -8,6 +8,10 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitchCross extends PartOther {
|
||||
@ -21,22 +25,23 @@ public class PartSwitchCross extends PartOther {
|
||||
private final Junction output1;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
private boolean state;
|
||||
|
||||
public PartSwitchCross(final String name, final int x, final int y) {
|
||||
public PartSwitchCross(final String name, final int x, final int y, final boolean state) {
|
||||
super(name, x, y);
|
||||
common0 = new Junction(this, "", 0, 1);
|
||||
common1 = new Junction(this, "", 0, 1);
|
||||
output0 = new Junction(this, "", 2, 1);
|
||||
output1 = new Junction(this, "", 2, 1);
|
||||
common0 = new Junction(this, "", JUNCTION_LEFT, FOURTH1);
|
||||
common1 = new Junction(this, "", JUNCTION_LEFT, FOURTH3);
|
||||
output0 = new Junction(this, "", JUNCTION_RIGHT, FOURTH1);
|
||||
output1 = new Junction(this, "", JUNCTION_RIGHT, FOURTH3);
|
||||
junctions.add(common0);
|
||||
junctions.add(common1);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public PartSwitchCross(final PartSwitchCrossDto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
super(dto.getName(), dto.getX(), dto.getY());
|
||||
common0 = new Junction(this, dto.getCommon0());
|
||||
common1 = new Junction(this, dto.getCommon1());
|
||||
output0 = new Junction(this, dto.getOutput0());
|
||||
@ -48,10 +53,6 @@ public class PartSwitchCross extends PartOther {
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common0) {
|
||||
@ -81,4 +82,20 @@ public class PartSwitchCross extends PartOther {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _paint(final Graphics2D g) {
|
||||
if (!state) {
|
||||
line(g, common0, output0, common0.getColor(), SWITCH_STROKE);
|
||||
line(g, common1, output1, common1.getColor(), SWITCH_STROKE);
|
||||
} else {
|
||||
line(g, common0, output1, common0.getColor(), SWITCH_STROKE);
|
||||
line(g, common1, output0, common1.getColor(), SWITCH_STROKE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartLight extends PartOther {
|
||||
|
||||
private final Junction minus;
|
||||
|
||||
private final Junction plus;
|
||||
|
||||
private final double maxVoltage;
|
||||
|
||||
private boolean defect = false;
|
||||
|
||||
private double voltage = 0;
|
||||
|
||||
public PartLight(final String name, final int x, final int y, final double initialMaxVoltage) {
|
||||
super(name, x, y);
|
||||
minus = new Junction(this, "", 0, 1);
|
||||
plus = new Junction(this, "", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
maxVoltage = initialMaxVoltage;
|
||||
}
|
||||
|
||||
public PartLight(final PartLightDto dto) {
|
||||
super(dto);
|
||||
minus = new Junction(this, dto.getMinus());
|
||||
plus = new Junction(this, dto.getPlus());
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
maxVoltage = dto.getMaxVoltage();
|
||||
defect = dto.isDefect();
|
||||
}
|
||||
|
||||
public void repair() {
|
||||
defect = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
voltage = abs(plus.getVoltage() - minus.getVoltage());
|
||||
if (voltage > maxVoltage) {
|
||||
defect = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
34
src/main/java/de/ph87/electro/demo/DemoAll.java
Normal file
34
src/main/java/de/ph87/electro/demo/DemoAll.java
Normal file
@ -0,0 +1,34 @@
|
||||
package de.ph87.electro.demo;
|
||||
|
||||
import de.ph87.electro.circuit.Circuit;
|
||||
import de.ph87.electro.circuit.part.impl.*;
|
||||
|
||||
public class DemoAll {
|
||||
|
||||
public static Circuit create() {
|
||||
final double voltage = 3.0;
|
||||
|
||||
final Circuit circuit = new Circuit();
|
||||
|
||||
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 2, voltage);
|
||||
final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ausschalter", 2, 0, 0, false);
|
||||
final PartLight light = circuit.addLight("Licht", 4, 0, 0, voltage);
|
||||
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 2, 0, false);
|
||||
final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 2, 2, 0, false);
|
||||
final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 4, 2, 2, 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());
|
||||
|
||||
circuit.evaluate();
|
||||
return circuit;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -14,14 +14,14 @@ public class BatteryLightTest {
|
||||
|
||||
private static final Circuit circuit = new Circuit();
|
||||
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
||||
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
circuit.connect(battery.getPlus(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
circuit.connect(battery.getPlus(), light.getPin1());
|
||||
circuit.connect(light.getPin0(), battery.getMinus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -29,10 +29,10 @@ public class BatteryLightTest {
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, light.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, light.getPin1().getVoltage());
|
||||
assertEquals(VOLTAGE, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, light.getPin0().getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x1;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -15,17 +15,17 @@ public class BatterySwitcher1x1Test {
|
||||
|
||||
private static final Circuit circuit = new Circuit();
|
||||
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
||||
|
||||
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 1, 1);
|
||||
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, 0, false);
|
||||
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
circuit.connect(battery.getPlus(), switcher.getCommon());
|
||||
circuit.connect(switcher.getOutput(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
circuit.connect(switcher.getOutput(), light.getPin1());
|
||||
circuit.connect(light.getPin0(), battery.getMinus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,10 +51,10 @@ public class BatterySwitcher1x1Test {
|
||||
assertEquals(VOLTAGE, switcher.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher.getOutput().getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getPin1().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, light.getPin0().getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -15,21 +15,21 @@ public class BatterySwitcher1x2Test {
|
||||
|
||||
private static final Circuit circuit = new Circuit();
|
||||
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
||||
|
||||
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 1, 1);
|
||||
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, 0, false);
|
||||
|
||||
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, VOLTAGE);
|
||||
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, 0, VOLTAGE);
|
||||
|
||||
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 1, VOLTAGE);
|
||||
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, 0, VOLTAGE);
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
circuit.connect(battery.getPlus(), switcher.getCommon());
|
||||
circuit.connect(switcher.getOutput0(), light0.getPlus());
|
||||
circuit.connect(light0.getMinus(), battery.getMinus());
|
||||
circuit.connect(switcher.getOutput1(), light1.getPlus());
|
||||
circuit.connect(light1.getMinus(), battery.getMinus());
|
||||
circuit.connect(switcher.getOutput0(), light0.getPin1());
|
||||
circuit.connect(light0.getPin0(), battery.getMinus());
|
||||
circuit.connect(switcher.getOutput1(), light1.getPin1());
|
||||
circuit.connect(light1.getPin0(), battery.getMinus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -56,15 +56,15 @@ public class BatterySwitcher1x2Test {
|
||||
assertEquals(VOLTAGE, switcher.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage0, switcher.getOutput0().getVoltage());
|
||||
assertEquals(voltage0, light0.getPlus().getVoltage());
|
||||
assertEquals(voltage0, light0.getPin1().getVoltage());
|
||||
assertEquals(voltage0, light0.getVoltage());
|
||||
|
||||
assertEquals(voltage1, switcher.getOutput1().getVoltage());
|
||||
assertEquals(voltage1, light1.getPlus().getVoltage());
|
||||
assertEquals(voltage1, light1.getPin1().getVoltage());
|
||||
assertEquals(voltage1, light1.getVoltage());
|
||||
|
||||
assertEquals(0, light0.getMinus().getVoltage());
|
||||
assertEquals(0, light1.getMinus().getVoltage());
|
||||
assertEquals(0, light0.getPin0().getVoltage());
|
||||
assertEquals(0, light1.getPin0().getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
|
||||
assertFalse(light0.isDefect());
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -15,21 +15,21 @@ public class BatterySwitcher2x2Test {
|
||||
|
||||
private static final Circuit circuit = new Circuit();
|
||||
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
|
||||
|
||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 1, 1);
|
||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
|
||||
|
||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1);
|
||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, 0, false);
|
||||
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 0, 0, VOLTAGE);
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
circuit.connect(battery.getPlus(), switcher0.getCommon());
|
||||
circuit.connect(switcher0.getOutput0(), switcher1.getOutput0());
|
||||
circuit.connect(switcher0.getOutput1(), switcher1.getOutput1());
|
||||
circuit.connect(switcher1.getCommon(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
circuit.connect(switcher1.getCommon(), light.getPin1());
|
||||
circuit.connect(light.getPin0(), battery.getMinus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -75,10 +75,10 @@ public class BatterySwitcher2x2Test {
|
||||
assertEquals(voltage1, switcher1.getOutput1().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher1.getCommon().getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getPin1().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, light.getPin0().getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.impl.PartSwitchCross;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -16,15 +16,15 @@ public class BatterySwitcherCrossTest {
|
||||
|
||||
private static final Circuit circuit = new Circuit();
|
||||
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
|
||||
|
||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 1, 1);
|
||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
|
||||
|
||||
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1);
|
||||
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, 0, false);
|
||||
|
||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1);
|
||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, 0, false);
|
||||
|
||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
private static final PartLight light = circuit.addLight("Licht", 2, 0, 0, VOLTAGE);
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
@ -33,8 +33,8 @@ public class BatterySwitcherCrossTest {
|
||||
circuit.connect(switcher0.getOutput1(), switcherX.getCommon1());
|
||||
circuit.connect(switcherX.getOutput0(), switcher1.getOutput0());
|
||||
circuit.connect(switcherX.getOutput1(), switcher1.getOutput1());
|
||||
circuit.connect(switcher1.getCommon(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
circuit.connect(switcher1.getCommon(), light.getPin1());
|
||||
circuit.connect(light.getPin0(), battery.getMinus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -110,10 +110,10 @@ public class BatterySwitcherCrossTest {
|
||||
assertEquals(voltageX1, switcher1.getOutput1().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher1.getCommon().getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getPin1().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, light.getPin0().getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
@ -2,8 +2,8 @@ package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@ -18,10 +18,10 @@ class CircuitServiceTest {
|
||||
@Test
|
||||
void serialization() throws IOException {
|
||||
final Circuit circuit = new Circuit();
|
||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 3.0);
|
||||
final PartLight light = circuit.addLight("Licht", 1, 1, 3.0);
|
||||
circuit.connect(battery.getPlus(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 1, 3.0);
|
||||
final PartLight light = circuit.addLight("Licht", 1, 1, 0, 3.0);
|
||||
circuit.connect(battery.getPlus(), light.getPin1());
|
||||
circuit.connect(light.getPin0(), battery.getMinus());
|
||||
check(circuit);
|
||||
}
|
||||
|
||||
@ -42,14 +42,17 @@ class CircuitServiceTest {
|
||||
final Part reloadedPart = reloaded.getParts().stream().filter(part -> part.getUuid().equals(originalPart.getUuid())).findFirst().orElseThrow();
|
||||
assertEquals(originalPart.getUuid(), reloadedPart.getUuid());
|
||||
assertEquals(originalPart.getName(), reloadedPart.getName());
|
||||
assertEquals(originalPart.getPosition(), reloadedPart.getPosition());
|
||||
assertEquals(originalPart.getX(), reloadedPart.getX());
|
||||
assertEquals(originalPart.getY(), reloadedPart.getY());
|
||||
assertEquals(originalPart.getRotation(), reloadedPart.getRotation());
|
||||
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
|
||||
for (final Junction originalJunction : originalPart.getJunctions()) {
|
||||
System.out.printf(" - Junction: %s\n", originalJunction.getUuid());
|
||||
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
||||
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
||||
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
||||
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
||||
assertEquals(originalJunction.getX(), reloadedJunction.getX());
|
||||
assertEquals(originalJunction.getY(), reloadedJunction.getY());
|
||||
assertEquals(originalJunction.getDestinations().size(), reloadedJunction.getDestinations().size());
|
||||
for (final Junction originalDestination : originalJunction.getDestinations()) {
|
||||
System.out.printf(" - Destination: %s\n", originalDestination.getUuid());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user