REFACTOR: position, evaluate, repaint
This commit is contained in:
parent
e7e74cba44
commit
d881c8ff04
@ -4,6 +4,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
import static java.lang.Math.round;
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class CONFIG {
|
public class CONFIG {
|
||||||
|
|
||||||
public static final double NO_RESISTANCE = 1e-12;
|
public static final double NO_RESISTANCE = 1e-12;
|
||||||
@ -20,6 +21,8 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final int RASTER = 200;
|
public static final int RASTER = 200;
|
||||||
|
|
||||||
|
public static final int P03 = (int) round(0.03 * RASTER);
|
||||||
|
|
||||||
public static final int P05 = (int) round(0.05 * RASTER);
|
public static final int P05 = (int) round(0.05 * RASTER);
|
||||||
|
|
||||||
public static final int P10 = (int) round(0.1 * RASTER);
|
public static final int P10 = (int) round(0.1 * RASTER);
|
||||||
@ -28,6 +31,8 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final int P50 = (int) round(0.5 * RASTER);
|
public static final int P50 = (int) round(0.5 * RASTER);
|
||||||
|
|
||||||
|
public static final int P65 = (int) round(0.65 * RASTER);
|
||||||
|
|
||||||
public static final int P75 = (int) round(0.75 * RASTER);
|
public static final int P75 = (int) round(0.75 * RASTER);
|
||||||
|
|
||||||
public static final int P70 = (int) round(0.7 * RASTER);
|
public static final int P70 = (int) round(0.7 * RASTER);
|
||||||
@ -42,7 +47,7 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final int JUNCTION_RADIUS_HOVER = (int) round(1.5 * JUNCTION_RADIUS);
|
public static final int JUNCTION_RADIUS_HOVER = (int) round(1.5 * JUNCTION_RADIUS);
|
||||||
|
|
||||||
public static final Color PART_BACKGROUND = new Color(224, 224, 224);
|
public static final Color PART_BACK_COLOR = new Color(224, 224, 224);
|
||||||
|
|
||||||
public static final Color PART_HOVER_COLOR = new Color(192, 192, 192, 128);
|
public static final Color PART_HOVER_COLOR = new Color(192, 192, 192, 128);
|
||||||
|
|
||||||
@ -76,4 +81,12 @@ public class CONFIG {
|
|||||||
|
|
||||||
public static final BasicStroke SWITCH_STROKE = new BasicStroke(15);
|
public static final BasicStroke SWITCH_STROKE = new BasicStroke(15);
|
||||||
|
|
||||||
|
public static Point ALIGN(final Point position) {
|
||||||
|
return new Point(position.x / RASTER * RASTER, position.y / RASTER * RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Point RASTER(final int x, final int y) {
|
||||||
|
return new Point(x * RASTER, y * RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package de.ph87.electro;
|
package de.ph87.electro;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.Circuit;
|
||||||
import de.ph87.electro.circuit.CircuitPanel;
|
import de.ph87.electro.circuit.CircuitPanel;
|
||||||
|
import de.ph87.electro.circuit.demo.Demos;
|
||||||
import de.ph87.electro.sidebar.Sidebar;
|
import de.ph87.electro.sidebar.Sidebar;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
@ -10,17 +12,17 @@ import static de.ph87.electro.CONFIG.RASTER;
|
|||||||
|
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame {
|
||||||
|
|
||||||
|
private final CircuitPanel circuitPanel = new CircuitPanel();
|
||||||
|
|
||||||
public Window() {
|
public Window() {
|
||||||
positionOnRightMostScreen();
|
positionOnRightMostScreen();
|
||||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
setPreferredSize(new Dimension(1200, 900));
|
setPreferredSize(new Dimension(1200, 900));
|
||||||
setExtendedState(MAXIMIZED_BOTH);
|
setExtendedState(MAXIMIZED_BOTH);
|
||||||
|
|
||||||
final CircuitPanel circuitPanel = new CircuitPanel();
|
final Sidebar sidebar = new Sidebar(() -> setCircuit(new Circuit()));
|
||||||
final Sidebar sidebar = new Sidebar(circuitPanel::newCircuit, circuitPanel.getCircuit()::save);
|
|
||||||
final JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sidebar, circuitPanel);
|
final JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sidebar, circuitPanel);
|
||||||
|
|
||||||
sidebar.setRepaintCallback(circuitPanel::repaint);
|
|
||||||
sidebar.setPreferredSize(new Dimension(calcWidth(3), 0));
|
sidebar.setPreferredSize(new Dimension(calcWidth(3), 0));
|
||||||
sidebar.setMinimumSize(new Dimension(calcWidth(1), 0));
|
sidebar.setMinimumSize(new Dimension(calcWidth(1), 0));
|
||||||
|
|
||||||
@ -34,6 +36,10 @@ public class Window extends JFrame {
|
|||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCircuit(final Circuit circuit) {
|
||||||
|
circuitPanel.setCircuit(circuit);
|
||||||
|
}
|
||||||
|
|
||||||
private int calcWidth(final int raster) {
|
private int calcWidth(final int raster) {
|
||||||
return raster * (RASTER + 5) + 5;
|
return raster * (RASTER + 5) + 5;
|
||||||
}
|
}
|
||||||
@ -46,7 +52,8 @@ public class Window extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Window();
|
final Window window = new Window();
|
||||||
|
window.setCircuit(Demos.potiAndVoltmeter());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package de.ph87.electro.circuit;
|
|||||||
import de.ph87.electro.circuit.calculation.Calculation;
|
import de.ph87.electro.circuit.calculation.Calculation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.PartDto;
|
import de.ph87.electro.circuit.part.PartDto;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import de.ph87.electro.circuit.wire.Wire;
|
import de.ph87.electro.circuit.wire.Wire;
|
||||||
import de.ph87.electro.circuit.wire.WireDto;
|
import de.ph87.electro.circuit.wire.WireDto;
|
||||||
@ -11,6 +10,7 @@ import lombok.Getter;
|
|||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@ -19,6 +19,8 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.ALIGN;
|
||||||
|
|
||||||
public class Circuit {
|
public class Circuit {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -39,9 +41,9 @@ public class Circuit {
|
|||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public Circuit(final File file, final CircuitDto dto) {
|
public Circuit(final File file, final CircuitDto dto) {
|
||||||
created = dto.getCreated();
|
this.created = dto.getCreated();
|
||||||
for (PartDto partDto : dto.getParts()) {
|
for (PartDto partDto : dto.getParts()) {
|
||||||
final Part part = Part.fromDto(this, partDto);
|
final Part part = Part.fromDto(partDto);
|
||||||
verifyFree(part.getPosition());
|
verifyFree(part.getPosition());
|
||||||
parts.add(part);
|
parts.add(part);
|
||||||
}
|
}
|
||||||
@ -50,12 +52,12 @@ public class Circuit {
|
|||||||
final Junction b = findJunctionByUuid(wire.getB()).orElseThrow();
|
final Junction b = findJunctionByUuid(wire.getB()).orElseThrow();
|
||||||
wires.add(new Wire(a, b));
|
wires.add(new Wire(a, b));
|
||||||
}
|
}
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(final Junction a, final Junction b) {
|
public Wire connect(final Junction a, final Junction b) {
|
||||||
wires.add(new Wire(a, b));
|
final Wire wire = new Wire(a, b);
|
||||||
evaluate();
|
wires.add(wire);
|
||||||
|
return wire;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect(final Wire wire) {
|
public void disconnect(final Wire wire) {
|
||||||
@ -65,7 +67,6 @@ public class Circuit {
|
|||||||
wires.remove(wire);
|
wires.remove(wire);
|
||||||
wire.getA().getWires().remove(wire);
|
wire.getA().getWires().remove(wire);
|
||||||
wire.getB().getWires().remove(wire);
|
wire.getB().getWires().remove(wire);
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Part> T addPart(final T part) {
|
public <T extends Part> T addPart(final T part) {
|
||||||
@ -74,8 +75,6 @@ public class Circuit {
|
|||||||
}
|
}
|
||||||
verifyFree(part.getPosition());
|
verifyFree(part.getPosition());
|
||||||
parts.add(part);
|
parts.add(part);
|
||||||
part.render();
|
|
||||||
evaluate();
|
|
||||||
return part;
|
return part;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,32 +84,17 @@ public class Circuit {
|
|||||||
} else {
|
} else {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void movePart(final Part part, final Position position) {
|
public void verifyFree(final Point position) {
|
||||||
if (!parts.contains(part)) {
|
if (isOccupied(position)) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
verifyFree(position);
|
|
||||||
part.setPosition(position);
|
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void evaluate() {
|
public boolean isOccupied(final Point position) {
|
||||||
Calculation.calculate(this);
|
final Point aligned = ALIGN(position);
|
||||||
parts.forEach(Part::render);
|
return parts.stream().anyMatch(part -> part.getPosition().equals(aligned));
|
||||||
save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save() {
|
|
||||||
CircuitIOService.save(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyFree(final Position position) {
|
|
||||||
if (parts.stream().anyMatch(part -> part.getPosition().equals(position))) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stream<Part> streamParts() {
|
public Stream<Part> streamParts() {
|
||||||
@ -125,19 +109,16 @@ public class Circuit {
|
|||||||
return parts.size();
|
return parts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Part> findPartByPosition(final Position position) {
|
public Optional<Part> findPartByPosition(final Point position) {
|
||||||
return streamParts().filter(p -> p.getPosition().raster.equals(position.raster)).findFirst();
|
final Point aligned = ALIGN(position);
|
||||||
}
|
return streamParts().filter(p -> p.getPosition().equals(aligned)).findFirst();
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByAbsolute(final Position position) {
|
|
||||||
return findPartByPosition(position).flatMap(part -> part.findJunctionByPosition(position));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByUuid(@NonNull final String junctionUuid) {
|
public Optional<Junction> findJunctionByUuid(@NonNull final String junctionUuid) {
|
||||||
return parts.stream().map(part -> part.findJunctionByUuid(junctionUuid)).filter(Optional::isPresent).map(Optional::get).findFirst();
|
return parts.stream().map(part -> part.findJunctionByUuid(junctionUuid)).filter(Optional::isPresent).map(Optional::get).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Wire> findWireByPosition(final Position position) {
|
public Optional<Wire> findWireByPosition(final Point position) {
|
||||||
return wires.stream().filter(wire -> wire.intersects(position)).findFirst();
|
return wires.stream().filter(wire -> wire.intersects(position)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,4 +129,8 @@ public class Circuit {
|
|||||||
return new File(file.getAbsolutePath().replaceAll("\\.json$", ".png"));
|
return new File(file.getAbsolutePath().replaceAll("\\.json$", ".png"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void evaluate() {
|
||||||
|
Calculation.calculate(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,9 +7,10 @@ import javax.imageio.ImageIO;
|
|||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
import static de.ph87.electro.circuit.CircuitPainter.paintCircuit;
|
import static de.ph87.electro.circuit.CircuitPainter.draw;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class CircuitIOService {
|
public class CircuitIOService {
|
||||||
|
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
|
private static final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
|
||||||
@ -27,7 +28,7 @@ public class CircuitIOService {
|
|||||||
log.error(e.toString());
|
log.error(e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
final BufferedImage img = paintCircuit(circuit, 1920, 1080);
|
final BufferedImage img = draw(circuit, 1920, 1080);
|
||||||
try {
|
try {
|
||||||
ImageIO.write(img, "PNG", circuit.getPreviewFile());
|
ImageIO.write(img, "PNG", circuit.getPreviewFile());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@ -1,26 +1,31 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
public class CircuitPainter {
|
public class CircuitPainter {
|
||||||
|
|
||||||
public static BufferedImage paintCircuit(final Circuit circuit, final int w, final int h) {
|
public static BufferedImage draw(final Circuit circuit, final int w, final int h) {
|
||||||
final BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
final BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
||||||
final Graphics2D g = img.createGraphics();
|
final Graphics2D g = img.createGraphics();
|
||||||
paintCircuit(circuit, g, w, h);
|
draw(g, circuit, w, h);
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void paintCircuit(final Circuit circuit, final Graphics2D g, final int w, final int h) {
|
public static void draw(final Graphics2D g, final Circuit circuit, final int w, final int h) {
|
||||||
|
g.setTransform(new AffineTransform());
|
||||||
drawBack(g, w, h);
|
drawBack(g, w, h);
|
||||||
drawParts(circuit, g);
|
drawParts(circuit, g);
|
||||||
|
g.setTransform(new AffineTransform());
|
||||||
drawRaster(g, w, h);
|
drawRaster(g, w, h);
|
||||||
drawWires(circuit, g);
|
drawWires(circuit, g);
|
||||||
drawVoltages(circuit, g);
|
drawVoltages(circuit, g);
|
||||||
@ -32,7 +37,10 @@ public class CircuitPainter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void drawParts(final Circuit circuit, final Graphics2D g) {
|
private static void drawParts(final Circuit circuit, final Graphics2D g) {
|
||||||
circuit.streamParts().forEach(part -> part.paint(g));
|
circuit.streamParts().forEach(part -> {
|
||||||
|
g.setTransform(part.getTransform());
|
||||||
|
part.draw(g);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void drawRaster(final Graphics2D g, final int w, final int h) {
|
private static void drawRaster(final Graphics2D g, final int w, final int h) {
|
||||||
@ -61,6 +69,7 @@ public class CircuitPainter {
|
|||||||
|
|
||||||
for (Part part : circuit.getParts()) {
|
for (Part part : circuit.getParts()) {
|
||||||
for (final Junction junction : part.getJunctions()) {
|
for (final Junction junction : part.getJunctions()) {
|
||||||
|
final Point absolute = junction.getAbsolute();
|
||||||
if (SHOW_JUNCTION_NAMES) {
|
if (SHOW_JUNCTION_NAMES) {
|
||||||
int offsetY = third;
|
int offsetY = third;
|
||||||
if (SHOW_JUNCTION_VOLTAGES) {
|
if (SHOW_JUNCTION_VOLTAGES) {
|
||||||
@ -68,7 +77,7 @@ public class CircuitPainter {
|
|||||||
}
|
}
|
||||||
final String string = junction.getName();
|
final String string = junction.getName();
|
||||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||||
g.drawString(string, junction.getPosition().absolute.x - (int) (bounds.getWidth() / 2), junction.getPosition().absolute.y + offsetY);
|
g.drawString(string, absolute.x - (int) (bounds.getWidth() / 2), absolute.y + offsetY);
|
||||||
}
|
}
|
||||||
if (SHOW_JUNCTION_VOLTAGES) {
|
if (SHOW_JUNCTION_VOLTAGES) {
|
||||||
int offsetY = third;
|
int offsetY = third;
|
||||||
@ -77,10 +86,66 @@ public class CircuitPainter {
|
|||||||
}
|
}
|
||||||
final String string = "%.1fV".formatted(junction.getVoltage());
|
final String string = "%.1fV".formatted(junction.getVoltage());
|
||||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||||
g.drawString(string, junction.getPosition().absolute.x - (int) (bounds.getWidth() / 2), junction.getPosition().absolute.y + offsetY);
|
g.drawString(string, absolute.x - (int) (bounds.getWidth() / 2), absolute.y + offsetY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void drawLine(final Graphics2D g, final Junction a, final Junction b, final Color color, final Stroke stroke) {
|
||||||
|
drawLine(g, a.getInside(), b.getInside(), color, stroke);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawLine(final Graphics2D g, final Junction a, final Point b, final Color color, final Stroke stroke) {
|
||||||
|
drawLine(g, a.getInside(), b, color, stroke);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawLine(final Graphics2D g, final Point a, final Point b, final Color color, final Stroke stroke) {
|
||||||
|
drawLine(g, a.x, a.y, b.x, b.y, color, stroke);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawLine(final Graphics2D g, final int x0, final int y0, final int x1, final int y1, final Color color, final Stroke stroke) {
|
||||||
|
g.setColor(color);
|
||||||
|
g.setStroke(stroke);
|
||||||
|
g.drawLine(x0, y0, x1, y1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fillRect(final Graphics2D g, final int x0, final int y0, final int x1, final int y1, final Color color) {
|
||||||
|
g.setColor(color);
|
||||||
|
g.fillRect(x0, y0, x1, y1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawCircle(final Graphics2D g, final Point center, final int radius, final double degreesOffset, final double degreesRange, final Color border, final Stroke stroke, final Color fill) {
|
||||||
|
drawCircle(g, center.x, center.y, radius, border, stroke, fill, degreesOffset, degreesRange);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawCircle(final Graphics2D g, final int x, final int y, final int radius, final Color border, final Stroke stroke, final Color fill, final double degreesOffset, final double degreesRange) {
|
||||||
|
if (fill != null) {
|
||||||
|
g.setColor(fill);
|
||||||
|
g.fillArc(x - radius, y - radius, 2 * radius, 2 * radius, (int) round(degreesOffset), (int) round(degreesRange));
|
||||||
|
}
|
||||||
|
if (border != null && stroke != null) {
|
||||||
|
g.setColor(border);
|
||||||
|
g.setStroke(stroke);
|
||||||
|
g.drawArc(x - radius, y - radius, 2 * radius, 2 * radius, (int) round(degreesOffset), (int) round(degreesRange));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawText(final Graphics2D g, final Font font, final String text, final int x, int y, final Color color, final Orientation orientation) {
|
||||||
|
final AffineTransform transformBackup = g.getTransform();
|
||||||
|
if (orientation == Orientation.R180) {
|
||||||
|
g.rotate(orientation.getRadians(), P50, P50);
|
||||||
|
y = RASTER - y;
|
||||||
|
}
|
||||||
|
|
||||||
|
g.setFont(font);
|
||||||
|
g.setColor(color);
|
||||||
|
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(text, g);
|
||||||
|
final int xx = (int) round(x - bounds.getWidth() / 2);
|
||||||
|
final int yy = (int) round(y - bounds.getHeight() / 2 + font.getSize());
|
||||||
|
g.drawString(text, xx, yy);
|
||||||
|
|
||||||
|
g.setTransform(transformBackup);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,36 +1,47 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ComponentAdapter;
|
||||||
import static de.ph87.electro.circuit.CircuitPainter.paintCircuit;
|
import java.awt.event.ComponentEvent;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CircuitPanel extends JPanel {
|
public class CircuitPanel extends JPanel {
|
||||||
|
|
||||||
@Getter
|
|
||||||
private Circuit circuit = new Circuit();
|
|
||||||
|
|
||||||
private final CircuitPanelMouseAdapter mouseAdapter = new CircuitPanelMouseAdapter(this);
|
private final CircuitPanelMouseAdapter mouseAdapter = new CircuitPanelMouseAdapter(this);
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@NonNull
|
||||||
|
private Circuit circuit = new Circuit();
|
||||||
|
|
||||||
public CircuitPanel() {
|
public CircuitPanel() {
|
||||||
new CircuitPanelDropTarget(this);
|
new CircuitPanelDropTarget(this);
|
||||||
|
addComponentListener(new ComponentAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void componentResized(final ComponentEvent e) {
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCircuit(@NonNull final Circuit circuit) {
|
||||||
|
this.circuit = circuit;
|
||||||
|
this.circuit.evaluate();
|
||||||
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paint(final Graphics _g) {
|
public void paint(final Graphics graphics) {
|
||||||
final Graphics2D g = (Graphics2D) _g;
|
final Graphics2D g = (Graphics2D) graphics;
|
||||||
paintCircuit(circuit, g, getWidth(), getHeight());
|
CircuitPainter.draw(g, circuit, getWidth(), getHeight());
|
||||||
mouseAdapter.drawHover(g);
|
mouseAdapter.drawHover(g);
|
||||||
mouseAdapter.drawDrag(g);
|
mouseAdapter.drawDrag(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newCircuit() {
|
|
||||||
circuit = new Circuit();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
import de.ph87.electro.circuit.part.parts.*;
|
||||||
import de.ph87.electro.common.AbstractDropTarget;
|
import de.ph87.electro.common.AbstractDropTarget;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.ALIGN;
|
||||||
|
|
||||||
public class CircuitPanelDropTarget extends AbstractDropTarget {
|
public class CircuitPanelDropTarget extends AbstractDropTarget {
|
||||||
|
|
||||||
private final CircuitPanel circuitPanel;
|
private final CircuitPanel circuitPanel;
|
||||||
@ -18,31 +19,30 @@ public class CircuitPanelDropTarget extends AbstractDropTarget {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean drop(final Point point, final String data) {
|
protected boolean drop(final Point point, final String data) {
|
||||||
final Position position = new Position(point);
|
final Point aligned = ALIGN(point);
|
||||||
if (data.equals(Battery.class.getSimpleName())) {
|
if (data.equals(Battery.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Battery(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Battery(aligned));
|
||||||
} else if (data.equals(ConnectorCorner.class.getSimpleName())) {
|
} else if (data.equals(ConnectorCorner.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new ConnectorCorner(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new ConnectorCorner(aligned));
|
||||||
} else if (data.equals(ConnectorEdge.class.getSimpleName())) {
|
} else if (data.equals(ConnectorEdge.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new ConnectorEdge(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new ConnectorEdge(aligned));
|
||||||
} else if (data.equals(ConnectorMiddle.class.getSimpleName())) {
|
} else if (data.equals(ConnectorMiddle.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new ConnectorMiddle(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new ConnectorMiddle(aligned));
|
||||||
} else if (data.equals(Light.class.getSimpleName())) {
|
} else if (data.equals(Light.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Light(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Light(aligned));
|
||||||
} else if (data.equals(Switch1x1.class.getSimpleName())) {
|
} else if (data.equals(Switch1x1.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Switch1x1(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Switch1x1(aligned));
|
||||||
} else if (data.equals(Switch1x2.class.getSimpleName())) {
|
} else if (data.equals(Switch1x2.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Switch1x2(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Switch1x2(aligned));
|
||||||
} else if (data.equals(SwitchCross.class.getSimpleName())) {
|
} else if (data.equals(SwitchCross.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new SwitchCross(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new SwitchCross(aligned));
|
||||||
} else if (data.equals(Poti.class.getSimpleName())) {
|
} else if (data.equals(Poti.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Poti(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Poti(aligned));
|
||||||
} else if (data.equals(Voltmeter.class.getSimpleName())) {
|
} else if (data.equals(Voltmeter.class.getSimpleName())) {
|
||||||
circuitPanel.getCircuit().addPart(new Voltmeter(circuitPanel.getCircuit(), position));
|
circuitPanel.getCircuit().addPart(new Voltmeter(aligned));
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
circuitPanel.repaint();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import de.ph87.electro.circuit.wire.Wire;
|
import de.ph87.electro.circuit.wire.Wire;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
@ -12,21 +12,18 @@ import java.awt.event.MouseEvent;
|
|||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static java.awt.event.MouseEvent.*;
|
import static java.awt.event.MouseEvent.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
class CircuitPanelMouseAdapter extends MouseAdapter {
|
class CircuitPanelMouseAdapter extends MouseAdapter {
|
||||||
|
|
||||||
private final CircuitPanel circuitPanel;
|
private final CircuitPanel circuitPanel;
|
||||||
|
|
||||||
private Part partHover = null;
|
private Part part = null;
|
||||||
|
|
||||||
private Junction junctionHover = null;
|
private Junction junction = null;
|
||||||
|
|
||||||
private Wire wireHover = null;
|
private Wire wire = null;
|
||||||
|
|
||||||
private Part partDrag = null;
|
private Point dragging = null;
|
||||||
|
|
||||||
private Junction junctionDrag = null;
|
|
||||||
|
|
||||||
private Position dragPosition = null;
|
|
||||||
|
|
||||||
CircuitPanelMouseAdapter(final CircuitPanel circuitPanel) {
|
CircuitPanelMouseAdapter(final CircuitPanel circuitPanel) {
|
||||||
this.circuitPanel = circuitPanel;
|
this.circuitPanel = circuitPanel;
|
||||||
@ -36,135 +33,141 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(final MouseEvent event) {
|
public void mouseClicked(final MouseEvent event) {
|
||||||
final Position position = new Position(event);
|
hoverUpdate(event);
|
||||||
|
|
||||||
if (event.getButton() == BUTTON3) {
|
if (wire != null) {
|
||||||
if (wireHover != null) {
|
switch (event.getButton()) {
|
||||||
circuitPanel.getCircuit().disconnect(wireHover);
|
case BUTTON3:
|
||||||
wireHover = null;
|
circuitPanel.getCircuit().disconnect(wire);
|
||||||
circuitPanel.repaint();
|
circuitPanel.getCircuit().evaluate();
|
||||||
return;
|
wire = null;
|
||||||
}
|
break;
|
||||||
if (partHover != null) {
|
|
||||||
circuitPanel.getCircuit().removePart(partHover);
|
|
||||||
partHover = null;
|
|
||||||
circuitPanel.repaint();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
circuitPanel.getCircuit().findPartByPosition(position).ifPresent(part -> {
|
if (part != null) {
|
||||||
switch (event.getButton()) {
|
switch (event.getButton()) {
|
||||||
case BUTTON1:
|
case BUTTON1:
|
||||||
part.action();
|
part.action();
|
||||||
circuitPanel.repaint();
|
circuitPanel.getCircuit().evaluate();
|
||||||
break;
|
break;
|
||||||
case BUTTON2:
|
case BUTTON2:
|
||||||
part.rotate();
|
part.rotate();
|
||||||
circuitPanel.repaint();
|
break;
|
||||||
|
case BUTTON3:
|
||||||
|
circuitPanel.getCircuit().removePart(part);
|
||||||
|
circuitPanel.getCircuit().evaluate();
|
||||||
|
part = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(final MouseEvent event) {
|
public void mouseMoved(final MouseEvent event) {
|
||||||
findHover(new Position(event));
|
hoverUpdate(event);
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mousePressed(final MouseEvent event) {
|
|
||||||
final Position position = new Position(event);
|
|
||||||
circuitPanel.getCircuit().findPartByPosition(position).ifPresent(part -> startPartOrJunction(part, position));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startPartOrJunction(final Part part, final Position position) {
|
|
||||||
part.findJunctionByPosition(position).ifPresentOrElse(junction -> {
|
|
||||||
partDrag = null;
|
|
||||||
junctionDrag = junction;
|
|
||||||
}, () -> {
|
|
||||||
partDrag = part;
|
|
||||||
junctionDrag = null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(final MouseEvent event) {
|
public void mouseDragged(final MouseEvent event) {
|
||||||
final Position position = new Position(event);
|
if (dragging == null) {
|
||||||
findHover(position);
|
hoverUpdate(event);
|
||||||
if (partDrag != null || junctionDrag != null) {
|
|
||||||
dragPosition = position;
|
|
||||||
}
|
}
|
||||||
|
dragging = event.getPoint();
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findHover(final Position position) {
|
|
||||||
partHover = circuitPanel.getCircuit().findPartByPosition(position).orElse(null);
|
|
||||||
junctionHover = partHover != null ? partHover.findJunctionByPosition(position).orElse(null) : null;
|
|
||||||
if (junctionHover != null) {
|
|
||||||
partHover = null;
|
|
||||||
wireHover = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
wireHover = circuitPanel.getCircuit().findWireByPosition(position).orElse(null);
|
|
||||||
if (wireHover != null) {
|
|
||||||
partHover = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseReleased(final MouseEvent event) {
|
public void mouseReleased(final MouseEvent event) {
|
||||||
final Position position = new Position(event);
|
if (dragging == null) {
|
||||||
if (partDrag != null) {
|
return;
|
||||||
circuitPanel.getCircuit().movePart(partDrag, position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (junctionDrag != null) {
|
if (part != null) {
|
||||||
circuitPanel.getCircuit().findJunctionByAbsolute(position).filter(destination -> destination != junctionDrag).ifPresent(destination -> circuitPanel.getCircuit().connect(junctionDrag, destination));
|
final Point aligned = ALIGN(event.getPoint());
|
||||||
|
if (!circuitPanel.getCircuit().isOccupied(aligned)) {
|
||||||
|
part.setPosition(aligned);
|
||||||
|
log.info("Dropped Part at {}: {}", aligned, part);
|
||||||
|
} else {
|
||||||
|
log.info("Cell already occupied at: {}", aligned);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
partDrag = null;
|
if (junction != null) {
|
||||||
junctionDrag = null;
|
final Junction source = junction;
|
||||||
dragPosition = null;
|
hoverUpdate(event);
|
||||||
|
if (junction != null) {
|
||||||
|
final Wire wire = circuitPanel.getCircuit().connect(source, junction);
|
||||||
|
log.info("Wire CREATED: {}", wire);
|
||||||
|
circuitPanel.getCircuit().evaluate();
|
||||||
|
} else {
|
||||||
|
log.info("No Wire created: No destination junction found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dragging = null;
|
||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawHover(final Graphics2D g) {
|
private void hoverUpdate(final MouseEvent event) {
|
||||||
if (partHover != null) {
|
final Point position = event.getPoint();
|
||||||
g.setColor(PART_HOVER_COLOR);
|
part = circuitPanel.getCircuit().findPartByPosition(position).orElse(null);
|
||||||
g.setStroke(HOVER_STROKE);
|
junction = part != null ? part.findJunctionByPosition(position).orElse(null) : null;
|
||||||
g.drawRect(partHover.getPosition().absolute.x, partHover.getPosition().absolute.y, RASTER, RASTER);
|
if (junction != null) {
|
||||||
|
part = null;
|
||||||
|
wire = null;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (junctionHover != null) {
|
wire = circuitPanel.getCircuit().findWireByPosition(position).orElse(null);
|
||||||
g.setColor(junctionHover.getColor());
|
if (wire != null) {
|
||||||
g.fillArc(junctionHover.getPosition().absolute.x - JUNCTION_RADIUS_HOVER, junctionHover.getPosition().absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
part = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawHover(final Graphics2D g) {
|
||||||
|
if (part != null) {
|
||||||
|
g.setColor(JUNCTION_HOVER_BORDER_COLOR);
|
||||||
|
g.setStroke(HOVER_STROKE);
|
||||||
|
g.drawRect(part.getPosition().x, part.getPosition().y, RASTER, RASTER);
|
||||||
|
}
|
||||||
|
if (junction != null) {
|
||||||
|
final Point absolute = junction.getAbsolute();
|
||||||
|
|
||||||
|
g.setColor(junction.getColor());
|
||||||
|
g.fillArc(absolute.x - JUNCTION_RADIUS_HOVER, absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
||||||
|
|
||||||
g.setColor(JUNCTION_HOVER_BORDER_COLOR);
|
g.setColor(JUNCTION_HOVER_BORDER_COLOR);
|
||||||
g.setStroke(HOVER_STROKE);
|
g.setStroke(HOVER_STROKE);
|
||||||
g.drawArc(junctionHover.getPosition().absolute.x - JUNCTION_RADIUS_HOVER, junctionHover.getPosition().absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
g.drawArc(absolute.x - JUNCTION_RADIUS_HOVER, absolute.y - JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 2 * JUNCTION_RADIUS_HOVER, 0, 360);
|
||||||
}
|
}
|
||||||
if (wireHover != null) {
|
if (wire != null) {
|
||||||
|
final Point aa = wire.getA().getAbsolute();
|
||||||
|
final Point bb = wire.getB().getAbsolute();
|
||||||
|
|
||||||
g.setColor(WIRE_HOVER_COLOR_BACK);
|
g.setColor(WIRE_HOVER_COLOR_BACK);
|
||||||
g.setStroke(WIRE_HOVER_STROKE_BACK);
|
g.setStroke(WIRE_HOVER_STROKE_BACK);
|
||||||
g.drawLine(wireHover.getA().getPosition().absolute.x, wireHover.getA().getPosition().absolute.y, wireHover.getB().getPosition().absolute.x, wireHover.getB().getPosition().absolute.y);
|
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
||||||
|
|
||||||
g.setColor(wireHover.getA().getColor());
|
g.setColor(wire.getA().getColor());
|
||||||
g.setStroke(WIRE_HOVER_STROKE);
|
g.setStroke(WIRE_HOVER_STROKE);
|
||||||
g.drawLine(wireHover.getA().getPosition().absolute.x, wireHover.getA().getPosition().absolute.y, wireHover.getB().getPosition().absolute.x, wireHover.getB().getPosition().absolute.y);
|
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawDrag(final Graphics2D g) {
|
public void drawDrag(final Graphics2D g) {
|
||||||
if (dragPosition != null) {
|
if (dragging != null) {
|
||||||
if (partDrag != null) {
|
if (part != null) {
|
||||||
g.setColor(PART_HOVER_COLOR);
|
g.setColor(PART_HOVER_COLOR);
|
||||||
g.fillRect(dragPosition.absolute.x - P50, dragPosition.absolute.y - P50, RASTER, RASTER);
|
g.fillRect(dragging.x - P50, dragging.y - P50, RASTER, RASTER);
|
||||||
}
|
}
|
||||||
if (junctionDrag != null) {
|
if (junction != null) {
|
||||||
g.setColor(junctionDrag.getColor());
|
g.setColor(junction.getColor());
|
||||||
g.setStroke(WIRE_STROKE);
|
g.setStroke(WIRE_STROKE);
|
||||||
g.drawLine(junctionDrag.getPosition().absolute.x, junctionDrag.getPosition().absolute.y, dragPosition.absolute.x, dragPosition.absolute.y);
|
final Point absolute = junction.getAbsolute();
|
||||||
|
g.drawLine(absolute.x, absolute.y, dragging.x, dragging.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,19 +5,19 @@ import de.ph87.electro.circuit.part.parts.Battery;
|
|||||||
import de.ph87.electro.circuit.part.parts.Poti;
|
import de.ph87.electro.circuit.part.parts.Poti;
|
||||||
import de.ph87.electro.circuit.part.parts.Voltmeter;
|
import de.ph87.electro.circuit.part.parts.Voltmeter;
|
||||||
|
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class Demos {
|
public class Demos {
|
||||||
|
|
||||||
public static Circuit potiAndVoltmeter() {
|
public static Circuit potiAndVoltmeter() {
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
final Battery battery = circuit.addPart(new Battery(circuit, RST(0, 0)));
|
final Battery battery = circuit.addPart(new Battery(RASTER(1, 0)));
|
||||||
final Poti poti = circuit.addPart(new Poti(circuit, RST(0, 1)));
|
final Poti poti = circuit.addPart(new Poti(RASTER(1, 2)));
|
||||||
final Voltmeter voltmeter = circuit.addPart(new Voltmeter(circuit, RST(0, 2)));
|
final Voltmeter voltmeter = circuit.addPart(new Voltmeter(RASTER(1, 4)));
|
||||||
circuit.connect(battery.getMinus(), poti.getCommon());
|
circuit.connect(battery.getMinus(), poti.getCommon());
|
||||||
circuit.connect(battery.getMinus(), voltmeter.getA());
|
|
||||||
circuit.connect(battery.getPlus(), poti.getEnd());
|
circuit.connect(battery.getPlus(), poti.getEnd());
|
||||||
|
circuit.connect(poti.getCommon(), voltmeter.getA());
|
||||||
circuit.connect(poti.getMiddle(), voltmeter.getB());
|
circuit.connect(poti.getMiddle(), voltmeter.getB());
|
||||||
return circuit;
|
return circuit;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,8 +25,8 @@ public enum Orientation {
|
|||||||
this.map = rotate;
|
this.map = rotate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point rotate(final Point p) {
|
public Point rotate(final Point inside) {
|
||||||
return map.apply(p);
|
return map.apply(inside);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Orientation clockwise() {
|
public Orientation clockwise() {
|
||||||
|
|||||||
@ -1,59 +1,67 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import de.ph87.electro.circuit.part.junction.JunctionDto;
|
import de.ph87.electro.circuit.part.junction.JunctionDto;
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
import de.ph87.electro.circuit.part.parts.*;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.PART_BACKGROUND;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
import static java.lang.Math.round;
|
||||||
import static de.ph87.electro.circuit.part.Position.ZERO;
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
@ToString(onlyExplicitlyIncluded = true)
|
@ToString(onlyExplicitlyIncluded = true)
|
||||||
public abstract class Part {
|
public abstract class Part {
|
||||||
|
|
||||||
private final Circuit circuit;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@ToString.Include
|
@ToString.Include
|
||||||
private final String uuid;
|
private final String uuid;
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
@Setter
|
||||||
@ToString.Include
|
@ToString.Include
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Getter
|
private Point position;
|
||||||
private Position position;
|
|
||||||
|
|
||||||
@Getter
|
protected Orientation orientation = Orientation.R0;
|
||||||
private Orientation orientation = Orientation.R0;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final List<Junction> junctions = new ArrayList<>();
|
private final List<Junction> junctions = new ArrayList<>();
|
||||||
|
|
||||||
protected final Render render = new Render();
|
@NonNull
|
||||||
|
private AffineTransform transform;
|
||||||
|
|
||||||
protected Part(final Circuit circuit, final String name, final Position position) {
|
protected Part(final String name, final Point position) {
|
||||||
this.circuit = circuit;
|
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
setPosition(position);
|
this.position = position;
|
||||||
|
this.transform = newTransform();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Part(final Circuit circuit, final PartDto dto) {
|
protected Part(final PartDto dto) {
|
||||||
this.circuit = circuit;
|
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.orientation = dto.getOrientation();
|
this.orientation = dto.getOrientation();
|
||||||
setPosition(new Position(dto.getPosition()));
|
this.position = dto.getPosition();
|
||||||
|
this.transform = newTransform();
|
||||||
|
}
|
||||||
|
|
||||||
|
private AffineTransform newTransform() {
|
||||||
|
final AffineTransform transform = new AffineTransform();
|
||||||
|
transform.translate(position.x, position.y);
|
||||||
|
transform.rotate(orientation.getRadians(), P50, P50);
|
||||||
|
return transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point transform(final Point point) {
|
||||||
|
final Point2D result = transform.transform(point, new Point2D.Double());
|
||||||
|
return new Point((int) round(result.getX()), (int) round(result.getY()));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Junction addJunction(final String name, final int x, final int y) {
|
protected Junction addJunction(final String name, final int x, final int y) {
|
||||||
@ -69,85 +77,64 @@ public abstract class Part {
|
|||||||
return junction;
|
return junction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosition(final Position position) {
|
public void setPosition(final Point position) {
|
||||||
this.position = position.alignToRaster();
|
this.position = position;
|
||||||
junctions.forEach(Junction::updatePosition);
|
this.transform = newTransform();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rotate() {
|
public void rotate() {
|
||||||
orientation = orientation.clockwise();
|
this.orientation = orientation.clockwise();
|
||||||
junctions.forEach(Junction::updatePosition);
|
this.transform = newTransform();
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void render() {
|
public final void draw(final Graphics2D g) {
|
||||||
render.rect(ZERO, RASTER, RASTER, null, null, PART_BACKGROUND);
|
g.setColor(PART_BACK_COLOR);
|
||||||
|
g.fillRect(0, 0, RASTER, RASTER);
|
||||||
_labels();
|
_render(g);
|
||||||
render.resetTransform();
|
junctions.forEach(junction -> junction.draw(g));
|
||||||
|
_labels(g);
|
||||||
_render();
|
|
||||||
render.resetTransform();
|
|
||||||
|
|
||||||
junctions.forEach(junction -> junction.render(render));
|
|
||||||
render.resetTransform();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
g.drawImage(render.getImage(), position.absolute.x, position.absolute.y, null);
|
// -
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByUuid(final String junctionUuid) {
|
protected void _labels(final Graphics2D g) {
|
||||||
return junctions.stream().filter(junction -> junction.getUuid().equals(junctionUuid)).findFirst();
|
// -
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Junction> findJunctionByPosition(final Position position) {
|
public void action() {
|
||||||
return junctions.stream().filter(junction -> junction.intersects(position)).findFirst();
|
// -
|
||||||
}
|
|
||||||
|
|
||||||
public final void action() {
|
|
||||||
_action();
|
|
||||||
evaluate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void _action() {
|
|
||||||
// may be overwritten
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void postCalculate() {
|
public void postCalculate() {
|
||||||
// may be overwritten
|
// -
|
||||||
}
|
|
||||||
|
|
||||||
protected void _render() {
|
|
||||||
// may be overwritten
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void _labels() {
|
|
||||||
// may be overwritten
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void evaluate() {
|
|
||||||
if (circuit != null) {
|
|
||||||
circuit.evaluate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<InnerConnection> getInnerConnections() {
|
public List<InnerConnection> getInnerConnections() {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Part fromDto(final Circuit circuit, final PartDto abstractDto) {
|
public Optional<Junction> findJunctionByUuid(final String junctionUuid) {
|
||||||
|
return junctions.stream().filter(junction -> junction.getUuid().equals(junctionUuid)).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Junction> findJunctionByPosition(final Point position) {
|
||||||
|
return junctions.stream().filter(junction -> junction.intersects(position)).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Part fromDto(final PartDto abstractDto) {
|
||||||
return switch (abstractDto) {
|
return switch (abstractDto) {
|
||||||
case final BatteryDto dto -> new Battery(circuit, dto);
|
case final BatteryDto dto -> new Battery(dto);
|
||||||
case final ConnectorCornerDto dto -> new ConnectorCorner(circuit, dto);
|
case final ConnectorCornerDto dto -> new ConnectorCorner(dto);
|
||||||
case final ConnectorEdgeDto dto -> new ConnectorEdge(circuit, dto);
|
case final ConnectorEdgeDto dto -> new ConnectorEdge(dto);
|
||||||
case final ConnectorMiddleDto dto -> new ConnectorMiddle(circuit, dto);
|
case final ConnectorMiddleDto dto -> new ConnectorMiddle(dto);
|
||||||
case final LightDto dto -> new Light(circuit, dto);
|
case final LightDto dto -> new Light(dto);
|
||||||
case final Switch1x1Dto dto -> new Switch1x1(circuit, dto);
|
case final Switch1x1Dto dto -> new Switch1x1(dto);
|
||||||
case final Switch1x2Dto dto -> new Switch1x2(circuit, dto);
|
case final Switch1x2Dto dto -> new Switch1x2(dto);
|
||||||
case final SwitchCrossDto dto -> new SwitchCross(circuit, dto);
|
case final SwitchCrossDto dto -> new SwitchCross(dto);
|
||||||
case final PotiDto dto -> new Poti(circuit, dto);
|
case final PotiDto dto -> new Poti(dto);
|
||||||
case final VoltmeterDto dto -> new Voltmeter(circuit, dto);
|
case final VoltmeterDto dto -> new Voltmeter(dto);
|
||||||
case null, default -> throw new RuntimeException();
|
case null, default -> throw new RuntimeException();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ public abstract class PartDto {
|
|||||||
protected PartDto(final Part part) {
|
protected PartDto(final Part part) {
|
||||||
this.uuid = part.getUuid();
|
this.uuid = part.getUuid();
|
||||||
this.name = part.getName();
|
this.name = part.getName();
|
||||||
this.position = part.getPosition().absolute;
|
this.position = part.getPosition();
|
||||||
this.orientation = part.getOrientation();
|
this.orientation = part.getOrientation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,94 +0,0 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.ToString;
|
|
||||||
import org.apache.commons.math3.linear.RealVector;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
|
||||||
import static de.ph87.electro.CONFIG.WIRE_HOVER_STROKE_BACK;
|
|
||||||
import static java.lang.Math.*;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@ToString
|
|
||||||
public final class Position {
|
|
||||||
|
|
||||||
public static final Position ZERO = new Position(0, 0);
|
|
||||||
|
|
||||||
public final Point absolute;
|
|
||||||
|
|
||||||
public final Point inside;
|
|
||||||
|
|
||||||
public final Point raster;
|
|
||||||
|
|
||||||
public static Position ABS(final RealVector vector) {
|
|
||||||
return ABS(vector.getEntry(0), vector.getEntry(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Position ABS(final double absoluteX, final double absoluteY) {
|
|
||||||
return new Position((int) round(absoluteX), (int) round(absoluteY));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Position RST(final int x, final int y) {
|
|
||||||
return ABS(x * RASTER, y * RASTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Position(final int absoluteX, final int absoluteY) {
|
|
||||||
this.absolute = new Point(absoluteX, absoluteY);
|
|
||||||
this.raster = new Point(absoluteX / RASTER, absoluteY / RASTER);
|
|
||||||
this.inside = new Point(absoluteX % RASTER, absoluteY % RASTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(final Object obj) {
|
|
||||||
if (!(obj instanceof final Position other)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return absolute.equals(other.absolute);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return absolute.hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Position(final Point point) {
|
|
||||||
this(point.x, point.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Position(final MouseEvent event) {
|
|
||||||
this(event.getX(), event.getY());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Position plus(final Point vector) {
|
|
||||||
return new Position(absolute.x + vector.x, absolute.y + vector.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public double distance(final Position position) {
|
|
||||||
return sqrt(pow(position.absolute.x - absolute.x, 2) + pow(position.absolute.y - absolute.y, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean distanceToLine(final Position lineStart, final Position lineEnd) {
|
|
||||||
double dx = lineEnd.absolute.x - lineStart.absolute.x;
|
|
||||||
double dy = lineEnd.absolute.y - lineStart.absolute.y;
|
|
||||||
double lineLength = dx * dx + dy * dy;
|
|
||||||
if (lineLength == 0) {
|
|
||||||
return distance(lineStart) <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
|
||||||
}
|
|
||||||
|
|
||||||
double t = ((absolute.x - lineStart.absolute.x) * dx + (absolute.y - lineStart.absolute.y) * dy) / lineLength;
|
|
||||||
t = Math.max(0, Math.min(1, t));
|
|
||||||
|
|
||||||
double closestX = lineStart.absolute.x + t * dx;
|
|
||||||
double closestY = lineStart.absolute.y + t * dy;
|
|
||||||
final double distance = sqrt(pow(closestX - absolute.x, 2) + pow(closestY - absolute.y, 2));
|
|
||||||
return distance <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Position alignToRaster() {
|
|
||||||
return new Position(raster.x * RASTER, raster.y * RASTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,82 +0,0 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.geom.AffineTransform;
|
|
||||||
import java.awt.geom.Rectangle2D;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P50;
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
|
||||||
import static java.lang.Math.round;
|
|
||||||
|
|
||||||
public class Render {
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final BufferedImage image = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
|
||||||
|
|
||||||
private final Graphics2D g = image.createGraphics();
|
|
||||||
|
|
||||||
public void line(final Junction junction0, final Junction junction1, final Color color, final Stroke stroke) {
|
|
||||||
line(junction0.getPosition(), junction1.getPosition(), color, stroke);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void line(final Position p0, final Position p1, final Color color, final Stroke stroke) {
|
|
||||||
if (color != null) {
|
|
||||||
g.setColor(color);
|
|
||||||
}
|
|
||||||
if (stroke != null) {
|
|
||||||
g.setStroke(stroke);
|
|
||||||
}
|
|
||||||
g.drawLine(p0.inside.x, p0.inside.y, p1.inside.x, p1.inside.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rect(final Position p, final double w, final double h, final Color border, final Stroke stroke, final Color fill) {
|
|
||||||
final int _w = (int) round(w);
|
|
||||||
final int _h = (int) round(h);
|
|
||||||
if (fill != null) {
|
|
||||||
g.setColor(fill);
|
|
||||||
g.fillRect(p.inside.x, p.inside.y, _w, _h);
|
|
||||||
}
|
|
||||||
if (border != null && stroke != null) {
|
|
||||||
g.setColor(border);
|
|
||||||
g.setStroke(stroke);
|
|
||||||
g.drawRect(p.inside.x, p.inside.y, _w, _h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void circle(final Position center, final double radius, final Color border, final Stroke stroke, final Color fill, final double startAngle, final double arcAngle) {
|
|
||||||
final int _x = (int) round(center.inside.x - radius);
|
|
||||||
final int _y = (int) round(center.inside.y - radius);
|
|
||||||
final int diameter = (int) round(radius * 2);
|
|
||||||
if (fill != null) {
|
|
||||||
g.setColor(fill);
|
|
||||||
g.fillArc(_x, _y, diameter, diameter, (int) round(startAngle), (int) round(arcAngle));
|
|
||||||
}
|
|
||||||
if (border != null && stroke != null) {
|
|
||||||
g.setColor(border);
|
|
||||||
g.setStroke(stroke);
|
|
||||||
g.drawArc(_x, _y, diameter, diameter, (int) round(startAngle), (int) round(arcAngle));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void textCenter(final Font font, final String string, final int innerX, final int innerY, final Color color) {
|
|
||||||
g.setFont(font);
|
|
||||||
g.setColor(color);
|
|
||||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
|
||||||
final int x = innerX - (int) round(bounds.getWidth()) / 2;
|
|
||||||
final int y = innerY + (int) round(bounds.getHeight()) / 2;
|
|
||||||
g.drawString(string, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clockwise(final Orientation orientation) {
|
|
||||||
g.rotate(orientation.getRadians(), P50, P50);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void resetTransform() {
|
|
||||||
g.setTransform(new AffineTransform());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,8 +1,6 @@
|
|||||||
package de.ph87.electro.circuit.part.junction;
|
package de.ph87.electro.circuit.part.junction;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.Render;
|
|
||||||
import de.ph87.electro.circuit.wire.Wire;
|
import de.ph87.electro.circuit.wire.Wire;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -27,9 +25,7 @@ public class Junction {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private final Point relative;
|
private final Point inside;
|
||||||
|
|
||||||
private Position position;
|
|
||||||
|
|
||||||
private final Set<Wire> wires = new HashSet<>();
|
private final Set<Wire> wires = new HashSet<>();
|
||||||
|
|
||||||
@ -44,20 +40,18 @@ public class Junction {
|
|||||||
return wires.stream().map(wire -> wire.getOpposite(this)).map(Junction::getUuid).toList();
|
return wires.stream().map(wire -> wire.getOpposite(this)).map(Junction::getUuid).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final String name, final Point relative) {
|
public Junction(final Part owner, final String name, final Point inside) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.relative = relative;
|
this.inside = inside;
|
||||||
updatePosition();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final JunctionDto dto, final Point relative) {
|
public Junction(final Part owner, final JunctionDto dto, final Point inside) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.relative = relative;
|
this.inside = inside;
|
||||||
updatePosition();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVoltage(final double voltage) {
|
public void setVoltage(final double voltage) {
|
||||||
@ -65,16 +59,17 @@ public class Junction {
|
|||||||
this.color = Double.isNaN(voltage) ? VOLTAGE_UNKNOWN_COLOR : ((voltage >= VOLTAGE_HIGH_MIN) ? VOLTAGE_HIGH_COLOR : VOLTAGE_LOW_COLOR);
|
this.color = Double.isNaN(voltage) ? VOLTAGE_UNKNOWN_COLOR : ((voltage >= VOLTAGE_HIGH_MIN) ? VOLTAGE_HIGH_COLOR : VOLTAGE_LOW_COLOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(final Render render) {
|
public void draw(final Graphics2D g) {
|
||||||
render.circle(position, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color, 0, 360);
|
g.setColor(color);
|
||||||
|
g.fillArc(inside.x - JUNCTION_RADIUS, inside.y - JUNCTION_RADIUS, 2 * JUNCTION_RADIUS, 2 * JUNCTION_RADIUS, 0, 360);
|
||||||
|
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.setStroke(JUNCTION_STROKE);
|
||||||
|
g.drawArc(inside.x - JUNCTION_RADIUS, inside.y - JUNCTION_RADIUS, 2 * JUNCTION_RADIUS, 2 * JUNCTION_RADIUS, 0, 360);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePosition() {
|
public boolean intersects(final Point position) {
|
||||||
position = owner.getPosition().plus(owner.getOrientation().rotate(relative));
|
return getAbsolute().distance(position) <= JUNCTION_RADIUS_HOVER;
|
||||||
}
|
|
||||||
|
|
||||||
public boolean intersects(final Position position) {
|
|
||||||
return this.position.distance(position) <= JUNCTION_RADIUS_HOVER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void collectConnectedJunctions(final Set<Junction> connected) {
|
public void collectConnectedJunctions(final Set<Junction> connected) {
|
||||||
@ -86,4 +81,8 @@ public class Junction {
|
|||||||
owner.getInnerConnections().stream().flatMap(innerConnection -> innerConnection.filter(this)).forEach(opposite -> opposite.collectConnectedJunctions(connected));
|
owner.getInnerConnections().stream().flatMap(innerConnection -> innerConnection.filter(this)).forEach(opposite -> opposite.collectConnectedJunctions(connected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Point getAbsolute() {
|
||||||
|
return owner.transform(inside);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,7 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -15,21 +12,22 @@ import java.util.List;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static de.ph87.electro.circuit.part.Position.ABS;
|
import static de.ph87.electro.circuit.CircuitPainter.*;
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
|
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
|
||||||
public class Battery extends Part {
|
public class Battery extends Part {
|
||||||
|
|
||||||
private static final double MINUS_W = 0.1 * RASTER;
|
private static final int MINUS_W = (int) round(0.1 * RASTER);
|
||||||
|
|
||||||
private static final double MINUS_H = 0.3 * RASTER;
|
private static final int MINUS_H = (int) round(0.3 * RASTER);
|
||||||
|
|
||||||
private static final double GAP = 0.05 * RASTER;
|
private static final int GAP = (int) round(0.05 * RASTER);
|
||||||
|
|
||||||
private static final double PLUS_W = 0.02 * RASTER;
|
private static final int PLUS_W = (int) round(0.02 * RASTER);
|
||||||
|
|
||||||
private static final double PLUS_H = 0.6 * RASTER;
|
private static final int PLUS_H = (int) round(0.6 * RASTER);
|
||||||
|
|
||||||
private final Junction minus;
|
private final Junction minus;
|
||||||
|
|
||||||
@ -43,41 +41,19 @@ public class Battery extends Part {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
private double resistance = 0.05;
|
private double resistance = 0.05;
|
||||||
|
|
||||||
public Battery(final Circuit circuit, final Position position) {
|
public Battery(final Point position) {
|
||||||
super(circuit, "Batterie", position);
|
super("Batterie", position);
|
||||||
minus = addJunction("MINUS", P10, P50);
|
minus = addJunction("MINUS", P10, P50);
|
||||||
plus = addJunction("PLUS", P90, P50);
|
plus = addJunction("PLUS", P90, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Battery(final Circuit circuit, final BatteryDto dto) {
|
public Battery(final BatteryDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
minus = addJunction(dto.getMinus(), P10, P50);
|
minus = addJunction(dto.getMinus(), P10, P50);
|
||||||
plus = addJunction(dto.getPlus(), P90, P50);
|
plus = addJunction(dto.getPlus(), P90, P50);
|
||||||
voltage = dto.getVoltage();
|
voltage = dto.getVoltage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void _render() {
|
|
||||||
render.clockwise(getOrientation());
|
|
||||||
render.line(ABS(P10, P50), ABS(P50 - GAP / 2 - MINUS_W / 2, P50), Color.BLACK, SYMBOL_STROKE);
|
|
||||||
render.line(ABS(P50 + GAP / 2 + PLUS_W / 2, P50), ABS(P90, P50), Color.BLACK, SYMBOL_STROKE);
|
|
||||||
render.rect(ABS(P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2), MINUS_W, MINUS_H, null, null, Color.BLACK);
|
|
||||||
render.rect(ABS(P50 + GAP / 2, P50 - PLUS_H / 2), PLUS_W, PLUS_H, null, null, Color.BLACK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void _labels() {
|
|
||||||
final int x = RASTER / 2;
|
|
||||||
final int y0;
|
|
||||||
if (getOrientation() == Orientation.R180) {
|
|
||||||
y0 = P95 - LABEL_FONT.getSize();
|
|
||||||
} else {
|
|
||||||
render.clockwise(getOrientation());
|
|
||||||
y0 = P05;
|
|
||||||
}
|
|
||||||
render.textCenter(LABEL_FONT, "%.1fV".formatted(voltage), x, y0, Color.BLACK);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Stream<Battery> filterCast(final Part part) {
|
public static Stream<Battery> filterCast(final Part part) {
|
||||||
if (part instanceof final Battery battery) {
|
if (part instanceof final Battery battery) {
|
||||||
return Stream.of(battery);
|
return Stream.of(battery);
|
||||||
@ -90,4 +66,17 @@ public class Battery extends Part {
|
|||||||
return List.of(new InnerConnection(minus, plus, resistance));
|
return List.of(new InnerConnection(minus, plus, resistance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void _render(final Graphics2D g) {
|
||||||
|
drawLine(g, P10, P50, P50 - GAP / 2 - MINUS_W / 2, P50, Color.BLACK, SYMBOL_STROKE);
|
||||||
|
drawLine(g, P50 + GAP / 2 + PLUS_W / 2, P50, P90, P50, Color.BLACK, SYMBOL_STROKE);
|
||||||
|
fillRect(g, P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2, MINUS_W, MINUS_H, Color.BLACK);
|
||||||
|
fillRect(g, P50 + GAP / 2, P50 - PLUS_H / 2, PLUS_W, PLUS_H, Color.BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void _labels(final Graphics2D g) {
|
||||||
|
drawText(g, LABEL_FONT, "%.1fV".formatted(voltage), P50, P10, Color.BLACK, orientation);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P10;
|
import static de.ph87.electro.CONFIG.P10;
|
||||||
import static de.ph87.electro.CONFIG.P90;
|
import static de.ph87.electro.CONFIG.P90;
|
||||||
|
|
||||||
@ -18,14 +18,14 @@ public class ConnectorCorner extends Part {
|
|||||||
|
|
||||||
private final Junction j1;
|
private final Junction j1;
|
||||||
|
|
||||||
public ConnectorCorner(final Circuit circuit, final Position position) {
|
public ConnectorCorner(final Point position) {
|
||||||
super(circuit, "", position);
|
super("", position);
|
||||||
j0 = addJunction("J0", P10, P10);
|
j0 = addJunction("J0", P10, P10);
|
||||||
j1 = addJunction("J1", P90, P90);
|
j1 = addJunction("J1", P90, P90);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectorCorner(final Circuit circuit, final ConnectorCornerDto dto) {
|
public ConnectorCorner(final ConnectorCornerDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
j0 = addJunction(dto.getJ0(), P10, P10);
|
j0 = addJunction(dto.getJ0(), P10, P10);
|
||||||
j1 = addJunction(dto.getJ1(), P90, P90);
|
j1 = addJunction(dto.getJ1(), P90, P90);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P10;
|
import static de.ph87.electro.CONFIG.P10;
|
||||||
import static de.ph87.electro.CONFIG.P50;
|
import static de.ph87.electro.CONFIG.P50;
|
||||||
|
|
||||||
@ -18,14 +18,14 @@ public class ConnectorEdge extends Part {
|
|||||||
|
|
||||||
private final Junction j1;
|
private final Junction j1;
|
||||||
|
|
||||||
public ConnectorEdge(final Circuit circuit, final Position position) {
|
public ConnectorEdge(final Point position) {
|
||||||
super(circuit, "", position);
|
super("", position);
|
||||||
j0 = addJunction("J0", P10, P50);
|
j0 = addJunction("J0", P10, P50);
|
||||||
j1 = addJunction("J1", P50, P10);
|
j1 = addJunction("J1", P50, P10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectorEdge(final Circuit circuit, final ConnectorEdgeDto dto) {
|
public ConnectorEdge(final ConnectorEdgeDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
j0 = addJunction(dto.getJ0(), P10, P50);
|
j0 = addJunction(dto.getJ0(), P10, P50);
|
||||||
j1 = addJunction(dto.getJ1(), P50, P10);
|
j1 = addJunction(dto.getJ1(), P50, P10);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.P50;
|
import static de.ph87.electro.CONFIG.P50;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -15,13 +15,13 @@ public class ConnectorMiddle extends Part {
|
|||||||
|
|
||||||
private final Junction junction;
|
private final Junction junction;
|
||||||
|
|
||||||
public ConnectorMiddle(final Circuit circuit, final Position position) {
|
public ConnectorMiddle(final Point position) {
|
||||||
super(circuit, "", position);
|
super("", position);
|
||||||
junction = addJunction("J", P50, P50);
|
junction = addJunction("J", P50, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectorMiddle(final Circuit circuit, final ConnectorMiddleDto dto) {
|
public ConnectorMiddle(final ConnectorMiddleDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
junction = addJunction(dto.getJunction(), P50, P50);
|
junction = addJunction(dto.getJunction(), P50, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Orientation;
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -14,7 +12,7 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static de.ph87.electro.circuit.part.Position.ABS;
|
import static de.ph87.electro.circuit.CircuitPainter.*;
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.abs;
|
||||||
import static java.lang.Math.round;
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
@ -52,14 +50,14 @@ public class Light extends Part {
|
|||||||
|
|
||||||
private Color color = BULB_OFF_COLOR;
|
private Color color = BULB_OFF_COLOR;
|
||||||
|
|
||||||
public Light(final Circuit circuit, final Position position) {
|
public Light(final Point position) {
|
||||||
super(circuit, "Licht", position);
|
super("Licht", position);
|
||||||
a = addJunction("A", P10, P50);
|
a = addJunction("A", P10, P50);
|
||||||
b = addJunction("B", P90, P50);
|
b = addJunction("B", P90, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Light(final Circuit circuit, final LightDto dto) {
|
public Light(final LightDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
a = addJunction(dto.getB(), P10, P50);
|
a = addJunction(dto.getB(), P10, P50);
|
||||||
b = addJunction(dto.getA(), P90, P50);
|
b = addJunction(dto.getA(), P90, P50);
|
||||||
resistance = dto.getResistance();
|
resistance = dto.getResistance();
|
||||||
@ -68,7 +66,7 @@ public class Light extends Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void _action() {
|
public void action() {
|
||||||
defect = false;
|
defect = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,17 +83,17 @@ public class Light extends Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render() {
|
protected void _render(final Graphics2D g) {
|
||||||
render.line(a, b, Color.BLACK, SYMBOL_STROKE);
|
drawLine(g, a, b, Color.BLACK, SYMBOL_STROKE);
|
||||||
render.circle(ABS(P50, P50), BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color, 0, 360);
|
drawCircle(g, P50, P50, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color, 0, 360);
|
||||||
|
|
||||||
final double diag = 0.33 * RASTER;
|
final int diag = (int) round(0.33 * RASTER);
|
||||||
render.line(ABS(diag, diag), ABS(RASTER - diag, RASTER - diag), Color.BLACK, SYMBOL_STROKE);
|
drawLine(g, RASTER - diag, RASTER - diag, diag, diag, Color.BLACK, SYMBOL_STROKE);
|
||||||
render.line(ABS(diag, RASTER - diag), ABS(RASTER - diag, diag), Color.BLACK, SYMBOL_STROKE);
|
drawLine(g, RASTER - diag, diag, diag, RASTER - diag, Color.BLACK, SYMBOL_STROKE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _labels() {
|
protected void _labels(final Graphics2D g) {
|
||||||
final int x = RASTER / 2;
|
final int x = RASTER / 2;
|
||||||
final int y0;
|
final int y0;
|
||||||
final int y1;
|
final int y1;
|
||||||
@ -103,13 +101,13 @@ public class Light extends Part {
|
|||||||
y0 = P95 - LABEL_FONT.getSize();
|
y0 = P95 - LABEL_FONT.getSize();
|
||||||
y1 = P05;
|
y1 = P05;
|
||||||
} else {
|
} else {
|
||||||
render.clockwise(getOrientation());
|
g.rotate(getOrientation().getRadians(), P50, P50);
|
||||||
y0 = P05;
|
y0 = P05;
|
||||||
y1 = P95 - LABEL_FONT.getSize();
|
y1 = P95 - LABEL_FONT.getSize();
|
||||||
}
|
}
|
||||||
render.textCenter(LABEL_FONT, "%.1fV (max)".formatted(maxVoltage), x, y0, Color.BLACK);
|
drawText(g, LABEL_FONT, "%.1fV (max)".formatted(maxVoltage), x, y0, Color.BLACK, orientation);
|
||||||
if (defect) {
|
if (defect) {
|
||||||
render.textCenter(LABEL_FONT, "DEFEKT", x, y1, Color.RED);
|
drawText(g, LABEL_FONT, "DEFEKT", x, y1, Color.RED, orientation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -13,6 +11,7 @@ import java.awt.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static de.ph87.electro.circuit.CircuitPainter.drawText;
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -30,27 +29,22 @@ public class Poti extends Part {
|
|||||||
|
|
||||||
private double ratio = 0.0;
|
private double ratio = 0.0;
|
||||||
|
|
||||||
public Poti(final Circuit circuit, final Position position) {
|
public Poti(final Point position) {
|
||||||
super(circuit, "Poti", position);
|
super("Poti", position);
|
||||||
common = addJunction("C", P10, P50);
|
common = addJunction("C", P10, P50);
|
||||||
middle = addJunction("M", P50, P10);
|
middle = addJunction("M", P50, P10);
|
||||||
end = addJunction("E", P90, P50);
|
end = addJunction("E", P90, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Poti(final Circuit circuit, final PotiDto dto) {
|
public Poti(final PotiDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
common = addJunction(dto.getCommon().getName(), P10, P50);
|
common = addJunction(dto.getCommon().getName(), P10, P50);
|
||||||
middle = addJunction(dto.getMiddle().getName(), P50, P10);
|
middle = addJunction(dto.getMiddle().getName(), P50, P10);
|
||||||
end = addJunction(dto.getEnd().getName(), P90, P50);
|
end = addJunction(dto.getEnd().getName(), P90, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRatio(final double ratio) {
|
|
||||||
this.ratio = ratio;
|
|
||||||
evaluate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void _action() {
|
public void action() {
|
||||||
if (ratio > 0.95) {
|
if (ratio > 0.95) {
|
||||||
ratio = 0;
|
ratio = 0;
|
||||||
} else {
|
} else {
|
||||||
@ -59,15 +53,14 @@ public class Poti extends Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _labels() {
|
protected void _labels(final Graphics2D g) {
|
||||||
render.textCenter(LABEL_FONT, "%3.0f%%".formatted(ratio * 100), P50, P50, Color.BLACK);
|
drawText(g, LABEL_FONT, "%3.0f%%".formatted(ratio * 100), P50, P50, Color.BLACK, orientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<InnerConnection> getInnerConnections() {
|
public List<InnerConnection> getInnerConnections() {
|
||||||
return List.of(
|
return List.of(
|
||||||
new InnerConnection(common, middle, max(NO_RESISTANCE, resistance * ratio)),
|
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)))
|
new InnerConnection(middle, end, max(NO_RESISTANCE, resistance * (1 - ratio)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,22 +2,20 @@ package de.ph87.electro.circuit.part.parts;
|
|||||||
|
|
||||||
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
|
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
|
||||||
import org.apache.commons.math3.linear.RealMatrix;
|
import org.apache.commons.math3.linear.RealMatrix;
|
||||||
|
import org.apache.commons.math3.linear.RealVector;
|
||||||
|
|
||||||
import static java.lang.Math.cos;
|
import static java.lang.Math.cos;
|
||||||
import static java.lang.Math.sin;
|
import static java.lang.Math.sin;
|
||||||
|
|
||||||
public class RotationMatrix extends Array2DRowRealMatrix {
|
public class RotationMatrix extends Array2DRowRealMatrix {
|
||||||
|
|
||||||
public static RealMatrix ofDegrees(final double degrees) {
|
public static RealVector rotateDegrees(final RealVector finger, final double degrees) {
|
||||||
final double radians = degrees * Math.PI / 180.0;
|
final double radians = degrees * Math.PI / 180.0;
|
||||||
return new RotationMatrix(radians);
|
final RealMatrix matrix = new Array2DRowRealMatrix(new double[][]{
|
||||||
}
|
|
||||||
|
|
||||||
private RotationMatrix(final double radians) {
|
|
||||||
super(new double[][]{
|
|
||||||
{cos(radians), -sin(radians)},
|
{cos(radians), -sin(radians)},
|
||||||
{sin(radians), cos(radians)},
|
{sin(radians), cos(radians)},
|
||||||
});
|
});
|
||||||
|
return matrix.operate(finger);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -12,25 +10,28 @@ import java.awt.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static de.ph87.electro.circuit.CircuitPainter.drawLine;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
public class Switch1x1 extends Part {
|
public class Switch1x1 extends Part {
|
||||||
|
|
||||||
|
private static final Point END = new Point(P90, P25);
|
||||||
|
|
||||||
private final Junction common;
|
private final Junction common;
|
||||||
|
|
||||||
private final Junction output;
|
private final Junction output;
|
||||||
|
|
||||||
private boolean state = false;
|
private boolean state = false;
|
||||||
|
|
||||||
public Switch1x1(final Circuit circuit, final Position position) {
|
public Switch1x1(final Point position) {
|
||||||
super(circuit, "Ausschalter", position);
|
super("Ausschalter", position);
|
||||||
common = addJunction("C", P10, P50);
|
common = addJunction("C", P10, P50);
|
||||||
output = addJunction("O", P90, P50);
|
output = addJunction("O", P90, P50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Switch1x1(final Circuit circuit, final Switch1x1Dto dto) {
|
public Switch1x1(final Switch1x1Dto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
common = addJunction(dto.getCommon(), P10, P50);
|
common = addJunction(dto.getCommon(), P10, P50);
|
||||||
output = addJunction(dto.getOutput(), P90, P50);
|
output = addJunction(dto.getOutput(), P90, P50);
|
||||||
state = dto.isState();
|
state = dto.isState();
|
||||||
@ -38,21 +39,20 @@ public class Switch1x1 extends Part {
|
|||||||
|
|
||||||
public void setState(final boolean state) {
|
public void setState(final boolean state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void _action() {
|
public void action() {
|
||||||
state = !state;
|
state = !state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render() {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
final Point end = getOrientation().rotate(new Point(P90, P25));
|
final Point end = getOrientation().rotate(END);
|
||||||
render.line(common.getPosition(), new Position(end), common.getColor(), SWITCH_STROKE);
|
drawLine(g, common.getAbsolute(), end, common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
render.line(common, output, common.getColor(), SWITCH_STROKE);
|
drawLine(g, common, output, common.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static de.ph87.electro.circuit.CircuitPainter.drawLine;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
@ -24,15 +24,15 @@ public class Switch1x2 extends Part {
|
|||||||
|
|
||||||
private boolean state = false;
|
private boolean state = false;
|
||||||
|
|
||||||
public Switch1x2(final Circuit circuit, final Position position) {
|
public Switch1x2(final Point position) {
|
||||||
super(circuit, "Wechselschalter", position);
|
super("Wechselschalter", position);
|
||||||
common = addJunction("C", P10, P50);
|
common = addJunction("C", P10, P50);
|
||||||
output0 = addJunction("O0", P90, P25);
|
output0 = addJunction("O0", P90, P25);
|
||||||
output1 = addJunction("O1", P90, P75);
|
output1 = addJunction("O1", P90, P75);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Switch1x2(final Circuit circuit, final Switch1x2Dto dto) {
|
public Switch1x2(final Switch1x2Dto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
common = addJunction(dto.getCommon(), P10, P50);
|
common = addJunction(dto.getCommon(), P10, P50);
|
||||||
output0 = addJunction(dto.getOutput0(), P90, P25);
|
output0 = addJunction(dto.getOutput0(), P90, P25);
|
||||||
output1 = addJunction(dto.getOutput1(), P90, P75);
|
output1 = addJunction(dto.getOutput1(), P90, P75);
|
||||||
@ -41,20 +41,19 @@ public class Switch1x2 extends Part {
|
|||||||
|
|
||||||
public void setState(final boolean state) {
|
public void setState(final boolean state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void _action() {
|
public void action() {
|
||||||
state = !state;
|
state = !state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render() {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
render.line(common, output0, common.getColor(), SWITCH_STROKE);
|
drawLine(g, common, output0, common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
render.line(common, output1, common.getColor(), SWITCH_STROKE);
|
drawLine(g, common, output1, common.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static de.ph87.electro.circuit.CircuitPainter.drawLine;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
@ -26,16 +26,16 @@ public class SwitchCross extends Part {
|
|||||||
|
|
||||||
private boolean state = false;
|
private boolean state = false;
|
||||||
|
|
||||||
public SwitchCross(final Circuit circuit, final Position position) {
|
public SwitchCross(final Point position) {
|
||||||
super(circuit, "Kreuzschalter", position);
|
super("Kreuzschalter", position);
|
||||||
common0 = addJunction("C0", P10, P25);
|
common0 = addJunction("C0", P10, P25);
|
||||||
common1 = addJunction("C1", P10, P75);
|
common1 = addJunction("C1", P10, P75);
|
||||||
output0 = addJunction("O0", P90, P25);
|
output0 = addJunction("O0", P90, P25);
|
||||||
output1 = addJunction("O1", P90, P75);
|
output1 = addJunction("O1", P90, P75);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SwitchCross(final Circuit circuit, final SwitchCrossDto dto) {
|
public SwitchCross(final SwitchCrossDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
common0 = addJunction(dto.getCommon0(), P10, P25);
|
common0 = addJunction(dto.getCommon0(), P10, P25);
|
||||||
common1 = addJunction(dto.getCommon1(), P10, P75);
|
common1 = addJunction(dto.getCommon1(), P10, P75);
|
||||||
output0 = addJunction(dto.getOutput0(), P90, P25);
|
output0 = addJunction(dto.getOutput0(), P90, P25);
|
||||||
@ -45,22 +45,21 @@ public class SwitchCross extends Part {
|
|||||||
|
|
||||||
public void setState(final boolean state) {
|
public void setState(final boolean state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void _action() {
|
public void action() {
|
||||||
state = !state;
|
state = !state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _render() {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
render.line(common0, output0, common0.getColor(), SWITCH_STROKE);
|
drawLine(g, common0, output0, common0.getColor(), SWITCH_STROKE);
|
||||||
render.line(common1, output1, common1.getColor(), SWITCH_STROKE);
|
drawLine(g, common1, output1, common1.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
render.line(common0, output1, common0.getColor(), SWITCH_STROKE);
|
drawLine(g, common0, output1, common0.getColor(), SWITCH_STROKE);
|
||||||
render.line(common1, output0, common1.getColor(), SWITCH_STROKE);
|
drawLine(g, common1, output0, common1.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,93 +1,90 @@
|
|||||||
package de.ph87.electro.circuit.part.parts;
|
package de.ph87.electro.circuit.part.parts;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
|
||||||
import de.ph87.electro.circuit.part.InnerConnection;
|
import de.ph87.electro.circuit.part.InnerConnection;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.PartDto;
|
import de.ph87.electro.circuit.part.PartDto;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.apache.commons.math3.linear.ArrayRealVector;
|
import org.apache.commons.math3.linear.ArrayRealVector;
|
||||||
import org.apache.commons.math3.linear.RealMatrix;
|
|
||||||
import org.apache.commons.math3.linear.RealVector;
|
import org.apache.commons.math3.linear.RealVector;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static de.ph87.electro.circuit.part.Position.ABS;
|
import static de.ph87.electro.circuit.CircuitPainter.*;
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class Voltmeter extends Part {
|
public class Voltmeter extends Part {
|
||||||
|
|
||||||
private static final double FINGER_LENGTH = P70;
|
private static final int FINGER_LENGTH = P65;
|
||||||
|
|
||||||
private static final RealVector ANCHOR = new ArrayRealVector(new double[]{P50, P80});
|
private static final RealVector ANCHOR = new ArrayRealVector(new double[]{P50, P75});
|
||||||
|
|
||||||
private static final RealVector FINGER = new ArrayRealVector(new double[]{ANCHOR.getEntry(0) + FINGER_LENGTH, P50}).subtract(ANCHOR);
|
private static final RealVector FINGER = new ArrayRealVector(new double[]{FINGER_LENGTH - P05, 0});
|
||||||
|
|
||||||
private static final Stroke SCALE_STROKE = new BasicStroke(1);
|
private static final Stroke SCALE_STROKE = new BasicStroke(1);
|
||||||
|
|
||||||
private static final double DEGREES_RANGE = 90;
|
private static final double DEGREES_RANGE = 90;
|
||||||
|
|
||||||
public static final double DEGREES_OFFSET = (180 - DEGREES_RANGE) / 2;
|
|
||||||
|
|
||||||
private final Junction a;
|
private final Junction a;
|
||||||
|
|
||||||
private final Junction b;
|
private final Junction b;
|
||||||
|
|
||||||
|
@Setter
|
||||||
private double min = -3;
|
private double min = -3;
|
||||||
|
|
||||||
|
@Setter
|
||||||
private double max = 3;
|
private double max = 3;
|
||||||
|
|
||||||
private double voltage;
|
|
||||||
|
|
||||||
private RealVector tip;
|
private RealVector tip;
|
||||||
|
|
||||||
public Voltmeter(final Circuit circuit, final Position position) {
|
public Voltmeter(final Point position) {
|
||||||
super(circuit, "Voltmeter", position);
|
super("Voltmeter", position);
|
||||||
a = addJunction("A", P10, P90);
|
a = addJunction("A", P10, P90);
|
||||||
b = addJunction("B", P90, P90);
|
b = addJunction("B", P90, P90);
|
||||||
postCalculate(); // TODO remove
|
postCalculate(); // TODO remove
|
||||||
}
|
}
|
||||||
|
|
||||||
public Voltmeter(final Circuit circuit, final PartDto dto) {
|
public Voltmeter(final PartDto dto) {
|
||||||
super(circuit, dto);
|
super(dto);
|
||||||
a = addJunction("A", P10, P90);
|
a = addJunction("A", P10, P90);
|
||||||
b = addJunction("B", P90, P90);
|
b = addJunction("B", P90, P90);
|
||||||
postCalculate(); // TODO remove
|
postCalculate(); // TODO remove
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMin(final double min) {
|
double x = 0;
|
||||||
this.min = min;
|
|
||||||
evaluate();
|
@Override
|
||||||
|
public void _render(final Graphics2D g) {
|
||||||
|
// scale
|
||||||
|
final double DEGREES_OFFSET = 45;
|
||||||
|
drawCircle(g, P(ANCHOR), FINGER_LENGTH, DEGREES_OFFSET, DEGREES_RANGE, Color.BLACK, SCALE_STROKE, Color.white);
|
||||||
|
|
||||||
|
// finger
|
||||||
|
final double ratio = (getVoltage() - min) / (max - min);
|
||||||
|
final double degrees = -(DEGREES_OFFSET + DEGREES_RANGE * (1 - ratio));
|
||||||
|
final RealVector finger = RotationMatrix.rotateDegrees(FINGER, degrees);
|
||||||
|
tip = ANCHOR.add(finger);
|
||||||
|
drawLine(g, P(ANCHOR), P(tip), Color.BLACK, SCALE_STROKE);
|
||||||
|
|
||||||
|
// anchor
|
||||||
|
drawCircle(g, P(ANCHOR), P03, 0, 360, null, null, Color.black);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMax(final double max) {
|
public static Point P(final RealVector anchor) {
|
||||||
this.max = max;
|
return new Point((int) round(anchor.getEntry(0)), (int) round(anchor.getEntry(1)));
|
||||||
evaluate();
|
}
|
||||||
|
|
||||||
|
private double getVoltage() {
|
||||||
|
return !Double.isNaN(b.getVoltage()) && !Double.isNaN(a.getVoltage()) ? b.getVoltage() - a.getVoltage() : 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postCalculate() {
|
protected void _labels(final Graphics2D g) {
|
||||||
voltage = !Double.isNaN(b.getVoltage()) && !Double.isNaN(a.getVoltage()) ? b.getVoltage() - a.getVoltage() : 0.0;
|
drawText(g, LABEL_FONT, "%.2f V".formatted(getVoltage()), P50, P90, Color.BLACK, orientation);
|
||||||
final double ratio = (voltage - min) / (max - min);
|
|
||||||
final double degrees = DEGREES_RANGE * ratio + DEGREES_OFFSET / 2;
|
|
||||||
final RealMatrix rotate = RotationMatrix.ofDegrees(degrees);
|
|
||||||
final RealVector rotatedFinger = rotate.operate(FINGER);
|
|
||||||
tip = ANCHOR.add(rotatedFinger);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void _render() {
|
|
||||||
render.clockwise(getOrientation());
|
|
||||||
render.circle(ABS(ANCHOR), FINGER_LENGTH, Color.BLACK, SCALE_STROKE, Color.white, DEGREES_OFFSET, DEGREES_RANGE);
|
|
||||||
render.line(ABS(ANCHOR), ABS(tip), Color.BLACK, SCALE_STROKE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void _labels() {
|
|
||||||
render.textCenter(LABEL_FONT, "%.2f V".formatted(voltage), P50, P90, Color.BLACK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package de.ph87.electro.circuit.wire;
|
package de.ph87.electro.circuit.wire;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.junction.Junction;
|
import de.ph87.electro.circuit.part.junction.Junction;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
@ -11,8 +10,7 @@ import java.awt.*;
|
|||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.*;
|
||||||
import static java.lang.Math.round;
|
|
||||||
|
|
||||||
@ToString
|
@ToString
|
||||||
public class Wire {
|
public class Wire {
|
||||||
@ -35,8 +33,25 @@ public class Wire {
|
|||||||
b.getWires().add(this);
|
b.getWires().add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean intersects(final Position position) {
|
public boolean intersects(final Point position) {
|
||||||
return position.distanceToLine(a.getPosition(), b.getPosition());
|
return distanceToLine(position, a.getAbsolute(), b.getAbsolute());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean distanceToLine(final Point point, final Point lineStart, final Point lineEnd) {
|
||||||
|
double dx = lineEnd.x - lineStart.x;
|
||||||
|
double dy = lineEnd.y - lineStart.y;
|
||||||
|
double lineLength = dx * dx + dy * dy;
|
||||||
|
if (lineLength == 0) {
|
||||||
|
return sqrt(pow(lineStart.x - point.x, 2) + pow(lineStart.y - point.y, 2)) <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
double t = ((point.x - lineStart.x) * dx + (point.y - lineStart.y) * dy) / lineLength;
|
||||||
|
t = Math.max(0, Math.min(1, t));
|
||||||
|
|
||||||
|
double closestX = lineStart.x + t * dx;
|
||||||
|
double closestY = lineStart.y + t * dy;
|
||||||
|
final double distance = sqrt(pow(closestX - point.x, 2) + pow(closestY - point.y, 2));
|
||||||
|
return distance <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction getOpposite(final Junction junction) {
|
public Junction getOpposite(final Junction junction) {
|
||||||
@ -55,7 +70,7 @@ public class Wire {
|
|||||||
public void draw(final Graphics2D g) {
|
public void draw(final Graphics2D g) {
|
||||||
g.setColor(a.getColor());
|
g.setColor(a.getColor());
|
||||||
g.setStroke(WIRE_STROKE);
|
g.setStroke(WIRE_STROKE);
|
||||||
g.drawLine(a.getPosition().absolute.x, a.getPosition().absolute.y, b.getPosition().absolute.x, b.getPosition().absolute.y);
|
g.drawLine(a.getAbsolute().x, a.getAbsolute().y, b.getAbsolute().x, b.getAbsolute().y);
|
||||||
|
|
||||||
if (SHOW_WIRE_DETAILS) {
|
if (SHOW_WIRE_DETAILS) {
|
||||||
drawValues(g, "%.2f A".formatted(abs(current)), -0.5);
|
drawValues(g, "%.2f A".formatted(abs(current)), -0.5);
|
||||||
@ -66,8 +81,11 @@ public class Wire {
|
|||||||
private void drawValues(final Graphics2D g, final String string, final double offset) {
|
private void drawValues(final Graphics2D g, final String string, final double offset) {
|
||||||
g.setFont(LABEL_FONT);
|
g.setFont(LABEL_FONT);
|
||||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||||
final int mx = (a.getPosition().absolute.x + b.getPosition().absolute.x) / 2;
|
final Point aa = a.getAbsolute();
|
||||||
final int my = (a.getPosition().absolute.y + b.getPosition().absolute.y) / 2;
|
final Point bb = b.getAbsolute();
|
||||||
|
|
||||||
|
final int mx = (aa.x + bb.x) / 2;
|
||||||
|
final int my = (aa.y + bb.y) / 2;
|
||||||
|
|
||||||
final int bx = (int) round((mx - bounds.getWidth() / 2)) - 2;
|
final int bx = (int) round((mx - bounds.getWidth() / 2)) - 2;
|
||||||
final int by = (int) round((my - bounds.getHeight() / 2) + offset * bounds.getHeight()) - 2;
|
final int by = (int) round((my - bounds.getHeight() / 2) + offset * bounds.getHeight()) - 2;
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
package de.ph87.electro.sidebar;
|
package de.ph87.electro.sidebar;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.Position;
|
|
||||||
import de.ph87.electro.circuit.part.parts.*;
|
import de.ph87.electro.circuit.part.parts.*;
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -14,37 +12,26 @@ import static de.ph87.electro.CONFIG.*;
|
|||||||
|
|
||||||
public class Sidebar extends JPanel {
|
public class Sidebar extends JPanel {
|
||||||
|
|
||||||
@Setter
|
public Sidebar(final Runnable newCircuit) {
|
||||||
private Runnable repaintCallback = null;
|
|
||||||
|
|
||||||
public Sidebar(final Runnable newCircuit, final Runnable save) {
|
|
||||||
setPreferredSize(new Dimension(0, 200));
|
setPreferredSize(new Dimension(0, 200));
|
||||||
|
|
||||||
addButton("Neu", null, newCircuit, new Color(128, 202, 255));
|
addButton("Neu", null, newCircuit, new Color(128, 202, 255));
|
||||||
|
|
||||||
addToggle("Details", () -> SHOW_WIRE_DETAILS, v -> {
|
addToggle("Details", () -> SHOW_WIRE_DETAILS, v -> SHOW_WIRE_DETAILS = v);
|
||||||
SHOW_WIRE_DETAILS = v;
|
addToggle("Namen", () -> SHOW_JUNCTION_NAMES, v -> SHOW_JUNCTION_NAMES = v);
|
||||||
save.run();
|
addToggle("Spannungen", () -> SHOW_JUNCTION_VOLTAGES, v -> SHOW_JUNCTION_VOLTAGES = v);
|
||||||
});
|
|
||||||
addToggle("Namen", () -> SHOW_JUNCTION_NAMES, v -> {
|
|
||||||
SHOW_JUNCTION_NAMES = v;
|
|
||||||
save.run();
|
|
||||||
});
|
|
||||||
addToggle("Spannungen", () -> SHOW_JUNCTION_VOLTAGES, v -> {
|
|
||||||
SHOW_JUNCTION_VOLTAGES = v;
|
|
||||||
save.run();
|
|
||||||
});
|
|
||||||
|
|
||||||
addPart(new Battery(null, Position.ZERO));
|
final Point ZERO = new Point();
|
||||||
addPart(new ConnectorCorner(null, Position.ZERO));
|
addPart(new Battery(ZERO));
|
||||||
addPart(new ConnectorEdge(null, Position.ZERO));
|
addPart(new ConnectorCorner(ZERO));
|
||||||
addPart(new ConnectorMiddle(null, Position.ZERO));
|
addPart(new ConnectorEdge(ZERO));
|
||||||
addPart(new Light(null, Position.ZERO));
|
addPart(new ConnectorMiddle(ZERO));
|
||||||
addPart(new Switch1x1(null, Position.ZERO));
|
addPart(new Light(ZERO));
|
||||||
addPart(new Switch1x2(null, Position.ZERO));
|
addPart(new Switch1x1(ZERO));
|
||||||
addPart(new SwitchCross(null, Position.ZERO));
|
addPart(new Switch1x2(ZERO));
|
||||||
addPart(new Poti(null, Position.ZERO));
|
addPart(new SwitchCross(ZERO));
|
||||||
addPart(new Voltmeter(null, Position.ZERO));
|
addPart(new Poti(ZERO));
|
||||||
|
addPart(new Voltmeter(ZERO));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("UnusedReturnValue")
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
@ -56,10 +43,7 @@ public class Sidebar extends JPanel {
|
|||||||
button.setBackground(new Color(255, 128, 128));
|
button.setBackground(new Color(255, 128, 128));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final Runnable click = () -> {
|
final Runnable click = () -> set.accept(!get.get());
|
||||||
set.accept(!get.get());
|
|
||||||
triggerRepaint();
|
|
||||||
};
|
|
||||||
return addButton(label, refresh, click, null);
|
return addButton(label, refresh, click, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,12 +64,6 @@ public class Sidebar extends JPanel {
|
|||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void triggerRepaint() {
|
|
||||||
if (repaintCallback != null) {
|
|
||||||
repaintCallback.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addPart(final Part part) {
|
private void addPart(final Part part) {
|
||||||
final SidebarPart entry = new SidebarPart(part);
|
final SidebarPart entry = new SidebarPart(part);
|
||||||
add(entry);
|
add(entry);
|
||||||
|
|||||||
@ -30,12 +30,11 @@ public class SidebarPart extends JPanel {
|
|||||||
DnDConstants.ACTION_COPY_OR_MOVE,
|
DnDConstants.ACTION_COPY_OR_MOVE,
|
||||||
event -> dragSource.startDrag(event, DragSource.DefaultCopyDrop, new StringSelection(part.getClass().getSimpleName()), null)
|
event -> dragSource.startDrag(event, DragSource.DefaultCopyDrop, new StringSelection(part.getClass().getSimpleName()), null)
|
||||||
);
|
);
|
||||||
part.render();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paint(final Graphics g) {
|
public void paint(final Graphics g) {
|
||||||
part.paint((Graphics2D) g);
|
part.draw((Graphics2D) g);
|
||||||
g.setColor(Color.black);
|
g.setColor(Color.black);
|
||||||
g.drawRect(0, 0, RASTER - 1, RASTER - 1);
|
g.drawRect(0, 0, RASTER - 1, RASTER - 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import de.ph87.electro.circuit.wire.Wire;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
class CalculationServiceTest {
|
class CalculationServiceTest {
|
||||||
@ -16,8 +16,8 @@ class CalculationServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
final Battery battery = circuit.addPart(new Battery(circuit, RST(0, 0)));
|
final Battery battery = circuit.addPart(new Battery(RASTER(0, 0)));
|
||||||
final Light light = circuit.addPart(new Light(circuit, RST(0, 1)));
|
final Light light = circuit.addPart(new Light(RASTER(0, 1)));
|
||||||
circuit.connect(battery.getMinus(), light.getA());
|
circuit.connect(battery.getMinus(), light.getA());
|
||||||
circuit.connect(battery.getPlus(), light.getB());
|
circuit.connect(battery.getPlus(), light.getB());
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,8 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.CircuitIOService.serialize;
|
import static de.ph87.electro.circuit.CircuitIOService.serialize;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class CircuitIOServiceTest {
|
class CircuitIOServiceTest {
|
||||||
@ -22,8 +22,8 @@ class CircuitIOServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void serialization() throws IOException {
|
void serialization() throws IOException {
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
final Battery battery = circuit.addPart(new Battery(circuit, RST(0, 0)));
|
final Battery battery = circuit.addPart(new Battery(RASTER(0, 0)));
|
||||||
final Light light = circuit.addPart(new Light(circuit, RST(0, 1)));
|
final Light light = circuit.addPart(new Light(RASTER(0, 1)));
|
||||||
circuit.connect(battery.getPlus(), light.getB());
|
circuit.connect(battery.getPlus(), light.getB());
|
||||||
circuit.connect(light.getA(), battery.getMinus());
|
circuit.connect(light.getA(), battery.getMinus());
|
||||||
check(circuit);
|
check(circuit);
|
||||||
@ -38,6 +38,9 @@ class CircuitIOServiceTest {
|
|||||||
final ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
|
final ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
|
||||||
final Circuit reloaded = CircuitIOService.load(null, input);
|
final Circuit reloaded = CircuitIOService.load(null, input);
|
||||||
|
|
||||||
|
original.evaluate();
|
||||||
|
reloaded.evaluate();
|
||||||
|
|
||||||
assertEquals(original.getCreated(), reloaded.getCreated());
|
assertEquals(original.getCreated(), reloaded.getCreated());
|
||||||
assertEquals(original.getPartCount(), reloaded.getPartCount());
|
assertEquals(original.getPartCount(), reloaded.getPartCount());
|
||||||
original.streamParts().forEach(originalPart -> {
|
original.streamParts().forEach(originalPart -> {
|
||||||
@ -53,8 +56,7 @@ class CircuitIOServiceTest {
|
|||||||
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
||||||
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
||||||
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
||||||
assertEquals(originalJunction.getRelative(), reloadedJunction.getRelative());
|
assertEquals(originalJunction.getAbsolute(), reloadedJunction.getAbsolute());
|
||||||
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
|
||||||
assertEquals(originalJunction.getWires().size(), reloadedJunction.getWires().size());
|
assertEquals(originalJunction.getWires().size(), reloadedJunction.getWires().size());
|
||||||
originalJunction.getWires().stream()
|
originalJunction.getWires().stream()
|
||||||
.map(originalWire -> originalWire.getOpposite(originalJunction))
|
.map(originalWire -> originalWire.getOpposite(originalJunction))
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import de.ph87.electro.circuit.Circuit;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
public class BatteryLightTest {
|
public class BatteryLightTest {
|
||||||
@ -14,9 +14,9 @@ public class BatteryLightTest {
|
|||||||
|
|
||||||
private static final Circuit CIRCUIT = new Circuit();
|
private static final Circuit CIRCUIT = new Circuit();
|
||||||
|
|
||||||
private static final Battery battery = CIRCUIT.addPart(new Battery(CIRCUIT, RST(0, 0)));
|
private static final Battery battery = CIRCUIT.addPart(new Battery(RASTER(0, 0)));
|
||||||
|
|
||||||
private static final Light light = CIRCUIT.addPart(new Light(CIRCUIT, RST(0, 1)));
|
private static final Light light = CIRCUIT.addPart(new Light(RASTER(0, 1)));
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
@ -26,6 +26,8 @@ public class BatteryLightTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
|
CIRCUIT.evaluate();
|
||||||
|
|
||||||
assertVoltage(VOLTAGE, battery.getPlus().getVoltage());
|
assertVoltage(VOLTAGE, battery.getPlus().getVoltage());
|
||||||
assertVoltage(VOLTAGE, light.getB().getVoltage());
|
assertVoltage(VOLTAGE, light.getB().getVoltage());
|
||||||
assertVoltage(VOLTAGE, light.getPotentialDifference());
|
assertVoltage(VOLTAGE, light.getPotentialDifference());
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import de.ph87.electro.circuit.Circuit;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -15,11 +15,11 @@ public class BatterySwitcher1x1Test {
|
|||||||
|
|
||||||
private static final Circuit CIRCUIT = new Circuit();
|
private static final Circuit CIRCUIT = new Circuit();
|
||||||
|
|
||||||
private static final Battery battery = CIRCUIT.addPart(new Battery(CIRCUIT, RST(0, 0)));
|
private static final Battery battery = CIRCUIT.addPart(new Battery(RASTER(0, 0)));
|
||||||
|
|
||||||
private static final Switch1x1 switcher = CIRCUIT.addPart(new Switch1x1(CIRCUIT, RST(0, 1)));
|
private static final Switch1x1 switcher = CIRCUIT.addPart(new Switch1x1(RASTER(0, 1)));
|
||||||
|
|
||||||
private static final Light light = CIRCUIT.addPart(new Light(CIRCUIT, RST(1, 0)));
|
private static final Light light = CIRCUIT.addPart(new Light(RASTER(1, 0)));
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
@ -42,6 +42,7 @@ public class BatterySwitcher1x1Test {
|
|||||||
final double voltage = state ? VOLTAGE : 0.0;
|
final double voltage = state ? VOLTAGE : 0.0;
|
||||||
|
|
||||||
switcher.setState(state);
|
switcher.setState(state);
|
||||||
|
CIRCUIT.evaluate();
|
||||||
|
|
||||||
assertEquals(state, switcher.isState());
|
assertEquals(state, switcher.isState());
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import de.ph87.electro.circuit.Circuit;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -15,13 +15,13 @@ public class BatterySwitcher1x2Test {
|
|||||||
|
|
||||||
private static final Circuit CIRCUIT = new Circuit();
|
private static final Circuit CIRCUIT = new Circuit();
|
||||||
|
|
||||||
private static final Battery battery = CIRCUIT.addPart(new Battery(CIRCUIT, RST(0, 0)));
|
private static final Battery battery = CIRCUIT.addPart(new Battery(RASTER(0, 0)));
|
||||||
|
|
||||||
private static final Switch1x2 switcher = CIRCUIT.addPart(new Switch1x2(CIRCUIT, RST(0, 2)));
|
private static final Switch1x2 switcher = CIRCUIT.addPart(new Switch1x2(RASTER(0, 2)));
|
||||||
|
|
||||||
private static final Light light0 = CIRCUIT.addPart(new Light(CIRCUIT, RST(1, 1)));
|
private static final Light light0 = CIRCUIT.addPart(new Light(RASTER(1, 1)));
|
||||||
|
|
||||||
private static final Light light1 = CIRCUIT.addPart(new Light(CIRCUIT, RST(1, 3)));
|
private static final Light light1 = CIRCUIT.addPart(new Light(RASTER(1, 3)));
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
@ -47,6 +47,7 @@ public class BatterySwitcher1x2Test {
|
|||||||
final double voltage1 = state ? VOLTAGE : 0.0;
|
final double voltage1 = state ? VOLTAGE : 0.0;
|
||||||
|
|
||||||
switcher.setState(state);
|
switcher.setState(state);
|
||||||
|
CIRCUIT.evaluate();
|
||||||
|
|
||||||
assertEquals(state, switcher.isState());
|
assertEquals(state, switcher.isState());
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import de.ph87.electro.circuit.Circuit;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -15,13 +15,13 @@ public class BatterySwitcher2x2Test {
|
|||||||
|
|
||||||
private static final Circuit CIRCUIT = new Circuit();
|
private static final Circuit CIRCUIT = new Circuit();
|
||||||
|
|
||||||
private static final Battery battery = CIRCUIT.addPart(new Battery(CIRCUIT, RST(0, 0)));
|
private static final Battery battery = CIRCUIT.addPart(new Battery(RASTER(0, 0)));
|
||||||
|
|
||||||
private static final Switch1x2 switcher0 = CIRCUIT.addPart(new Switch1x2(CIRCUIT, RST(0, 1)));
|
private static final Switch1x2 switcher0 = CIRCUIT.addPart(new Switch1x2(RASTER(0, 1)));
|
||||||
|
|
||||||
private static final Switch1x2 switcher1 = CIRCUIT.addPart(new Switch1x2(CIRCUIT, RST(1, 1)));
|
private static final Switch1x2 switcher1 = CIRCUIT.addPart(new Switch1x2(RASTER(1, 1)));
|
||||||
|
|
||||||
private static final Light light = CIRCUIT.addPart(new Light(CIRCUIT, RST(1, 0)));
|
private static final Light light = CIRCUIT.addPart(new Light(RASTER(1, 0)));
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
@ -59,6 +59,7 @@ public class BatterySwitcher2x2Test {
|
|||||||
|
|
||||||
switcher0.setState(state0);
|
switcher0.setState(state0);
|
||||||
switcher1.setState(state1);
|
switcher1.setState(state1);
|
||||||
|
CIRCUIT.evaluate();
|
||||||
|
|
||||||
assertEquals(state0, switcher0.isState());
|
assertEquals(state0, switcher0.isState());
|
||||||
assertEquals(state1, switcher1.isState());
|
assertEquals(state1, switcher1.isState());
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import de.ph87.electro.circuit.Circuit;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
import static de.ph87.electro.circuit.AssertHelper.assertVoltage;
|
||||||
import static de.ph87.electro.circuit.part.Position.RST;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
@ -15,15 +15,15 @@ public class BatterySwitcherCrossTest {
|
|||||||
|
|
||||||
private static final Circuit CIRCUIT = new Circuit();
|
private static final Circuit CIRCUIT = new Circuit();
|
||||||
|
|
||||||
private static final Battery battery = CIRCUIT.addPart(new Battery(CIRCUIT, RST(0, 0)));
|
private static final Battery battery = CIRCUIT.addPart(new Battery(RASTER(0, 0)));
|
||||||
|
|
||||||
private static final Switch1x2 switcher0 = CIRCUIT.addPart(new Switch1x2(CIRCUIT, RST(0, 1)));
|
private static final Switch1x2 switcher0 = CIRCUIT.addPart(new Switch1x2(RASTER(0, 1)));
|
||||||
|
|
||||||
private static final SwitchCross switcherX = CIRCUIT.addPart(new SwitchCross(CIRCUIT, RST(1, 1)));
|
private static final SwitchCross switcherX = CIRCUIT.addPart(new SwitchCross(RASTER(1, 1)));
|
||||||
|
|
||||||
private static final Switch1x2 switcher1 = CIRCUIT.addPart(new Switch1x2(CIRCUIT, RST(2, 1)));
|
private static final Switch1x2 switcher1 = CIRCUIT.addPart(new Switch1x2(RASTER(2, 1)));
|
||||||
|
|
||||||
private static final Light light = CIRCUIT.addPart(new Light(CIRCUIT, RST(2, 0)));
|
private static final Light light = CIRCUIT.addPart(new Light(RASTER(2, 0)));
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
@ -86,6 +86,7 @@ public class BatterySwitcherCrossTest {
|
|||||||
switcher0.setState(state0);
|
switcher0.setState(state0);
|
||||||
switcherX.setState(stateX);
|
switcherX.setState(stateX);
|
||||||
switcher1.setState(state1);
|
switcher1.setState(state1);
|
||||||
|
CIRCUIT.evaluate();
|
||||||
|
|
||||||
assertEquals(state0, switcher0.isState());
|
assertEquals(state0, switcher0.isState());
|
||||||
assertEquals(stateX, switcherX.isState());
|
assertEquals(stateX, switcherX.isState());
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user