This commit is contained in:
Patrick Haßel 2024-05-22 16:27:53 +02:00
parent 42de5cd032
commit 8da0cf8071
21 changed files with 182 additions and 84 deletions

View File

@ -2,6 +2,7 @@ package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.parts.Battery;
import de.ph87.electro.circuit.part.parts.Light;
import de.ph87.electro.circuit.part.parts.Poti;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
@ -19,10 +20,16 @@ public class CircuitPanel extends JPanel {
public CircuitPanel() {
new CircuitPanelDropTarget(this, circuit);
final Battery battery = circuit.addPart(new Battery(RST(0, 0)));
final Light light = circuit.addPart(new Light(RST(0, 1)));
final Battery battery = circuit.addPart(new Battery(RST(2, 0)));
final Light light = circuit.addPart(new Light(RST(0, 2)));
final Poti poti = circuit.addPart(new Poti(RST(2, 2)));
poti.rotate();
poti.rotate();
light.rotate();
circuit.connect(battery.getMinus(), light.getA());
circuit.connect(light.getB(), battery.getPlus());
circuit.connect(battery.getMinus(), poti.getEnd());
circuit.connect(battery.getPlus(), poti.getCommon());
circuit.connect(poti.getMiddle(), light.getB());
}
@Override

View File

@ -38,6 +38,8 @@ public class CircuitPanelDropTarget extends AbstractDropTarget {
circuit.addPart(new Switch1x2(position));
} else if (data.equals(SwitchCross.class.getSimpleName())) {
circuit.addPart(new SwitchCross(position));
} else if (data.equals(Poti.class.getSimpleName())) {
circuit.addPart(new Poti(position));
} else {
throw new RuntimeException();
}

View File

@ -60,7 +60,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
circuitPanel.repaint();
break;
case BUTTON3:
part.clockwise();
part.rotate();
circuitPanel.repaint();
break;
}

View File

@ -47,12 +47,12 @@ public abstract class Part {
setPosition(new Position(dto.getPosition()));
}
protected Junction newJunction(final Part owner, final String name, final int x, final int y) {
return addJunction(new Junction(owner, name, new Point(x, y)));
protected Junction addJunction(final String name, final int x, final int y) {
return addJunction(new Junction(this, name, new Point(x, y)));
}
protected Junction newJunction(final Part owner, final JunctionDto dto, final int x, final int y) {
return addJunction(new Junction(owner, dto, new Point(x, y)));
protected Junction addJunction(final JunctionDto dto, final int x, final int y) {
return addJunction(new Junction(this, dto, new Point(x, y)));
}
private Junction addJunction(final Junction junction) {
@ -65,7 +65,7 @@ public abstract class Part {
junctions.forEach(Junction::updatePosition);
}
public void clockwise() {
public void rotate() {
orientation = orientation.clockwise();
junctions.forEach(Junction::updatePosition);
render();

View File

@ -39,6 +39,7 @@ public abstract class PartDto {
case final Switch1x1 part -> new Switch1x1Dto(part);
case final Switch1x2 part -> new Switch1x2Dto(part);
case final SwitchCross part -> new SwitchCrossDto(part);
case final Poti part -> new PotiDto(part);
case null, default -> throw new RuntimeException();
};
}

View File

@ -1,13 +0,0 @@
package de.ph87.electro.circuit.part;
public abstract class PartOther extends Part {
protected PartOther(final String name, final Position position) {
super(name, position);
}
protected PartOther(final PartDto dto) {
super(dto);
}
}

View File

@ -65,8 +65,9 @@ public class Junction {
this.color = Double.isNaN(voltage) ? VOLTAGE_UNKNOWN_COLOR : ((voltage >= VOLTAGE_HIGH_MIN) ? VOLTAGE_HIGH_COLOR : VOLTAGE_LOW_COLOR);
}
public void render(final Render g) {
g.circle(position, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color);
public void render(final Render render) {
render.circle(position, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color);
// render.textCenter(LABEL_FONT, name, position.inside.x, position.inside.y, Color.WHITE);
}
public void updatePosition() {

View File

@ -42,14 +42,14 @@ public class Battery extends Part {
public Battery(final Position position) {
super("Batterie", position);
minus = newJunction(this, "MINUS", P10, P50);
plus = newJunction(this, "PLUS", P90, P50);
minus = addJunction("MINUS", P10, P50);
plus = addJunction("PLUS", P90, P50);
}
public Battery(final BatteryDto dto) {
super(dto);
minus = newJunction(this, dto.getMinus(), P10, P50);
plus = newJunction(this, dto.getPlus(), P90, P50);
minus = addJunction(dto.getMinus(), P10, P50);
plus = addJunction(dto.getPlus(), P90, P50);
voltage = dto.getVoltage();
}

View File

@ -1,6 +1,6 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -11,7 +11,7 @@ import static de.ph87.electro.CONFIG.P90;
@Getter
@ToString(callSuper = true)
public class ConnectorCorner extends PartOther {
public class ConnectorCorner extends Part {
private final Junction j0;
@ -19,14 +19,14 @@ public class ConnectorCorner extends PartOther {
public ConnectorCorner(final Position position) {
super("", position);
j0 = newJunction(this, "J0", P10, P10);
j1 = newJunction(this, "J1", P90, P90);
j0 = addJunction("J0", P10, P10);
j1 = addJunction("J1", P90, P90);
}
public ConnectorCorner(final ConnectorCornerDto dto) {
super(dto);
j0 = newJunction(this, dto.getJ0(), P10, P10);
j1 = newJunction(this, dto.getJ1(), P90, P90);
j0 = addJunction(dto.getJ0(), P10, P10);
j1 = addJunction(dto.getJ1(), P90, P90);
}
}

View File

@ -1,6 +1,6 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -11,7 +11,7 @@ import static de.ph87.electro.CONFIG.P50;
@Getter
@ToString(callSuper = true)
public class ConnectorEdge extends PartOther {
public class ConnectorEdge extends Part {
private final Junction j0;
@ -19,14 +19,14 @@ public class ConnectorEdge extends PartOther {
public ConnectorEdge(final Position position) {
super("", position);
j0 = newJunction(this, "J0", P10, P50);
j1 = newJunction(this, "J1", P50, P10);
j0 = addJunction("J0", P10, P50);
j1 = addJunction("J1", P50, P10);
}
public ConnectorEdge(final ConnectorEdgeDto dto) {
super(dto);
j0 = newJunction(this, dto.getJ0(), P10, P50);
j1 = newJunction(this, dto.getJ1(), P50, P10);
j0 = addJunction(dto.getJ0(), P10, P50);
j1 = addJunction(dto.getJ1(), P50, P10);
}
}

View File

@ -1,6 +1,6 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -10,18 +10,18 @@ import static de.ph87.electro.CONFIG.P50;
@Getter
@ToString(callSuper = true)
public class ConnectorMiddle extends PartOther {
public class ConnectorMiddle extends Part {
private final Junction junction;
public ConnectorMiddle(final Position position) {
super("", position);
junction = newJunction(this, "J", P50, P50);
junction = addJunction("J", P50, P50);
}
public ConnectorMiddle(final ConnectorMiddleDto dto) {
super(dto);
junction = newJunction(this, dto.getJunction(), P50, P50);
junction = addJunction(dto.getJunction(), P50, P50);
}
}

View File

@ -2,7 +2,7 @@ package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.InnerConnection;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -19,7 +19,7 @@ import static java.lang.Math.round;
@Getter
@ToString(callSuper = true)
public class Light extends PartOther {
public class Light extends Part {
private static final Color COLOR_DEFECT = new Color(255, 0, 234);
@ -43,8 +43,6 @@ public class Light extends PartOther {
private double resistance = 15;
private double minVoltage = 1;
private double maxVoltage = 3;
private boolean defect = false;
@ -55,16 +53,15 @@ public class Light extends PartOther {
public Light(final Position position) {
super("Licht", position);
a = newJunction(this, "A", P10, P50);
b = newJunction(this, "B", P90, P50);
a = addJunction("A", P10, P50);
b = addJunction("B", P90, P50);
}
public Light(final LightDto dto) {
super(dto);
a = newJunction(this, dto.getB(), P10, P50);
b = newJunction(this, dto.getA(), P90, P50);
a = addJunction(dto.getB(), P10, P50);
b = addJunction(dto.getA(), P90, P50);
resistance = dto.getResistance();
minVoltage = dto.getMinVoltage();
maxVoltage = dto.getMaxVoltage();
defect = dto.isDefect();
}
@ -78,7 +75,7 @@ public class Light extends PartOther {
public void postCalculate() {
potentialDifference = abs(b.getVoltage() - a.getVoltage());
defect |= potentialDifference > maxVoltage;
if (defect || potentialDifference < minVoltage) {
if (defect) {
color = BULB_OFF_COLOR;
} else {
final int c = (int) round(255 * potentialDifference / maxVoltage);

View File

@ -17,8 +17,6 @@ public class LightDto extends PartDto {
private double resistance;
private double minVoltage;
private double maxVoltage;
private boolean defect;
@ -29,7 +27,6 @@ public class LightDto extends PartDto {
this.b = new JunctionDto(light.getA());
this.defect = light.isDefect();
this.resistance = light.getResistance();
this.minVoltage = light.getMinVoltage();
this.maxVoltage = light.getMaxVoltage();
}

View File

@ -0,0 +1,71 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.InnerConnection;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.awt.*;
import java.util.List;
import static de.ph87.electro.CONFIG.*;
import static de.ph87.electro.circuit.calculation.Calculation.NO_RESISTANCE;
import static java.lang.Math.max;
@Getter
@ToString(callSuper = true)
public class Poti extends Part {
private final Junction common;
private final Junction middle;
private final Junction end;
@Setter
private double resistance = 10;
@Setter
private double ratio = 0.0;
public Poti(final Position position) {
super("Poti", position);
common = addJunction("C", P10, P50);
middle = addJunction("M", P50, P10);
end = addJunction("E", P90, P50);
}
protected Poti(final PotiDto dto) {
super(dto);
common = addJunction(dto.getCommon().getName(), P10, P50);
middle = addJunction(dto.getMiddle().getName(), P50, P10);
end = addJunction(dto.getEnd().getName(), P90, P50);
}
@Override
public void action() {
if (ratio > 0.95) {
ratio = 0;
} else {
ratio += 0.1;
}
}
@Override
protected void _labels() {
render.textCenter(LABEL_FONT, "%3.0f%%".formatted(ratio * 100), P50, P50, Color.BLACK);
}
@Override
public List<InnerConnection> getInnerConnections() {
return List.of(
new InnerConnection(common, middle, max(NO_RESISTANCE, resistance * ratio)),
// new InnerConnection(common, end, max(NO_RESISTANCE, resistance)),
new InnerConnection(middle, end, max(NO_RESISTANCE, resistance * (1 - ratio)))
);
}
}

View File

@ -0,0 +1,35 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.PartDto;
import de.ph87.electro.circuit.part.junction.JunctionDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@ToString
@NoArgsConstructor
public class PotiDto extends PartDto {
private JunctionDto common;
private JunctionDto middle;
private JunctionDto end;
@Setter
private double resistance;
@Setter
private double ratio;
public PotiDto(final Poti poti) {
common = new JunctionDto(poti.getCommon());
middle = new JunctionDto(poti.getMiddle());
end = new JunctionDto(poti.getEnd());
resistance = poti.getResistance();
ratio = poti.getRatio();
}
}

View File

@ -1,7 +1,7 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.InnerConnection;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -16,7 +16,7 @@ import static de.ph87.electro.circuit.calculation.Calculation.NO_RESISTANCE;
@Getter
@ToString(callSuper = true)
public class Switch1x1 extends PartOther {
public class Switch1x1 extends Part {
private final Junction common;
@ -27,14 +27,14 @@ public class Switch1x1 extends PartOther {
public Switch1x1(final Position position) {
super("Ausschalter", position);
common = newJunction(this, "C", P10, P50);
output = newJunction(this, "O", P90, P50);
common = addJunction("C", P10, P50);
output = addJunction("O", P90, P50);
}
public Switch1x1(final Switch1x1Dto dto) {
super(dto);
common = newJunction(this, dto.getCommon(), P10, P50);
output = newJunction(this, dto.getOutput(), P90, P50);
common = addJunction(dto.getCommon(), P10, P50);
output = addJunction(dto.getOutput(), P90, P50);
state = dto.isState();
}

View File

@ -1,7 +1,7 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.InnerConnection;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -15,7 +15,7 @@ import static de.ph87.electro.circuit.calculation.Calculation.NO_RESISTANCE;
@Getter
@ToString(callSuper = true)
public class Switch1x2 extends PartOther {
public class Switch1x2 extends Part {
private final Junction common;
@ -28,16 +28,16 @@ public class Switch1x2 extends PartOther {
public Switch1x2(final Position position) {
super("Wechselschalter", position);
common = newJunction(this, "C", P10, P50);
output0 = newJunction(this, "O0", P90, P25);
output1 = newJunction(this, "O1", P90, P75);
common = addJunction("C", P10, P50);
output0 = addJunction("O0", P90, P25);
output1 = addJunction("O1", P90, P75);
}
public Switch1x2(final Switch1x2Dto dto) {
super(dto);
common = newJunction(this, dto.getCommon(), P10, P50);
output0 = newJunction(this, dto.getOutput0(), P90, P25);
output1 = newJunction(this, dto.getOutput1(), P90, P75);
common = addJunction(dto.getCommon(), P10, P50);
output0 = addJunction(dto.getOutput0(), P90, P25);
output1 = addJunction(dto.getOutput1(), P90, P75);
state = dto.isState();
}

View File

@ -1,7 +1,7 @@
package de.ph87.electro.circuit.part.parts;
import de.ph87.electro.circuit.part.InnerConnection;
import de.ph87.electro.circuit.part.PartOther;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.Position;
import de.ph87.electro.circuit.part.junction.Junction;
import lombok.Getter;
@ -15,7 +15,7 @@ import static de.ph87.electro.circuit.calculation.Calculation.NO_RESISTANCE;
@Getter
@ToString(callSuper = true)
public class SwitchCross extends PartOther {
public class SwitchCross extends Part {
private final Junction common0;
@ -30,18 +30,18 @@ public class SwitchCross extends PartOther {
public SwitchCross(final Position position) {
super("Kreuzschalter", position);
common0 = newJunction(this, "C0", P10, P25);
common1 = newJunction(this, "C1", P10, P75);
output0 = newJunction(this, "O0", P90, P25);
output1 = newJunction(this, "O1", P90, P75);
common0 = addJunction("C0", P10, P25);
common1 = addJunction("C1", P10, P75);
output0 = addJunction("O0", P90, P25);
output1 = addJunction("O1", P90, P75);
}
public SwitchCross(final SwitchCrossDto dto) {
super(dto);
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);
common0 = addJunction(dto.getCommon0(), P10, P25);
common1 = addJunction(dto.getCommon1(), P10, P75);
output0 = addJunction(dto.getOutput0(), P90, P25);
output1 = addJunction(dto.getOutput1(), P90, P75);
state = dto.isState();
}

View File

@ -30,8 +30,7 @@ public abstract class AbstractDropTarget extends DropTarget {
final boolean success = drop(event.getLocation(), data);
event.dropComplete(success);
} catch (Exception e) {
log.error(e.toString());
event.rejectDrop();
log.error("drop failed", e);
}
}

View File

@ -36,6 +36,7 @@ public class Sidebar extends JPanel {
add(new Switch1x1(Position.ZERO));
add(new Switch1x2(Position.ZERO));
add(new SwitchCross(Position.ZERO));
add(new Poti(Position.ZERO));
setPreferredSize(new Dimension(0, 200));
}

View File

@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class AssertHelper {
public static void assertVoltage(final double expected, final double actual) {
assertTrue(abs(expected - actual) <= 0.1);
assertTrue(abs(expected - actual) <= 0.1, () -> "expected: " + expected + ", actual: " + actual);
}
}