render + Sidebar DragDrop

This commit is contained in:
Patrick Haßel 2024-05-15 19:01:58 +02:00
parent f8e64b1280
commit b19547922c
25 changed files with 412 additions and 350 deletions

View File

@ -2,21 +2,25 @@ package de.ph87.electro;
import java.awt.*;
import static java.lang.Math.round;
public class CONFIG {
public static final double VOLTAGE = 3.0;
public static final int RASTER = 200;
public static final double HALF = 0.5;
public static final int HALF = (int) round(0.5 * RASTER);
public static final double FOURTH1 = 0.25;
public static final int FOURTH1 = (int) round(0.25 * RASTER);
public static final double FOURTH3 = 0.75;
public static final int FOURTH3 = (int) round(0.75 * RASTER);
public static final double JUNCTION_LEFT = 0.1;
public static final int JUNCTION_LEFT = (int) round(0.1 * RASTER);
public static final double JUNCTION_RIGHT = 0.9;
public static final int JUNCTION_RIGHT = (int) round(0.9 * RASTER);
public static final double JUNCTION_RADIUS = 0.05;
public static final int JUNCTION_RADIUS = (int) round(0.05 * RASTER);
public static final Color PART_BACKGROUND = new Color(224, 224, 224);

View File

@ -0,0 +1,11 @@
package de.ph87.electro;
import java.awt.*;
public class MathHelpers {
public static Point div(final Point p, final int div) {
return new Point(p.x / div, p.y / div);
}
}

View File

@ -19,7 +19,7 @@ public class Window extends JFrame {
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(new Sidebar(part -> circuit.getCircuit().add(part)), c);
add(new Sidebar(part -> circuit.getCircuit().getParts().add(part)), c);
c = new GridBagConstraints();
c.gridx = 1;

View File

@ -3,11 +3,13 @@ package de.ph87.electro.circuit;
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.Orientation;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.impl.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
@ -28,40 +30,33 @@ public class Circuit {
parts.forEach(part -> part.link(junctions));
}
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);
addAndRotate(battery, rotate);
public PartBattery addBattery(final String name, final int x, final int y, final Orientation orientation, final double voltage) {
final PartBattery battery = new PartBattery(name, new Point(x, y), orientation, voltage);
parts.add(battery);
return battery;
}
private void addAndRotate(final Part part, final int rotate) {
for (int i = 0; i < rotate % 4; i++) {
part.clockwise();
}
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);
addAndRotate(light, rotate);
public PartLight addLight(final String name, final int x, final int y, final Orientation orientation, final double maxVoltage) {
final PartLight light = new PartLight(name, new Point(x, y), orientation, maxVoltage);
parts.add(light);
return light;
}
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);
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, new Point(x, y), orientation, state);
parts.add(switch1x1);
return switch1x1;
}
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);
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, new Point(x, y), orientation, state);
parts.add(switch1x2);
return switch1x2;
}
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);
public PartSwitchCross addSwitchCross(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
final PartSwitchCross switchCross = new PartSwitchCross(name, new Point(x, y), orientation, state);
parts.add(switchCross);
return switchCross;
}
@ -70,14 +65,14 @@ public class Circuit {
b.getDestinations().add(a);
}
public void add(final Part part) {
parts.add(part);
evaluate();
}
public void evaluate() {
parts.forEach(Part::reset);
streamBatteries().forEach(PartBattery::startPropagation);
render();
}
public void render() {
parts.forEach(Part::render);
}
private Stream<PartBattery> streamBatteries() {
@ -98,4 +93,9 @@ public class Circuit {
return wires;
}
public void add(final Part part) {
parts.add(part);
evaluate();
}
}

View File

@ -1,23 +1,32 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.impl.*;
import de.ph87.electro.demo.DemoAll;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Optional;
import static de.ph87.electro.CONFIG.*;
import static de.ph87.electro.MathHelpers.div;
import static java.awt.event.MouseEvent.BUTTON1;
import static java.awt.event.MouseEvent.BUTTON3;
@Slf4j
public class CircuitPanel extends JPanel {
@Getter
private Circuit circuit = new Circuit();
private final Circuit circuit = DemoAll.create();
private Part dragPart = null;
@ -26,64 +35,43 @@ public class CircuitPanel extends JPanel {
private Point dragOffset = null;
public CircuitPanel() {
final MouseAdapter listener = new MouseAdapter() {
@Override
public void mouseClicked(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.clockwise();
repaint();
});
break;
}
}
@Override
public void mousePressed(final MouseEvent e) {
final int x = e.getX() / RASTER;
final int y = e.getY() / RASTER;
dragOffset = new Point(RASTER / 2, RASTER / 2);
dragPart = circuit.getParts().stream().filter(p -> p.getX() == x && p.getY() == y).findFirst().orElse(null);
}
@Override
public void mouseDragged(final MouseEvent e) {
if (dragPart != null) {
dragPoint = e.getPoint();
repaint();
}
}
@Override
public void mouseReleased(final MouseEvent e) {
if (dragPart != null) {
final int x = e.getX() / RASTER;
final int y = e.getY() / RASTER;
dragPart.setX(x);
dragPart.setY(y);
dragPart = null;
dragPoint = null;
dragOffset = null;
repaint();
}
}
};
final MouseAdapter listener = new MyMouseAdapter();
addMouseListener(listener);
addMouseMotionListener(listener);
circuit = DemoAll.create();
setDropTarget(new DropTarget() {
@Override
public void drop(final DropTargetDropEvent event) {
log.info("drop");
try {
final Transferable transferable = event.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
event.acceptDrop(DnDConstants.ACTION_COPY);
final Point raster = div(event.getLocation(), RASTER);
final String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
if (data.equals(PartBattery.class.getSimpleName())) {
circuit.add(new PartBattery(raster));
} else if (data.equals(PartLight.class.getSimpleName())) {
circuit.add(new PartLight(raster));
} else if (data.equals(PartSwitch1x1.class.getSimpleName())) {
circuit.add(new PartSwitch1x1(raster));
} else if (data.equals(PartSwitch1x2.class.getSimpleName())) {
circuit.add(new PartSwitch1x2(raster));
} else if (data.equals(PartSwitchCross.class.getSimpleName())) {
circuit.add(new PartSwitchCross(raster));
}
repaint();
event.dropComplete(true);
} else {
event.rejectDrop();
}
} catch (Exception e) {
log.error(e.toString());
event.rejectDrop();
}
}
});
}
@Override
@ -95,10 +83,7 @@ public class CircuitPanel extends JPanel {
drawParts(g);
drawRaster(g, w, h);
drawWires(g);
if (dragPart != null && dragPoint != null) {
g.setColor(new Color(192, 192, 192, 128));
g.fillRect(dragPoint.x - dragOffset.x, dragPoint.y - dragOffset.y, RASTER, RASTER);
}
drawDrag(g);
}
private void drawBack(final Graphics2D g, final int w, final int h) {
@ -123,19 +108,76 @@ public class CircuitPanel extends JPanel {
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();
final Point a = wire.getA().getAbsolute();
final Point b = wire.getB().getAbsolute();
g.setColor(Color.BLACK);
g.setStroke(WIRE_STROKE_BACK);
g.drawLine(x0, y0, x1, y1);
g.drawLine(a.x, a.y, b.x, b.y);
g.setColor(wire.getA().getColor());
g.setStroke(WIRE_STROKE);
g.drawLine(x0, y0, x1, y1);
g.drawLine(a.x, a.y, b.x, b.y);
}
}
private void drawDrag(final Graphics2D g) {
if (dragPart != null && dragPoint != null) {
g.setColor(new Color(192, 192, 192, 128));
g.fillRect(dragPoint.x - dragOffset.x, dragPoint.y - dragOffset.y, RASTER, RASTER);
}
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mouseClicked(final MouseEvent e) {
final Point cell = div(e.getPoint(), RASTER);
final Optional<Part> partOptional = circuit.getParts().stream().filter(p -> p.getPosition().equals(cell)).findFirst();
switch (e.getButton()) {
case BUTTON1:
partOptional.ifPresent(part -> {
part.click();
circuit.evaluate();
repaint();
});
break;
case BUTTON3:
partOptional.ifPresent(part -> {
part.clockwise();
circuit.render();
repaint();
});
break;
}
}
@Override
public void mousePressed(final MouseEvent e) {
final Point cell = div(e.getPoint(), RASTER);
dragOffset = new Point(HALF, HALF);
dragPart = circuit.getParts().stream().filter(p -> p.getPosition().equals(cell)).findFirst().orElse(null);
}
@Override
public void mouseDragged(final MouseEvent e) {
if (dragPart != null) {
dragPoint = e.getPoint();
repaint();
}
}
@Override
public void mouseReleased(final MouseEvent e) {
if (dragPart != null) {
dragPart.setPosition(div(e.getPoint(), RASTER));
dragPart = null;
dragPoint = null;
dragOffset = null;
repaint();
}
}
}
}

View File

@ -16,17 +16,11 @@ public class JunctionDto {
private String name;
private double x;
private double y;
private List<String> destinations;
public JunctionDto(final Junction junction) {
this.uuid = junction.getUuid();
this.name = junction.getName();
this.x = junction.getX();
this.y = junction.getY();
this.destinations = junction.getDestinations().stream().map(Junction::getUuid).toList();
}

View File

@ -8,6 +8,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.awt.*;
import java.util.List;
@Getter
@ -20,9 +21,7 @@ public class PartDto {
private String name;
private int x;
private int y;
private Point position;
private Orientation orientation;
@ -31,8 +30,7 @@ public class PartDto {
protected PartDto(final Part part) {
this.uuid = part.getUuid();
this.name = part.getName();
this.x = part.getX();
this.y = part.getY();
this.position = part.getPosition();
this.orientation = part.getOrientation();
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
}

View File

@ -13,7 +13,6 @@ import java.util.Set;
import java.util.UUID;
import static de.ph87.electro.CONFIG.*;
import static java.lang.Math.round;
@Getter
@ToString(onlyExplicitlyIncluded = true)
@ -29,9 +28,7 @@ public class Junction {
@ToString.Include
private final String name;
private double x;
private double y;
private final Point position;
private final Set<Junction> destinations = new HashSet<>();
@ -47,21 +44,19 @@ public class Junction {
return destinations.stream().map(Junction::getUuid).toList();
}
public Junction(final Part owner, final String name, final double x, final double y) {
public Junction(final Part owner, final String name, final Point position) {
this.uuid = UUID.randomUUID().toString();
this.owner = owner;
this.name = name;
this.x = x;
this.y = y;
this.position = position;
this._dto = null;
}
public Junction(final Part owner, final JunctionDto dto) {
public Junction(final Part owner, final JunctionDto dto, final Point position) {
this.uuid = dto.getUuid();
this.owner = owner;
this.name = dto.getName();
this.x = dto.getX();
this.y = dto.getY();
this.position = position;
this._dto = dto;
}
@ -100,23 +95,16 @@ public class Junction {
throw new ShortCircuit();
}
@SuppressWarnings("SuspiciousNameCombination")
public void rotate() {
final double oldX = x;
x = 1 - y;
y = oldX;
public void render(final Graphics2D g) {
owner.circle(g, position.x, position.y, JUNCTION_RADIUS, Color.BLACK, NORMAL_STROKE, color);
}
public void paint(final Graphics2D g) {
owner.circle(g, x, y, JUNCTION_RADIUS, Color.BLACK, NORMAL_STROKE, color);
public Point getAbsolute() {
return owner.translate(getRotated());
}
public int getAbsoluteX() {
return (int) round((owner.x + x) * RASTER);
}
public int getAbsoluteY() {
return (int) round((owner.y + y) * RASTER);
private Point getRotated() {
return owner.getOrientation().rotate(position);
}
}

View File

@ -1,28 +1,29 @@
package de.ph87.electro.circuit.part;
import java.awt.geom.Point2D;
import java.awt.*;
import java.util.function.Function;
import static de.ph87.electro.CONFIG.RASTER;
import static java.lang.Math.PI;
@SuppressWarnings("SuspiciousNameCombination")
public enum Orientation {
R0(0, p -> new Point2D.Double(p.x, p.y)),
R1(1, p -> new Point2D.Double(1 - p.y, p.x)),
R2(2, p -> new Point2D.Double(1 - p.x, 1 - p.y)),
R3(3, p -> new Point2D.Double(p.y, 1 - p.x)),
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)),
;
private final Function<Point2D.Double, Point2D.Double> map;
private final Function<Point, Point> map;
private final int deg90;
Orientation(final int deg90, final Function<Point2D.Double, Point2D.Double> map) {
Orientation(final int deg90, final Function<Point, Point> rotate) {
this.deg90 = deg90;
this.map = map;
this.map = rotate;
}
public Point2D.Double map(final Point2D.Double p) {
public Point rotate(final Point p) {
return map.apply(p);
}

View File

@ -13,8 +13,7 @@ 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 de.ph87.electro.CONFIG.*;
import static java.lang.Math.round;
@Getter
@ -28,26 +27,25 @@ public abstract class Part {
@ToString.Include
protected String name;
protected int x;
protected Point position;
protected int y;
private Orientation orientation;
protected Orientation orientation = Orientation.R0;
private final List<Junction> junctions = new ArrayList<>();
protected final List<Junction> junctions = new ArrayList<>();
private final BufferedImage render = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
protected Part(final String name, final int x, final int y) {
protected Part(final String name, final Point position, final Orientation orientation) {
this.uuid = UUID.randomUUID().toString();
this.name = name;
this.x = x;
this.y = y;
this.position = position;
this.orientation = orientation;
}
protected Part(final PartDto dto) {
this.uuid = dto.getUuid();
this.name = dto.getName();
this.x = dto.getX();
this.y = dto.getY();
this.position = dto.getPosition();
this.orientation = dto.getOrientation();
}
@ -55,30 +53,52 @@ public abstract class Part {
junctions.forEach(junction -> junction.link(junctions));
}
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 newJunction(final Part owner, final JunctionDto dto, final int x, final int y) {
return addJunction(new Junction(owner, dto, new Point(x, y)));
}
private Junction addJunction(final Junction junction) {
junctions.add(junction);
return junction;
}
public void reset() {
junctions.forEach(Junction::reset);
}
public void render() {
final Graphics2D g = (Graphics2D) render.getGraphics();
g.rotate(orientation.getTheta(), HALF, HALF);
rect(g, 0, 0, RASTER, RASTER, null, null, PART_BACKGROUND);
_render(g);
junctions.forEach(junction -> junction.render(g));
}
public void paint(final Graphics2D g) {
rect(g, 0, 0, 1, 1, null, null, PART_BACKGROUND);
_paint(g);
junctions.forEach(junction -> junction.paint(g));
g.drawImage(render, position.x * RASTER, position.y * RASTER, null);
}
public void clockwise() {
orientation = orientation.clockwise();
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 Junction junction0, final Junction junction1, final Color color, final BasicStroke stroke) {
line(g, junction0.getPosition().x, junction0.getPosition().y, junction1.getPosition().x, junction1.getPosition().y, color, stroke);
}
public void line(final Graphics2D g, final Junction junction0, final double x1, final double y1, final Color color, final BasicStroke stroke) {
line(g, junction0.getPosition().x, junction0.getPosition().y, x1, y1, color, stroke);
}
public void line(final Graphics2D g, final double x0, final double y0, final double x1, final double y1, final Color color, final Stroke stroke) {
final int _x0 = (int) round((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);
final int _x0 = (int) round(position.x + x0);
final int _y0 = (int) round(position.y + y0);
final int _x1 = (int) round(position.x + x1);
final int _y1 = (int) round(position.y + y1);
if (color != null) {
g.setColor(color);
}
@ -89,10 +109,10 @@ public abstract class Part {
}
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);
final int _x = (int) round(position.x + x);
final int _y = (int) round(position.y + y);
final int _w = (int) round(w);
final int _h = (int) round(h);
if (fill != null) {
g.setColor(fill);
g.fillRect(_x, _y, _w, _h);
@ -104,16 +124,10 @@ public abstract class Part {
}
}
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);
final int _x = (int) round(position.x + x - radius);
final int _y = (int) round(position.y + y - radius);
final int diameter = (int) round(radius * 2);
if (fill != null) {
g.setColor(fill);
g.fillArc(_x, _y, diameter, diameter, 0, 360);
@ -129,7 +143,9 @@ public abstract class Part {
public abstract void propagate(final Junction source) throws ShortCircuit;
protected abstract void _paint(final Graphics2D g);
protected abstract void _render(final Graphics2D g);
public abstract Part duplicate(final Point position);
public static Part of(final PartDto abstractDto) {
return switch (abstractDto) {
@ -142,6 +158,11 @@ public abstract class Part {
};
}
public abstract Part duplicate(final int x, final int y);
public Point translate(final Point p) {
return new Point(
p.x + position.x * RASTER,
p.y + position.y * RASTER
);
}
}

View File

@ -1,14 +1,16 @@
package de.ph87.electro.circuit.part;
import de.ph87.electro.circuit.dto.PartLightDto;
import de.ph87.electro.circuit.dto.PartDto;
import java.awt.*;
public abstract class PartOther extends Part {
protected PartOther(final String name, final int x, final int y) {
super(name, x, y);
protected PartOther(final String name, final Point position, final Orientation orientation) {
super(name, position, orientation);
}
protected PartOther(final PartLightDto dto) {
protected PartOther(final PartDto dto) {
super(dto);
}

View File

@ -3,13 +3,13 @@ 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.Orientation;
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.*;
@ -17,15 +17,15 @@ import static de.ph87.electro.CONFIG.*;
@ToString(callSuper = true)
public class PartBattery extends Part {
private static final double MINUS_W = 0.1;
private static final double MINUS_W = 0.1 * RASTER;
private static final double MINUS_H = 0.3;
private static final double MINUS_H = 0.3 * RASTER;
private static final double GAP = 0.05;
private static final double GAP = 0.05 * RASTER;
private static final double PLUS_W = 0.02;
private static final double PLUS_W = 0.02 * RASTER;
private static final double PLUS_H = 0.6;
private static final double PLUS_H = 0.6 * RASTER;
private final Junction minus;
@ -36,21 +36,21 @@ public class PartBattery extends Part {
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);
public PartBattery(final Point position) {
this("Batterie", position, Orientation.R0, VOLTAGE);
}
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);
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);
minus = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
plus = newJunction(this, dto.getPlus(), JUNCTION_RIGHT, HALF);
voltage = dto.getVoltage();
}
@ -75,22 +75,16 @@ public class PartBattery extends Part {
}
@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(orientation.getTheta());
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);
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);
}
@Override
public PartBattery duplicate(final int x, final int y) {
return new PartBattery(name, x, y, voltage);
public PartBattery duplicate(final Point position) {
return new PartBattery(name, position, getOrientation(), voltage);
}
@Override

View File

@ -2,6 +2,7 @@ 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.Orientation;
import de.ph87.electro.circuit.part.PartOther;
import lombok.Getter;
import lombok.ToString;
@ -18,17 +19,17 @@ 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 int MINUS_W = (int) round(0.1 * RASTER);
private static final double MINUS_H = 0.3;
private static final int MINUS_H = (int) round(0.3 * RASTER);
private static final double GAP = 0.05;
private static final int GAP = (int) round(0.05 * RASTER);
private static final double PLUS_W = 0.02;
private static final int PLUS_W = (int) round(0.02 * RASTER);
private static final double PLUS_H = 0.6;
private static final int PLUS_H = (int) round(0.6 * RASTER);
private static final double BULB_RADIUS = 0.25;
private static final int BULB_RADIUS = (int) round(0.25 * RASTER);
private final Junction pin0;
@ -42,21 +43,21 @@ public class PartLight extends PartOther {
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);
public PartLight(final Point position) {
this("Licht", position, Orientation.R0, VOLTAGE);
}
public PartLight(final String name, final Point position, final Orientation orientation, final double maxVoltage) {
super(name, position, orientation);
pin0 = newJunction(this, "", JUNCTION_LEFT, HALF);
pin1 = newJunction(this, "", JUNCTION_RIGHT, HALF);
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);
pin0 = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
pin1 = newJunction(this, dto.getPlus(), JUNCTION_LEFT, HALF);
maxVoltage = dto.getMaxVoltage();
defect = dto.isDefect();
}
@ -80,18 +81,18 @@ public class PartLight extends PartOther {
}
@Override
protected void _paint(final Graphics2D g) {
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);
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);
final double diag = 0.33 * RASTER;
line(g, diag, diag, RASTER - diag, RASTER - diag, Color.BLACK, SYMBOL_STROKE);
line(g, diag, RASTER - diag, RASTER - diag, diag, Color.BLACK, SYMBOL_STROKE);
}
@Override
public PartLight duplicate(final int x, final int y) {
return new PartLight(name, x, y, maxVoltage);
public PartLight duplicate(final Point position) {
return new PartLight(name, position, getOrientation(), maxVoltage);
}
@Override

View File

@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
import de.ph87.electro.circuit.ShortCircuit;
import de.ph87.electro.circuit.dto.PartSwitch1x1Dto;
import de.ph87.electro.circuit.part.Junction;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.PartOther;
import lombok.Getter;
import lombok.Setter;
@ -23,21 +24,21 @@ public class PartSwitch1x1 extends PartOther {
@Setter
private boolean state;
public PartSwitch1x1(final String name, final int x, final int y, final boolean state) {
super(name, x, y);
common = new Junction(this, "", JUNCTION_LEFT, HALF);
output = new Junction(this, "", JUNCTION_RIGHT, HALF);
junctions.add(common);
junctions.add(output);
public PartSwitch1x1(final Point position) {
this("Ausschalter", position, Orientation.R0, false);
}
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);
this.state = state;
}
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
super(dto.getName(), dto.getX(), dto.getY());
common = new Junction(this, dto.getCommon());
output = new Junction(this, dto.getOutput());
junctions.add(common);
junctions.add(output);
super(dto);
common = newJunction(this, dto.getCommon(), JUNCTION_LEFT, HALF);
output = newJunction(this, dto.getOutput(), JUNCTION_RIGHT, HALF);
state = dto.isState();
}
@ -55,9 +56,9 @@ public class PartSwitch1x1 extends PartOther {
}
@Override
protected void _paint(final Graphics2D g) {
protected void _render(final Graphics2D g) {
if (!state) {
line(g, common.getX(), common.getY(), JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
line(g, common, JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
} else {
line(g, common, output, common.getColor(), SWITCH_STROKE);
}
@ -69,8 +70,8 @@ public class PartSwitch1x1 extends PartOther {
}
@Override
public PartSwitch1x1 duplicate(final int x, final int y) {
return new PartSwitch1x1(name, x, y, state);
public PartSwitch1x1 duplicate(final Point position) {
return new PartSwitch1x1(name, position, getOrientation(), state);
}
}

View File

@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
import de.ph87.electro.circuit.ShortCircuit;
import de.ph87.electro.circuit.dto.PartSwitch1x2Dto;
import de.ph87.electro.circuit.part.Junction;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.PartOther;
import lombok.Getter;
import lombok.Setter;
@ -25,25 +26,23 @@ public class PartSwitch1x2 extends PartOther {
@Setter
private boolean state;
public PartSwitch1x2(final String name, final int x, final int y, final boolean state) {
super(name, x, y);
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);
public PartSwitch1x2(final Point position) {
this("Wechselschalter", position, Orientation.R0, false);
}
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);
this.state = state;
}
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
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());
junctions.add(common);
junctions.add(output0);
junctions.add(output1);
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);
state = dto.isState();
}
@ -67,7 +66,7 @@ public class PartSwitch1x2 extends PartOther {
}
@Override
protected void _paint(final Graphics2D g) {
protected void _render(final Graphics2D g) {
if (!state) {
line(g, common, output0, common.getColor(), SWITCH_STROKE);
} else {
@ -81,8 +80,8 @@ public class PartSwitch1x2 extends PartOther {
}
@Override
public PartSwitch1x2 duplicate(final int x, final int y) {
return new PartSwitch1x2(name, x, y, state);
public PartSwitch1x2 duplicate(final Point position) {
return new PartSwitch1x2(name, position, getOrientation(), state);
}
}

View File

@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
import de.ph87.electro.circuit.ShortCircuit;
import de.ph87.electro.circuit.dto.PartSwitchCrossDto;
import de.ph87.electro.circuit.part.Junction;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.PartOther;
import lombok.Getter;
import lombok.Setter;
@ -27,29 +28,25 @@ public class PartSwitchCross extends PartOther {
@Setter
private boolean state;
public PartSwitchCross(final String name, final int x, final int y, final boolean state) {
super(name, x, y);
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);
public PartSwitchCross(final Point position) {
this("Kreuzschalter", position, Orientation.R0, false);
}
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);
this.state = state;
}
public PartSwitchCross(final PartSwitchCrossDto dto) {
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());
output1 = new Junction(this, dto.getOutput1());
junctions.add(common0);
junctions.add(common1);
junctions.add(output0);
junctions.add(output1);
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);
state = dto.isState();
}
@ -83,7 +80,7 @@ public class PartSwitchCross extends PartOther {
}
@Override
protected void _paint(final Graphics2D g) {
protected void _render(final Graphics2D g) {
if (!state) {
line(g, common0, output0, common0.getColor(), SWITCH_STROKE);
line(g, common1, output1, common1.getColor(), SWITCH_STROKE);
@ -99,8 +96,8 @@ public class PartSwitchCross extends PartOther {
}
@Override
public PartSwitchCross duplicate(final int x, final int y) {
return new PartSwitchCross(name, x, y, state);
public PartSwitchCross duplicate(final Point position) {
return new PartSwitchCross(name, position, getOrientation(), state);
}
}

View File

@ -1,21 +1,22 @@
package de.ph87.electro.demo;
import de.ph87.electro.circuit.Circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.*;
import static de.ph87.electro.CONFIG.VOLTAGE;
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);
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R2, VOLTAGE);
final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ausschalter", 2, 0, Orientation.R0, false);
final PartLight light = circuit.addLight("Licht", 4, 0, Orientation.R0, 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);
circuit.connect(battery.getMinus(), switcher.getCommon());
circuit.connect(switcher.getOutput(), light.getPin0());

View File

@ -1,43 +1,32 @@
package de.ph87.electro.sidebar;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.impl.*;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import static de.ph87.electro.CONFIG.VOLTAGE;
public class Sidebar extends JPanel {
private final Consumer<Part> onAdd;
private final List<SidebarPart> parts = new ArrayList<>();
public Sidebar(final Consumer<Part> onAdd) {
this.onAdd = onAdd;
setLayout(new GridBagLayout());
add(new PartBattery("Batterie", 0, 0, 3.0));
add(new PartLight("Licht", 1, 0, 3.0));
add(new PartSwitch1x1("Ausschalter", 0, 1, false));
add(new PartSwitch1x2("Wechselschalter", 1, 1, false));
add(new PartSwitchCross("Kreuzschalter", 0, 2, false));
setLayout(new FlowLayout());
add(new PartBattery("Batterie", new Point(0, 0), Orientation.R0, VOLTAGE));
add(new PartLight("Licht", new Point(0, 0), Orientation.R0, VOLTAGE));
add(new PartSwitch1x1("Ausschalter", new Point(0, 0), Orientation.R0, false));
add(new PartSwitch1x2("Wechselschalter", new Point(0, 0), Orientation.R0, false));
add(new PartSwitchCross("Kreuzschalter", new Point(0, 0), Orientation.R0, false));
}
private void add(final Part part) {
final SidebarPart entry = new SidebarPart(part, onAdd);
parts.add(entry);
add(entry);
}
@Override
public void paint(final Graphics g) {
g.setColor(Color.gray);
g.fillRect(0, 0, getWidth(), getHeight());
for (final SidebarPart part : parts) {
part.getPart().paint((Graphics2D) g);
}
}
}

View File

@ -2,15 +2,18 @@ package de.ph87.electro.sidebar;
import de.ph87.electro.circuit.part.Part;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragSource;
import java.util.function.Consumer;
import static de.ph87.electro.CONFIG.RASTER;
@Slf4j
@Getter
public class SidebarPart extends JPanel {
@ -21,15 +24,26 @@ public class SidebarPart extends JPanel {
public SidebarPart(final Part part, final Consumer<Part> add) {
this.part = part;
this.add = add;
setTransferHandler(new TransferHandler("text"));
setPreferredSize(new Dimension(RASTER, RASTER));
addMouseListener(new MouseAdapter() {
createDragSource(part);
}
@Override
public void mouseClicked(final MouseEvent e) {
add.accept(part.duplicate(e.getPoint().x / RASTER, e.getPoint().y / RASTER));
}
private void createDragSource(final Part part) {
final DragSource dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer(
this,
DnDConstants.ACTION_COPY_OR_MOVE,
event -> dragSource.startDrag(event, DragSource.DefaultCopyDrop, new StringSelection(part.getClass().getSimpleName()), null)
);
part.render();
}
});
@Override
public void paint(final Graphics g) {
part.paint((Graphics2D) g);
g.setColor(Color.black);
g.drawRect(0, 0, RASTER - 1, RASTER - 1);
}
}

View File

@ -1,5 +1,6 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
import org.junit.jupiter.api.BeforeAll;
@ -14,9 +15,9 @@ public class BatteryLightTest {
private static final Circuit circuit = new Circuit();
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
@BeforeAll
public static void setUp() {

View File

@ -1,5 +1,6 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
import de.ph87.electro.circuit.part.impl.PartSwitch1x1;
@ -15,11 +16,11 @@ public class BatterySwitcher1x1Test {
private static final Circuit circuit = new Circuit();
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, 0, false);
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, Orientation.R0, false);
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
@BeforeAll
public static void setUp() {

View File

@ -1,5 +1,6 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
@ -15,13 +16,13 @@ public class BatterySwitcher1x2Test {
private static final Circuit circuit = new Circuit();
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, 0, false);
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, Orientation.R0, false);
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, 0, VOLTAGE);
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, Orientation.R0, VOLTAGE);
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, 0, VOLTAGE);
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, Orientation.R0, VOLTAGE);
@BeforeAll
public static void setUp() {

View File

@ -1,5 +1,6 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
@ -15,13 +16,13 @@ public class BatterySwitcher2x2Test {
private static final Circuit circuit = new Circuit();
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, 0, false);
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, Orientation.R0, false);
private static final PartLight light = circuit.addLight("Licht", 1, 0, 0, VOLTAGE);
private static final PartLight light = circuit.addLight("Licht", 1, 0, Orientation.R0, VOLTAGE);
@BeforeAll
public static void setUp() {

View File

@ -1,5 +1,6 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
@ -16,15 +17,15 @@ public class BatterySwitcherCrossTest {
private static final Circuit circuit = new Circuit();
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, 0, false);
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, Orientation.R0, false);
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, 0, false);
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, Orientation.R0, false);
private static final PartLight light = circuit.addLight("Licht", 2, 0, 0, VOLTAGE);
private static final PartLight light = circuit.addLight("Licht", 2, 0, Orientation.R0, VOLTAGE);
@BeforeAll
public static void setUp() {

View File

@ -1,6 +1,7 @@
package de.ph87.electro.circuit;
import de.ph87.electro.circuit.part.Junction;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.impl.PartBattery;
import de.ph87.electro.circuit.part.impl.PartLight;
@ -11,6 +12,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static de.ph87.electro.CONFIG.VOLTAGE;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CircuitServiceTest {
@ -18,8 +20,8 @@ class CircuitServiceTest {
@Test
void serialization() throws IOException {
final Circuit circuit = new Circuit();
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 1, 3.0);
final PartLight light = circuit.addLight("Licht", 1, 1, 0, 3.0);
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R1, VOLTAGE);
final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
circuit.connect(battery.getPlus(), light.getPin1());
circuit.connect(light.getPin0(), battery.getMinus());
check(circuit);
@ -42,8 +44,7 @@ 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.getX(), reloadedPart.getX());
assertEquals(originalPart.getY(), reloadedPart.getY());
assertEquals(originalPart.getPosition(), reloadedPart.getPosition());
assertEquals(originalPart.getOrientation(), reloadedPart.getOrientation());
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
for (final Junction originalJunction : originalPart.getJunctions()) {
@ -51,8 +52,7 @@ class CircuitServiceTest {
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.getX(), reloadedJunction.getX());
assertEquals(originalJunction.getY(), reloadedJunction.getY());
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
assertEquals(originalJunction.getDestinations().size(), reloadedJunction.getDestinations().size());
for (final Junction originalDestination : originalJunction.getDestinations()) {
System.out.printf(" - Destination: %s\n", originalDestination.getUuid());