This commit is contained in:
Patrick Haßel 2024-05-17 09:58:26 +02:00
parent 3591741ee5
commit 4fc22e961c
14 changed files with 138 additions and 135 deletions

View File

@ -12,15 +12,19 @@ public class CONFIG {
public static final int RASTER = 200;
public static final int HALF = (int) round(0.5 * RASTER);
public static final int P05 = (int) round(0.05 * RASTER);
public static final int FOURTH1 = (int) round(0.25 * RASTER);
public static final int P10 = (int) round(0.1 * RASTER);
public static final int FOURTH3 = (int) round(0.75 * RASTER);
public static final int P25 = (int) round(0.25 * RASTER);
public static final int JUNCTION_LEFT = (int) round(0.1 * RASTER);
public static final int P50 = (int) round(0.5 * RASTER);
public static final int JUNCTION_RIGHT = (int) round(0.9 * RASTER);
public static final int P75 = (int) round(0.75 * RASTER);
public static final int P90 = (int) round(0.9 * RASTER);
public static final int P95 = (int) round(0.95 * RASTER);
public static final int JUNCTION_RADIUS = (int) round(0.05 * RASTER);
@ -38,6 +42,8 @@ public class CONFIG {
public static final Color VOLTAGE_LOW_COLOR = new Color(0, 128, 255);
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 NORMAL_STROKE = new BasicStroke(1);

View File

@ -143,7 +143,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
if (dragPosition != null) {
if (partDrag != null) {
g.setColor(PART_HOVER_COLOR);
g.fillRect(dragPosition.x - HALF, dragPosition.y - HALF, RASTER, RASTER);
g.fillRect(dragPosition.x - P50, dragPosition.y - P50, RASTER, RASTER);
}
if (junctionDrag != null) {
junctionDrag.getOwner().line(g, junctionDrag.getAbsolute(), dragPosition, junctionDrag.getColor(), WIRE_STROKE);

View File

@ -9,9 +9,9 @@ import static java.lang.Math.PI;
@SuppressWarnings("SuspiciousNameCombination")
public enum Orientation {
R0(0, p -> new Point(p.x, p.y)),
R1(1, p -> new Point(RASTER - p.y, p.x)),
R2(2, p -> new Point(RASTER - p.x, RASTER - p.y)),
R3(3, p -> new Point(p.y, RASTER - p.x)),
R90(1, p -> new Point(RASTER - p.y, p.x)),
R180(2, p -> new Point(RASTER - p.x, RASTER - p.y)),
R270(3, p -> new Point(p.y, RASTER - p.x)),
;
private final Function<Point, Point> map;

View File

@ -8,6 +8,7 @@ import lombok.Setter;
import lombok.ToString;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
@ -74,8 +75,15 @@ public abstract class Part {
public void render() {
final Graphics2D g = (Graphics2D) render.getGraphics();
g.rotate(orientation.getTheta(), HALF, HALF);
g.rotate(orientation.getTheta(), P50, P50);
rect(g, 0, 0, RASTER, RASTER, null, null, PART_BACKGROUND);
if (getOrientation() == Orientation.R180) {
g.rotate(getOrientation().getTheta(), P50, P50);
}
_labels(g);
if (getOrientation() == Orientation.R180) {
g.rotate(-getOrientation().getTheta(), P50, P50);
}
_render(g);
junctions.forEach(junction -> junction.render(g));
}
@ -146,6 +154,13 @@ public abstract class Part {
}
}
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,
@ -157,11 +172,21 @@ public abstract class Part {
return junctions.stream().filter(junction -> junction.intersects(absolute)).findFirst();
}
public abstract void action();
public void action() {
// may be overwritten
}
public abstract void propagate(final Junction source) throws ShortCircuit;
public void propagate(final Junction source) throws ShortCircuit {
// may be overwritten
}
protected abstract void _render(final Graphics2D g);
protected void _render(final Graphics2D g) {
// may be overwritten
}
protected void _labels(final Graphics2D g) {
// may be overwritten
}
public static Part of(final PartDto abstractDto) {
return switch (abstractDto) {

View File

@ -42,16 +42,18 @@ public class PartBattery extends Part {
public PartBattery(final String name, final Point position, final Orientation orientation, final double initialVoltage) {
super(name, position, orientation);
minus = newJunction(this, "-", JUNCTION_LEFT, HALF);
plus = newJunction(this, "+", JUNCTION_RIGHT, HALF);
minus = newJunction(this, "-", P10, P50);
plus = newJunction(this, "+", P90, P50);
voltage = initialVoltage;
startPropagation();
}
public PartBattery(final PartBatteryDto dto) {
super(dto);
minus = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
plus = newJunction(this, dto.getPlus(), JUNCTION_RIGHT, HALF);
minus = newJunction(this, dto.getMinus(), P10, P50);
plus = newJunction(this, dto.getPlus(), P90, P50);
voltage = dto.getVoltage();
startPropagation();
}
public void startPropagation() {
@ -64,22 +66,29 @@ public class PartBattery extends Part {
}
}
@Override
public void propagate(final Junction source) {
// nothing
}
@Override
protected void _render(final Graphics2D g) {
line(g, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE);
line(g, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE);
rect(g, HALF - MINUS_W - GAP / 2, HALF - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
rect(g, HALF + GAP / 2, HALF - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
line(g, P10, P50, P50 - GAP / 2 - MINUS_W / 2, P50, Color.BLACK, SYMBOL_STROKE);
line(g, P50 + GAP / 2 + PLUS_W / 2, P50, P90, P50, Color.BLACK, SYMBOL_STROKE);
rect(g, P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
rect(g, P50 + GAP / 2, P50 - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
}
@Override
public void action() {
// nothing
protected void _labels(final Graphics2D g) {
final int y0;
final int y1;
if (getOrientation() == Orientation.R180) {
y0 = P05;
y1 = P95 - LABEL_FONT.getSize();
} else {
y0 = P95 - LABEL_FONT.getSize();
y1 = P05;
}
textCenter(g, LABEL_FONT, "%.1fV".formatted(voltage), y0, Color.BLACK);
if (shortCircuit != null) {
textCenter(g, LABEL_FONT, "KURZSCHLUSS", y1, Color.RED);
}
}
}

View File

@ -25,29 +25,14 @@ public class PartJunctionCorner extends PartOther {
public PartJunctionCorner(final String name, final Point position, final Orientation orientation) {
super(name, position, orientation);
j0 = newJunction(this, "", JUNCTION_LEFT, JUNCTION_LEFT);
j1 = newJunction(this, "", JUNCTION_RIGHT, JUNCTION_RIGHT);
j0 = newJunction(this, "", P10, P10);
j1 = newJunction(this, "", P90, P90);
}
public PartJunctionCorner(final PartSwitchCrossDto dto) {
super(dto);
j0 = newJunction(this, dto.getCommon0(), JUNCTION_LEFT, FOURTH1);
j1 = newJunction(this, dto.getOutput1(), JUNCTION_RIGHT, FOURTH3);
}
@Override
public void propagate(final Junction source) {
// nothing
}
@Override
protected void _render(final Graphics2D g) {
// nothing
}
@Override
public void action() {
// nothing
j0 = newJunction(this, dto.getCommon0(), P10, P25);
j1 = newJunction(this, dto.getOutput1(), P90, P75);
}
}

View File

@ -9,8 +9,8 @@ import lombok.ToString;
import java.awt.*;
import static de.ph87.electro.CONFIG.HALF;
import static de.ph87.electro.CONFIG.JUNCTION_LEFT;
import static de.ph87.electro.CONFIG.P50;
import static de.ph87.electro.CONFIG.P10;
@Getter
@ToString(callSuper = true)
@ -26,29 +26,14 @@ public class PartJunctionEdge extends PartOther {
public PartJunctionEdge(final String name, final Point position, final Orientation orientation) {
super(name, position, orientation);
j0 = newJunction(this, "", JUNCTION_LEFT, HALF);
j1 = newJunction(this, "", HALF, JUNCTION_LEFT);
j0 = newJunction(this, "", P10, P50);
j1 = newJunction(this, "", P50, P10);
}
public PartJunctionEdge(final PartSwitchCrossDto dto) {
super(dto);
j0 = newJunction(this, dto.getCommon0(), 0, HALF);
j1 = newJunction(this, dto.getOutput1(), HALF, 0);
}
@Override
public void propagate(final Junction source) {
// nothing
}
@Override
protected void _render(final Graphics2D g) {
// nothing
}
@Override
public void action() {
// nothing
j0 = newJunction(this, dto.getCommon0(), 0, P50);
j1 = newJunction(this, dto.getOutput1(), P50, 0);
}
}

View File

@ -9,7 +9,7 @@ import lombok.ToString;
import java.awt.*;
import static de.ph87.electro.CONFIG.HALF;
import static de.ph87.electro.CONFIG.P50;
@Getter
@ToString(callSuper = true)
@ -23,27 +23,12 @@ public class PartJunctionMiddle extends PartOther {
public PartJunctionMiddle(final String name, final Point position, final Orientation orientation) {
super(name, position, orientation);
junction = newJunction(this, "", HALF, HALF);
junction = newJunction(this, "", P50, P50);
}
public PartJunctionMiddle(final PartSwitchCrossDto dto) {
super(dto);
junction = newJunction(this, dto.getCommon0(), HALF, HALF);
}
@Override
public void propagate(final Junction source) {
// nothing
}
@Override
protected void _render(final Graphics2D g) {
// nothing
}
@Override
public void action() {
// nothing
junction = newJunction(this, dto.getCommon0(), P50, P50);
}
}

View File

@ -31,7 +31,7 @@ public class PartLight extends PartOther {
private static final int BULB_RADIUS = (int) round(0.25 * RASTER);
public static final Color BULB_OFF_COLOR = Color.DARK_GRAY;
private static final Color BULB_OFF_COLOR = Color.DARK_GRAY;
private final Junction pin0;
@ -55,19 +55,24 @@ public class PartLight extends PartOther {
super(name, position, orientation);
this.minVoltage = minVoltage;
this.maxVoltage = maxVoltage;
pin0 = newJunction(this, "", JUNCTION_LEFT, HALF);
pin1 = newJunction(this, "", JUNCTION_RIGHT, HALF);
pin0 = newJunction(this, "", P10, P50);
pin1 = newJunction(this, "", P90, P50);
}
public PartLight(final PartLightDto dto) {
super(dto);
pin0 = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
pin1 = newJunction(this, dto.getPlus(), JUNCTION_RIGHT, HALF);
pin0 = newJunction(this, dto.getMinus(), P10, P50);
pin1 = newJunction(this, dto.getPlus(), P90, P50);
minVoltage = dto.getMinVoltage();
maxVoltage = dto.getMaxVoltage();
defect = dto.isDefect();
}
@Override
public void action() {
defect = false;
}
@Override
public void propagate(final Junction source) {
voltage = abs(pin1.getVoltage() - pin0.getVoltage());
@ -89,7 +94,7 @@ public class PartLight extends PartOther {
@Override
protected void _render(final Graphics2D g) {
line(g, pin0, pin1, Color.BLACK, SYMBOL_STROKE);
circle(g, HALF, HALF, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
circle(g, P50, P50, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
final double diag = 0.33 * RASTER;
line(g, diag, diag, RASTER - diag, RASTER - diag, Color.BLACK, SYMBOL_STROKE);
@ -97,8 +102,11 @@ public class PartLight extends PartOther {
}
@Override
public void action() {
defect = false;
protected void _labels(final Graphics2D g) {
textCenter(g, LABEL_FONT, "%.1fV (max)".formatted(maxVoltage), P05, Color.BLACK);
if (defect) {
textCenter(g, LABEL_FONT, "DEFEKT", P95 - LABEL_FONT.getSize(), Color.RED);
}
}
}

View File

@ -30,18 +30,23 @@ public class PartSwitch1x1 extends PartOther {
public PartSwitch1x1(final String name, final Point position, final Orientation orientation, final boolean state) {
super(name, position, orientation);
common = newJunction(this, "", JUNCTION_LEFT, HALF);
output = newJunction(this, "", JUNCTION_RIGHT, HALF);
common = newJunction(this, "", P10, P50);
output = newJunction(this, "", P90, P50);
this.state = state;
}
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
super(dto);
common = newJunction(this, dto.getCommon(), JUNCTION_LEFT, HALF);
output = newJunction(this, dto.getOutput(), JUNCTION_RIGHT, HALF);
common = newJunction(this, dto.getCommon(), P10, P50);
output = newJunction(this, dto.getOutput(), P90, P50);
state = dto.isState();
}
@Override
public void action() {
state = !state;
}
@Override
public void propagate(final Junction source) throws ShortCircuit {
if (source == common) {
@ -58,15 +63,10 @@ public class PartSwitch1x1 extends PartOther {
@Override
protected void _render(final Graphics2D g) {
if (!state) {
line(g, common, JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
line(g, common, P90, P25, common.getColor(), SWITCH_STROKE);
} else {
line(g, common, output, common.getColor(), SWITCH_STROKE);
}
}
@Override
public void action() {
state = !state;
}
}

View File

@ -32,20 +32,25 @@ public class PartSwitch1x2 extends PartOther {
public PartSwitch1x2(final String name, final Point position, final Orientation orientation, final boolean state) {
super(name, position, orientation);
common = newJunction(this, "", JUNCTION_LEFT, HALF);
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
common = newJunction(this, "", P10, P50);
output0 = newJunction(this, "", P90, P25);
output1 = newJunction(this, "", P90, P75);
this.state = state;
}
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
super(dto);
common = newJunction(this, dto.getCommon(), JUNCTION_LEFT, HALF);
output0 = newJunction(this, dto.getOutput0(), JUNCTION_RIGHT, FOURTH1);
output1 = newJunction(this, dto.getOutput1(), JUNCTION_RIGHT, FOURTH3);
common = newJunction(this, dto.getCommon(), P10, P50);
output0 = newJunction(this, dto.getOutput0(), P90, P25);
output1 = newJunction(this, dto.getOutput1(), P90, P75);
state = dto.isState();
}
@Override
public void action() {
state = !state;
}
@Override
public void propagate(final Junction source) throws ShortCircuit {
if (source == common) {
@ -74,9 +79,4 @@ public class PartSwitch1x2 extends PartOther {
}
}
@Override
public void action() {
state = !state;
}
}

View File

@ -34,22 +34,27 @@ public class PartSwitchCross extends PartOther {
public PartSwitchCross(final String name, final Point position, final Orientation orientation, final boolean state) {
super(name, position, orientation);
common0 = newJunction(this, "", JUNCTION_LEFT, FOURTH1);
common1 = newJunction(this, "", JUNCTION_LEFT, FOURTH3);
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
common0 = newJunction(this, "", P10, P25);
common1 = newJunction(this, "", P10, P75);
output0 = newJunction(this, "", P90, P25);
output1 = newJunction(this, "", P90, P75);
this.state = state;
}
public PartSwitchCross(final PartSwitchCrossDto dto) {
super(dto);
common0 = newJunction(this, dto.getCommon0(), JUNCTION_LEFT, FOURTH1);
common1 = newJunction(this, dto.getCommon1(), JUNCTION_LEFT, FOURTH3);
output0 = newJunction(this, dto.getOutput0(), JUNCTION_RIGHT, FOURTH1);
output1 = newJunction(this, dto.getOutput1(), JUNCTION_RIGHT, FOURTH3);
common0 = newJunction(this, dto.getCommon0(), P10, P25);
common1 = newJunction(this, dto.getCommon1(), P10, P75);
output0 = newJunction(this, dto.getOutput0(), P90, P25);
output1 = newJunction(this, dto.getOutput1(), P90, P75);
state = dto.isState();
}
@Override
public void action() {
state = !state;
}
@Override
public void propagate(final Junction source) throws ShortCircuit {
if (source == common0) {
@ -90,9 +95,4 @@ public class PartSwitchCross extends PartOther {
}
}
@Override
public void action() {
state = !state;
}
}

View File

@ -12,12 +12,12 @@ public class DemoAll {
public static Circuit complexTrippleAndSimple() {
final Circuit circuit = new Circuit();
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R2, VOLTAGE);
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.R2, true);
final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 4, 2, Orientation.R180, true);
circuit.connect(battery.getMinus(), switcher.getCommon());
circuit.connect(switcher.getOutput(), light.getPin0());
@ -35,10 +35,10 @@ public class DemoAll {
public static Circuit simpleAlternative() {
final Circuit circuit = new Circuit();
final PartBattery battery = circuit.addBattery("Batterie", 2, 0, Orientation.R1, VOLTAGE);
final PartLight light0 = circuit.addLight("Licht 0", 4, 2, Orientation.R1, BULB_VOLTAGE_MIN, VOLTAGE);
final PartLight light1 = circuit.addLight("Licht 1", 0, 2, Orientation.R1, BULB_VOLTAGE_MIN, VOLTAGE);
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 2, 2, Orientation.R1, false);
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());

View File

@ -21,7 +21,7 @@ class CircuitServiceTest {
@Test
void serialization() throws IOException {
final Circuit circuit = new Circuit();
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R1, VOLTAGE);
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R90, VOLTAGE);
final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, BULB_VOLTAGE_MIN, VOLTAGE);
circuit.connect(battery.getPlus(), light.getPin1());
circuit.connect(light.getPin0(), battery.getMinus());