render + Sidebar DragDrop
This commit is contained in:
parent
f8e64b1280
commit
b19547922c
@ -2,21 +2,25 @@ package de.ph87.electro;
|
|||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
public class CONFIG {
|
public class CONFIG {
|
||||||
|
|
||||||
|
public static final double VOLTAGE = 3.0;
|
||||||
|
|
||||||
public static final int RASTER = 200;
|
public static final int RASTER = 200;
|
||||||
|
|
||||||
public static final double HALF = 0.5;
|
public static final int HALF = (int) round(0.5 * RASTER);
|
||||||
|
|
||||||
public static final double FOURTH1 = 0.25;
|
public static final int FOURTH1 = (int) round(0.25 * RASTER);
|
||||||
|
|
||||||
public static final double FOURTH3 = 0.75;
|
public static final int FOURTH3 = (int) round(0.75 * RASTER);
|
||||||
|
|
||||||
public static final double JUNCTION_LEFT = 0.1;
|
public static final int JUNCTION_LEFT = (int) round(0.1 * RASTER);
|
||||||
|
|
||||||
public static final double JUNCTION_RIGHT = 0.9;
|
public static final int JUNCTION_RIGHT = (int) round(0.9 * RASTER);
|
||||||
|
|
||||||
public static final double JUNCTION_RADIUS = 0.05;
|
public static final int JUNCTION_RADIUS = (int) round(0.05 * RASTER);
|
||||||
|
|
||||||
public static final Color PART_BACKGROUND = new Color(224, 224, 224);
|
public static final Color PART_BACKGROUND = new Color(224, 224, 224);
|
||||||
|
|
||||||
|
|||||||
11
src/main/java/de/ph87/electro/MathHelpers.java
Normal file
11
src/main/java/de/ph87/electro/MathHelpers.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package de.ph87.electro;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class MathHelpers {
|
||||||
|
|
||||||
|
public static Point div(final Point p, final int div) {
|
||||||
|
return new Point(p.x / div, p.y / div);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -19,7 +19,7 @@ public class Window extends JFrame {
|
|||||||
c.weightx = 1;
|
c.weightx = 1;
|
||||||
c.weighty = 1;
|
c.weighty = 1;
|
||||||
c.fill = GridBagConstraints.BOTH;
|
c.fill = GridBagConstraints.BOTH;
|
||||||
add(new Sidebar(part -> circuit.getCircuit().add(part)), c);
|
add(new Sidebar(part -> circuit.getCircuit().getParts().add(part)), c);
|
||||||
|
|
||||||
c = new GridBagConstraints();
|
c = new GridBagConstraints();
|
||||||
c.gridx = 1;
|
c.gridx = 1;
|
||||||
|
|||||||
@ -3,11 +3,13 @@ package de.ph87.electro.circuit;
|
|||||||
import de.ph87.electro.circuit.dto.CircuitDto;
|
import de.ph87.electro.circuit.dto.CircuitDto;
|
||||||
import de.ph87.electro.circuit.dto.PartDto;
|
import de.ph87.electro.circuit.dto.PartDto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.impl.*;
|
import de.ph87.electro.circuit.part.impl.*;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -28,40 +30,33 @@ public class Circuit {
|
|||||||
parts.forEach(part -> part.link(junctions));
|
parts.forEach(part -> part.link(junctions));
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartBattery addBattery(final String name, final int x, final int y, final int rotate, final double voltage) {
|
public PartBattery addBattery(final String name, final int x, final int y, final Orientation orientation, final double voltage) {
|
||||||
final PartBattery battery = new PartBattery(name, x, y, voltage);
|
final PartBattery battery = new PartBattery(name, new Point(x, y), orientation, voltage);
|
||||||
addAndRotate(battery, rotate);
|
parts.add(battery);
|
||||||
return battery;
|
return battery;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addAndRotate(final Part part, final int rotate) {
|
public PartLight addLight(final String name, final int x, final int y, final Orientation orientation, final double maxVoltage) {
|
||||||
for (int i = 0; i < rotate % 4; i++) {
|
final PartLight light = new PartLight(name, new Point(x, y), orientation, maxVoltage);
|
||||||
part.clockwise();
|
parts.add(light);
|
||||||
}
|
|
||||||
add(part);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartLight addLight(final String name, final int x, final int y, final int rotate, final double maxVoltage) {
|
|
||||||
final PartLight light = new PartLight(name, x, y, maxVoltage);
|
|
||||||
addAndRotate(light, rotate);
|
|
||||||
return light;
|
return light;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y, final int rotate, final boolean state) {
|
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
||||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, x, y, state);
|
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, new Point(x, y), orientation, state);
|
||||||
addAndRotate(switch1x1, rotate);
|
parts.add(switch1x1);
|
||||||
return switch1x1;
|
return switch1x1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y, final int rotate, final boolean state) {
|
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
||||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, x, y, state);
|
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, new Point(x, y), orientation, state);
|
||||||
addAndRotate(switch1x2, rotate);
|
parts.add(switch1x2);
|
||||||
return switch1x2;
|
return switch1x2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y, final int rotate, final boolean state) {
|
public PartSwitchCross addSwitchCross(final String name, final int x, final int y, final Orientation orientation, final boolean state) {
|
||||||
final PartSwitchCross switchCross = new PartSwitchCross(name, x, y, state);
|
final PartSwitchCross switchCross = new PartSwitchCross(name, new Point(x, y), orientation, state);
|
||||||
addAndRotate(switchCross, rotate);
|
parts.add(switchCross);
|
||||||
return switchCross;
|
return switchCross;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,14 +65,14 @@ public class Circuit {
|
|||||||
b.getDestinations().add(a);
|
b.getDestinations().add(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(final Part part) {
|
|
||||||
parts.add(part);
|
|
||||||
evaluate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void evaluate() {
|
public void evaluate() {
|
||||||
parts.forEach(Part::reset);
|
parts.forEach(Part::reset);
|
||||||
streamBatteries().forEach(PartBattery::startPropagation);
|
streamBatteries().forEach(PartBattery::startPropagation);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
parts.forEach(Part::render);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Stream<PartBattery> streamBatteries() {
|
private Stream<PartBattery> streamBatteries() {
|
||||||
@ -98,4 +93,9 @@ public class Circuit {
|
|||||||
return wires;
|
return wires;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void add(final Part part) {
|
||||||
|
parts.add(part);
|
||||||
|
evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +1,32 @@
|
|||||||
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.impl.*;
|
||||||
import de.ph87.electro.demo.DemoAll;
|
import de.ph87.electro.demo.DemoAll;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.DataFlavor;
|
||||||
|
import java.awt.datatransfer.Transferable;
|
||||||
|
import java.awt.dnd.DnDConstants;
|
||||||
|
import java.awt.dnd.DropTarget;
|
||||||
|
import java.awt.dnd.DropTargetDropEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
import static de.ph87.electro.MathHelpers.div;
|
||||||
import static java.awt.event.MouseEvent.BUTTON1;
|
import static java.awt.event.MouseEvent.BUTTON1;
|
||||||
import static java.awt.event.MouseEvent.BUTTON3;
|
import static java.awt.event.MouseEvent.BUTTON3;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class CircuitPanel extends JPanel {
|
public class CircuitPanel extends JPanel {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Circuit circuit = new Circuit();
|
private final Circuit circuit = DemoAll.create();
|
||||||
|
|
||||||
private Part dragPart = null;
|
private Part dragPart = null;
|
||||||
|
|
||||||
@ -26,64 +35,43 @@ public class CircuitPanel extends JPanel {
|
|||||||
private Point dragOffset = null;
|
private Point dragOffset = null;
|
||||||
|
|
||||||
public CircuitPanel() {
|
public CircuitPanel() {
|
||||||
final MouseAdapter listener = new MouseAdapter() {
|
final MouseAdapter listener = new MyMouseAdapter();
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseClicked(final MouseEvent e) {
|
|
||||||
final int x = e.getX() / RASTER;
|
|
||||||
final int y = e.getY() / RASTER;
|
|
||||||
final Optional<Part> partOptional = circuit.getParts().stream().filter(p -> p.getX() == x && p.getY() == y).findFirst();
|
|
||||||
switch (e.getButton()) {
|
|
||||||
case BUTTON1:
|
|
||||||
partOptional.ifPresent(part -> {
|
|
||||||
part.click();
|
|
||||||
circuit.evaluate();
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case BUTTON3:
|
|
||||||
partOptional.ifPresent(part -> {
|
|
||||||
part.clockwise();
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mousePressed(final MouseEvent e) {
|
|
||||||
final int x = e.getX() / RASTER;
|
|
||||||
final int y = e.getY() / RASTER;
|
|
||||||
dragOffset = new Point(RASTER / 2, RASTER / 2);
|
|
||||||
dragPart = circuit.getParts().stream().filter(p -> p.getX() == x && p.getY() == y).findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseDragged(final MouseEvent e) {
|
|
||||||
if (dragPart != null) {
|
|
||||||
dragPoint = e.getPoint();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseReleased(final MouseEvent e) {
|
|
||||||
if (dragPart != null) {
|
|
||||||
final int x = e.getX() / RASTER;
|
|
||||||
final int y = e.getY() / RASTER;
|
|
||||||
dragPart.setX(x);
|
|
||||||
dragPart.setY(y);
|
|
||||||
dragPart = null;
|
|
||||||
dragPoint = null;
|
|
||||||
dragOffset = null;
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
addMouseListener(listener);
|
addMouseListener(listener);
|
||||||
addMouseMotionListener(listener);
|
addMouseMotionListener(listener);
|
||||||
circuit = DemoAll.create();
|
setDropTarget(new DropTarget() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drop(final DropTargetDropEvent event) {
|
||||||
|
log.info("drop");
|
||||||
|
try {
|
||||||
|
final Transferable transferable = event.getTransferable();
|
||||||
|
if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
|
||||||
|
event.acceptDrop(DnDConstants.ACTION_COPY);
|
||||||
|
final Point raster = div(event.getLocation(), RASTER);
|
||||||
|
final String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
|
||||||
|
if (data.equals(PartBattery.class.getSimpleName())) {
|
||||||
|
circuit.add(new PartBattery(raster));
|
||||||
|
} else if (data.equals(PartLight.class.getSimpleName())) {
|
||||||
|
circuit.add(new PartLight(raster));
|
||||||
|
} else if (data.equals(PartSwitch1x1.class.getSimpleName())) {
|
||||||
|
circuit.add(new PartSwitch1x1(raster));
|
||||||
|
} else if (data.equals(PartSwitch1x2.class.getSimpleName())) {
|
||||||
|
circuit.add(new PartSwitch1x2(raster));
|
||||||
|
} else if (data.equals(PartSwitchCross.class.getSimpleName())) {
|
||||||
|
circuit.add(new PartSwitchCross(raster));
|
||||||
|
}
|
||||||
|
repaint();
|
||||||
|
event.dropComplete(true);
|
||||||
|
} else {
|
||||||
|
event.rejectDrop();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.toString());
|
||||||
|
event.rejectDrop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -95,10 +83,7 @@ public class CircuitPanel extends JPanel {
|
|||||||
drawParts(g);
|
drawParts(g);
|
||||||
drawRaster(g, w, h);
|
drawRaster(g, w, h);
|
||||||
drawWires(g);
|
drawWires(g);
|
||||||
if (dragPart != null && dragPoint != null) {
|
drawDrag(g);
|
||||||
g.setColor(new Color(192, 192, 192, 128));
|
|
||||||
g.fillRect(dragPoint.x - dragOffset.x, dragPoint.y - dragOffset.y, RASTER, RASTER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawBack(final Graphics2D g, final int w, final int h) {
|
private void drawBack(final Graphics2D g, final int w, final int h) {
|
||||||
@ -123,19 +108,76 @@ public class CircuitPanel extends JPanel {
|
|||||||
|
|
||||||
private void drawWires(final Graphics2D g) {
|
private void drawWires(final Graphics2D g) {
|
||||||
for (final Wire wire : circuit.getWires()) {
|
for (final Wire wire : circuit.getWires()) {
|
||||||
final int x0 = wire.getA().getAbsoluteX();
|
final Point a = wire.getA().getAbsolute();
|
||||||
final int y0 = wire.getA().getAbsoluteY();
|
final Point b = wire.getB().getAbsolute();
|
||||||
final int x1 = wire.getB().getAbsoluteX();
|
|
||||||
final int y1 = wire.getB().getAbsoluteY();
|
|
||||||
|
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.setStroke(WIRE_STROKE_BACK);
|
g.setStroke(WIRE_STROKE_BACK);
|
||||||
g.drawLine(x0, y0, x1, y1);
|
g.drawLine(a.x, a.y, b.x, b.y);
|
||||||
|
|
||||||
g.setColor(wire.getA().getColor());
|
g.setColor(wire.getA().getColor());
|
||||||
g.setStroke(WIRE_STROKE);
|
g.setStroke(WIRE_STROKE);
|
||||||
g.drawLine(x0, y0, x1, y1);
|
g.drawLine(a.x, a.y, b.x, b.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawDrag(final Graphics2D g) {
|
||||||
|
if (dragPart != null && dragPoint != null) {
|
||||||
|
g.setColor(new Color(192, 192, 192, 128));
|
||||||
|
g.fillRect(dragPoint.x - dragOffset.x, dragPoint.y - dragOffset.y, RASTER, RASTER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MyMouseAdapter extends MouseAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(final MouseEvent e) {
|
||||||
|
final Point cell = div(e.getPoint(), RASTER);
|
||||||
|
final Optional<Part> partOptional = circuit.getParts().stream().filter(p -> p.getPosition().equals(cell)).findFirst();
|
||||||
|
switch (e.getButton()) {
|
||||||
|
case BUTTON1:
|
||||||
|
partOptional.ifPresent(part -> {
|
||||||
|
part.click();
|
||||||
|
circuit.evaluate();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case BUTTON3:
|
||||||
|
partOptional.ifPresent(part -> {
|
||||||
|
part.clockwise();
|
||||||
|
circuit.render();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(final MouseEvent e) {
|
||||||
|
final Point cell = div(e.getPoint(), RASTER);
|
||||||
|
dragOffset = new Point(HALF, HALF);
|
||||||
|
dragPart = circuit.getParts().stream().filter(p -> p.getPosition().equals(cell)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(final MouseEvent e) {
|
||||||
|
if (dragPart != null) {
|
||||||
|
dragPoint = e.getPoint();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(final MouseEvent e) {
|
||||||
|
if (dragPart != null) {
|
||||||
|
dragPart.setPosition(div(e.getPoint(), RASTER));
|
||||||
|
dragPart = null;
|
||||||
|
dragPoint = null;
|
||||||
|
dragOffset = null;
|
||||||
|
repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -16,17 +16,11 @@ public class JunctionDto {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private double x;
|
|
||||||
|
|
||||||
private double y;
|
|
||||||
|
|
||||||
private List<String> destinations;
|
private List<String> destinations;
|
||||||
|
|
||||||
public JunctionDto(final Junction junction) {
|
public JunctionDto(final Junction junction) {
|
||||||
this.uuid = junction.getUuid();
|
this.uuid = junction.getUuid();
|
||||||
this.name = junction.getName();
|
this.name = junction.getName();
|
||||||
this.x = junction.getX();
|
|
||||||
this.y = junction.getY();
|
|
||||||
this.destinations = junction.getDestinations().stream().map(Junction::getUuid).toList();
|
this.destinations = junction.getDestinations().stream().map(Junction::getUuid).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import lombok.Getter;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -20,9 +21,7 @@ public class PartDto {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private int x;
|
private Point position;
|
||||||
|
|
||||||
private int y;
|
|
||||||
|
|
||||||
private Orientation orientation;
|
private Orientation orientation;
|
||||||
|
|
||||||
@ -31,8 +30,7 @@ public 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.x = part.getX();
|
this.position = part.getPosition();
|
||||||
this.y = part.getY();
|
|
||||||
this.orientation = part.getOrientation();
|
this.orientation = part.getOrientation();
|
||||||
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
|
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,6 @@ import java.util.Set;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
import static java.lang.Math.round;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString(onlyExplicitlyIncluded = true)
|
@ToString(onlyExplicitlyIncluded = true)
|
||||||
@ -29,9 +28,7 @@ public class Junction {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private double x;
|
private final Point position;
|
||||||
|
|
||||||
private double y;
|
|
||||||
|
|
||||||
private final Set<Junction> destinations = new HashSet<>();
|
private final Set<Junction> destinations = new HashSet<>();
|
||||||
|
|
||||||
@ -47,21 +44,19 @@ public class Junction {
|
|||||||
return destinations.stream().map(Junction::getUuid).toList();
|
return destinations.stream().map(Junction::getUuid).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final String name, final double x, final double y) {
|
public Junction(final Part owner, final String name, final Point position) {
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.x = x;
|
this.position = position;
|
||||||
this.y = y;
|
|
||||||
this._dto = null;
|
this._dto = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Junction(final Part owner, final JunctionDto dto) {
|
public Junction(final Part owner, final JunctionDto dto, final Point position) {
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.x = dto.getX();
|
this.position = position;
|
||||||
this.y = dto.getY();
|
|
||||||
this._dto = dto;
|
this._dto = dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,23 +95,16 @@ public class Junction {
|
|||||||
throw new ShortCircuit();
|
throw new ShortCircuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("SuspiciousNameCombination")
|
public void render(final Graphics2D g) {
|
||||||
public void rotate() {
|
owner.circle(g, position.x, position.y, JUNCTION_RADIUS, Color.BLACK, NORMAL_STROKE, color);
|
||||||
final double oldX = x;
|
|
||||||
x = 1 - y;
|
|
||||||
y = oldX;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(final Graphics2D g) {
|
public Point getAbsolute() {
|
||||||
owner.circle(g, x, y, JUNCTION_RADIUS, Color.BLACK, NORMAL_STROKE, color);
|
return owner.translate(getRotated());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAbsoluteX() {
|
private Point getRotated() {
|
||||||
return (int) round((owner.x + x) * RASTER);
|
return owner.getOrientation().rotate(position);
|
||||||
}
|
|
||||||
|
|
||||||
public int getAbsoluteY() {
|
|
||||||
return (int) round((owner.y + y) * RASTER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,28 +1,29 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
import java.awt.geom.Point2D;
|
import java.awt.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
import static java.lang.Math.PI;
|
import static java.lang.Math.PI;
|
||||||
|
|
||||||
@SuppressWarnings("SuspiciousNameCombination")
|
@SuppressWarnings("SuspiciousNameCombination")
|
||||||
public enum Orientation {
|
public enum Orientation {
|
||||||
R0(0, p -> new Point2D.Double(p.x, p.y)),
|
R0(0, p -> new Point(p.x, p.y)),
|
||||||
R1(1, p -> new Point2D.Double(1 - p.y, p.x)),
|
R1(1, p -> new Point(RASTER - p.y, p.x)),
|
||||||
R2(2, p -> new Point2D.Double(1 - p.x, 1 - p.y)),
|
R2(2, p -> new Point(RASTER - p.x, RASTER - p.y)),
|
||||||
R3(3, p -> new Point2D.Double(p.y, 1 - p.x)),
|
R3(3, p -> new Point(p.y, RASTER - p.x)),
|
||||||
;
|
;
|
||||||
|
|
||||||
private final Function<Point2D.Double, Point2D.Double> map;
|
private final Function<Point, Point> map;
|
||||||
|
|
||||||
private final int deg90;
|
private final int deg90;
|
||||||
|
|
||||||
Orientation(final int deg90, final Function<Point2D.Double, Point2D.Double> map) {
|
Orientation(final int deg90, final Function<Point, Point> rotate) {
|
||||||
this.deg90 = deg90;
|
this.deg90 = deg90;
|
||||||
this.map = map;
|
this.map = rotate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point2D.Double map(final Point2D.Double p) {
|
public Point rotate(final Point p) {
|
||||||
return map.apply(p);
|
return map.apply(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
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 java.lang.Math.round;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -28,26 +27,25 @@ public abstract class Part {
|
|||||||
@ToString.Include
|
@ToString.Include
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
protected int x;
|
protected Point position;
|
||||||
|
|
||||||
protected int y;
|
private Orientation orientation;
|
||||||
|
|
||||||
protected Orientation orientation = Orientation.R0;
|
private final List<Junction> junctions = new ArrayList<>();
|
||||||
|
|
||||||
protected final List<Junction> junctions = new ArrayList<>();
|
private final BufferedImage render = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
|
||||||
protected Part(final String name, final int x, final int y) {
|
protected Part(final String name, final Point position, final Orientation orientation) {
|
||||||
this.uuid = UUID.randomUUID().toString();
|
this.uuid = UUID.randomUUID().toString();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.x = x;
|
this.position = position;
|
||||||
this.y = y;
|
this.orientation = orientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Part(final PartDto dto) {
|
protected Part(final PartDto dto) {
|
||||||
this.uuid = dto.getUuid();
|
this.uuid = dto.getUuid();
|
||||||
this.name = dto.getName();
|
this.name = dto.getName();
|
||||||
this.x = dto.getX();
|
this.position = dto.getPosition();
|
||||||
this.y = dto.getY();
|
|
||||||
this.orientation = dto.getOrientation();
|
this.orientation = dto.getOrientation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,30 +53,52 @@ public abstract class Part {
|
|||||||
junctions.forEach(junction -> junction.link(junctions));
|
junctions.forEach(junction -> junction.link(junctions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Junction newJunction(final Part owner, final String name, final int x, final int y) {
|
||||||
|
return addJunction(new Junction(owner, name, new Point(x, y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Junction newJunction(final Part owner, final JunctionDto dto, final int x, final int y) {
|
||||||
|
return addJunction(new Junction(owner, dto, new Point(x, y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Junction addJunction(final Junction junction) {
|
||||||
|
junctions.add(junction);
|
||||||
|
return junction;
|
||||||
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
junctions.forEach(Junction::reset);
|
junctions.forEach(Junction::reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
final Graphics2D g = (Graphics2D) render.getGraphics();
|
||||||
|
g.rotate(orientation.getTheta(), HALF, HALF);
|
||||||
|
rect(g, 0, 0, RASTER, RASTER, null, null, PART_BACKGROUND);
|
||||||
|
_render(g);
|
||||||
|
junctions.forEach(junction -> junction.render(g));
|
||||||
|
}
|
||||||
|
|
||||||
public void paint(final Graphics2D g) {
|
public void paint(final Graphics2D g) {
|
||||||
rect(g, 0, 0, 1, 1, null, null, PART_BACKGROUND);
|
g.drawImage(render, position.x * RASTER, position.y * RASTER, null);
|
||||||
_paint(g);
|
|
||||||
junctions.forEach(junction -> junction.paint(g));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clockwise() {
|
public void clockwise() {
|
||||||
orientation = orientation.clockwise();
|
orientation = orientation.clockwise();
|
||||||
junctions.forEach(Junction::rotate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void line(final Graphics2D g, final Junction common0, final Junction output0, final Color color, final BasicStroke stroke) {
|
public void line(final Graphics2D g, final Junction junction0, final Junction junction1, final Color color, final BasicStroke stroke) {
|
||||||
line(g, common0.getX(), common0.getY(), output0.getX(), output0.getY(), color, stroke);
|
line(g, junction0.getPosition().x, junction0.getPosition().y, junction1.getPosition().x, junction1.getPosition().y, color, stroke);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void line(final Graphics2D g, final Junction junction0, final double x1, final double y1, final Color color, final BasicStroke stroke) {
|
||||||
|
line(g, junction0.getPosition().x, junction0.getPosition().y, x1, y1, color, stroke);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void line(final Graphics2D g, final double x0, final double y0, final double x1, final double y1, final Color color, final Stroke stroke) {
|
public void line(final Graphics2D g, final double x0, final double y0, final double x1, final double y1, final Color color, final Stroke stroke) {
|
||||||
final int _x0 = (int) round((x + x0) * RASTER);
|
final int _x0 = (int) round(position.x + x0);
|
||||||
final int _y0 = (int) round((y + y0) * RASTER);
|
final int _y0 = (int) round(position.y + y0);
|
||||||
final int _x1 = (int) round((x + x1) * RASTER);
|
final int _x1 = (int) round(position.x + x1);
|
||||||
final int _y1 = (int) round((y + y1) * RASTER);
|
final int _y1 = (int) round(position.y + y1);
|
||||||
if (color != null) {
|
if (color != null) {
|
||||||
g.setColor(color);
|
g.setColor(color);
|
||||||
}
|
}
|
||||||
@ -89,10 +109,10 @@ public abstract class Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void rect(final Graphics2D g, final double x, final double y, final double w, final double h, final Color border, final Stroke stroke, final Color fill) {
|
public void rect(final Graphics2D g, final double x, final double y, final double w, final double h, final Color border, final Stroke stroke, final Color fill) {
|
||||||
final int _x = (int) round((this.x + x) * RASTER);
|
final int _x = (int) round(position.x + x);
|
||||||
final int _y = (int) round((this.y + y) * RASTER);
|
final int _y = (int) round(position.y + y);
|
||||||
final int _w = (int) round(w * RASTER);
|
final int _w = (int) round(w);
|
||||||
final int _h = (int) round(h * RASTER);
|
final int _h = (int) round(h);
|
||||||
if (fill != null) {
|
if (fill != null) {
|
||||||
g.setColor(fill);
|
g.setColor(fill);
|
||||||
g.fillRect(_x, _y, _w, _h);
|
g.fillRect(_x, _y, _w, _h);
|
||||||
@ -104,16 +124,10 @@ public abstract class Part {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void img(final Graphics2D g, final BufferedImage img, final double x, final double y) {
|
|
||||||
final int _x = (int) round((this.x + x) * RASTER);
|
|
||||||
final int _y = (int) round((this.y + y) * RASTER);
|
|
||||||
g.drawImage(img, _x, _y, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void circle(final Graphics2D g, final double x, final double y, final double radius, final Color border, final Stroke stroke, final Color fill) {
|
public void circle(final Graphics2D g, final double x, final double y, final double radius, final Color border, final Stroke stroke, final Color fill) {
|
||||||
final int _x = (int) round((this.x + x - radius) * RASTER);
|
final int _x = (int) round(position.x + x - radius);
|
||||||
final int _y = (int) round((this.y + y - radius) * RASTER);
|
final int _y = (int) round(position.y + y - radius);
|
||||||
final int diameter = (int) round(radius * RASTER * 2);
|
final int diameter = (int) round(radius * 2);
|
||||||
if (fill != null) {
|
if (fill != null) {
|
||||||
g.setColor(fill);
|
g.setColor(fill);
|
||||||
g.fillArc(_x, _y, diameter, diameter, 0, 360);
|
g.fillArc(_x, _y, diameter, diameter, 0, 360);
|
||||||
@ -129,7 +143,9 @@ public abstract class Part {
|
|||||||
|
|
||||||
public abstract void propagate(final Junction source) throws ShortCircuit;
|
public abstract void propagate(final Junction source) throws ShortCircuit;
|
||||||
|
|
||||||
protected abstract void _paint(final Graphics2D g);
|
protected abstract void _render(final Graphics2D g);
|
||||||
|
|
||||||
|
public abstract Part duplicate(final Point position);
|
||||||
|
|
||||||
public static Part of(final PartDto abstractDto) {
|
public static Part of(final PartDto abstractDto) {
|
||||||
return switch (abstractDto) {
|
return switch (abstractDto) {
|
||||||
@ -142,6 +158,11 @@ public abstract class Part {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Part duplicate(final int x, final int y);
|
public Point translate(final Point p) {
|
||||||
|
return new Point(
|
||||||
|
p.x + position.x * RASTER,
|
||||||
|
p.y + position.y * RASTER
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
package de.ph87.electro.circuit.part;
|
package de.ph87.electro.circuit.part;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
import de.ph87.electro.circuit.dto.PartDto;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
public abstract class PartOther extends Part {
|
public abstract class PartOther extends Part {
|
||||||
|
|
||||||
protected PartOther(final String name, final int x, final int y) {
|
protected PartOther(final String name, final Point position, final Orientation orientation) {
|
||||||
super(name, x, y);
|
super(name, position, orientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected PartOther(final PartLightDto dto) {
|
protected PartOther(final PartDto dto) {
|
||||||
super(dto);
|
super(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,13 +3,13 @@ package de.ph87.electro.circuit.part.impl;
|
|||||||
import de.ph87.electro.circuit.ShortCircuit;
|
import de.ph87.electro.circuit.ShortCircuit;
|
||||||
import de.ph87.electro.circuit.dto.PartBatteryDto;
|
import de.ph87.electro.circuit.dto.PartBatteryDto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.*;
|
import static de.ph87.electro.CONFIG.*;
|
||||||
|
|
||||||
@ -17,15 +17,15 @@ import static de.ph87.electro.CONFIG.*;
|
|||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
public class PartBattery extends Part {
|
public class PartBattery extends Part {
|
||||||
|
|
||||||
private static final double MINUS_W = 0.1;
|
private static final double MINUS_W = 0.1 * RASTER;
|
||||||
|
|
||||||
private static final double MINUS_H = 0.3;
|
private static final double MINUS_H = 0.3 * RASTER;
|
||||||
|
|
||||||
private static final double GAP = 0.05;
|
private static final double GAP = 0.05 * RASTER;
|
||||||
|
|
||||||
private static final double PLUS_W = 0.02;
|
private static final double PLUS_W = 0.02 * RASTER;
|
||||||
|
|
||||||
private static final double PLUS_H = 0.6;
|
private static final double PLUS_H = 0.6 * RASTER;
|
||||||
|
|
||||||
private final Junction minus;
|
private final Junction minus;
|
||||||
|
|
||||||
@ -36,21 +36,21 @@ public class PartBattery extends Part {
|
|||||||
|
|
||||||
private ShortCircuit shortCircuit = null;
|
private ShortCircuit shortCircuit = null;
|
||||||
|
|
||||||
public PartBattery(final String name, final int x, final int y, final double initialVoltage) {
|
public PartBattery(final Point position) {
|
||||||
super(name, x, y);
|
this("Batterie", position, Orientation.R0, VOLTAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartBattery(final String name, final Point position, final Orientation orientation, final double initialVoltage) {
|
||||||
|
super(name, position, orientation);
|
||||||
minus = newJunction(this, "-", JUNCTION_LEFT, HALF);
|
minus = newJunction(this, "-", JUNCTION_LEFT, HALF);
|
||||||
plus = newJunction(this, "+", JUNCTION_RIGHT, HALF);
|
plus = newJunction(this, "+", JUNCTION_RIGHT, HALF);
|
||||||
junctions.add(minus);
|
|
||||||
junctions.add(plus);
|
|
||||||
voltage = initialVoltage;
|
voltage = initialVoltage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartBattery(final PartBatteryDto dto) {
|
public PartBattery(final PartBatteryDto dto) {
|
||||||
super(dto);
|
super(dto);
|
||||||
minus = new Junction(this, dto.getMinus());
|
minus = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
|
||||||
plus = new Junction(this, dto.getPlus());
|
plus = newJunction(this, dto.getPlus(), JUNCTION_RIGHT, HALF);
|
||||||
junctions.add(minus);
|
|
||||||
junctions.add(plus);
|
|
||||||
voltage = dto.getVoltage();
|
voltage = dto.getVoltage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,22 +75,16 @@ public class PartBattery extends Part {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
final BufferedImage img = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
|
line(g, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE);
|
||||||
final Graphics2D x = (Graphics2D) img.getGraphics();
|
line(g, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE);
|
||||||
x.translate(RASTER / 2, RASTER / 2);
|
rect(g, HALF - MINUS_W - GAP / 2, HALF - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
|
||||||
x.rotate(orientation.getTheta());
|
rect(g, HALF + GAP / 2, HALF - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
|
||||||
x.translate(-RASTER / 2, -RASTER / 2);
|
|
||||||
line(x, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE);
|
|
||||||
line(x, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE);
|
|
||||||
rect(x, HALF - MINUS_W - GAP / 2, HALF - MINUS_H / 2, MINUS_W, MINUS_H, null, null, Color.BLACK);
|
|
||||||
rect(x, HALF + GAP / 2, HALF - PLUS_H / 2, PLUS_W, PLUS_H, null, null, Color.BLACK);
|
|
||||||
img(g, img, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartBattery duplicate(final int x, final int y) {
|
public PartBattery duplicate(final Point position) {
|
||||||
return new PartBattery(name, x, y, voltage);
|
return new PartBattery(name, position, getOrientation(), voltage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package de.ph87.electro.circuit.part.impl;
|
|||||||
|
|
||||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
import de.ph87.electro.circuit.dto.PartLightDto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -18,17 +19,17 @@ public class PartLight extends PartOther {
|
|||||||
|
|
||||||
private static final Color COLOR_DEFECT = new Color(255, 0, 234);
|
private static final Color COLOR_DEFECT = new Color(255, 0, 234);
|
||||||
|
|
||||||
private static final double MINUS_W = 0.1;
|
private static final int MINUS_W = (int) round(0.1 * RASTER);
|
||||||
|
|
||||||
private static final double MINUS_H = 0.3;
|
private static final int MINUS_H = (int) round(0.3 * RASTER);
|
||||||
|
|
||||||
private static final double GAP = 0.05;
|
private static final int GAP = (int) round(0.05 * RASTER);
|
||||||
|
|
||||||
private static final double PLUS_W = 0.02;
|
private static final int PLUS_W = (int) round(0.02 * RASTER);
|
||||||
|
|
||||||
private static final double PLUS_H = 0.6;
|
private static final int PLUS_H = (int) round(0.6 * RASTER);
|
||||||
|
|
||||||
private static final double BULB_RADIUS = 0.25;
|
private static final int BULB_RADIUS = (int) round(0.25 * RASTER);
|
||||||
|
|
||||||
private final Junction pin0;
|
private final Junction pin0;
|
||||||
|
|
||||||
@ -42,21 +43,21 @@ public class PartLight extends PartOther {
|
|||||||
|
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public PartLight(final String name, final int x, final int y, final double maxVoltage) {
|
public PartLight(final Point position) {
|
||||||
super(name, x, y);
|
this("Licht", position, Orientation.R0, VOLTAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartLight(final String name, final Point position, final Orientation orientation, final double maxVoltage) {
|
||||||
|
super(name, position, orientation);
|
||||||
pin0 = newJunction(this, "", JUNCTION_LEFT, HALF);
|
pin0 = newJunction(this, "", JUNCTION_LEFT, HALF);
|
||||||
pin1 = newJunction(this, "", JUNCTION_RIGHT, HALF);
|
pin1 = newJunction(this, "", JUNCTION_RIGHT, HALF);
|
||||||
junctions.add(pin0);
|
|
||||||
junctions.add(pin1);
|
|
||||||
this.maxVoltage = maxVoltage;
|
this.maxVoltage = maxVoltage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartLight(final PartLightDto dto) {
|
public PartLight(final PartLightDto dto) {
|
||||||
super(dto);
|
super(dto);
|
||||||
pin0 = new Junction(this, dto.getMinus());
|
pin0 = newJunction(this, dto.getMinus(), JUNCTION_LEFT, HALF);
|
||||||
pin1 = new Junction(this, dto.getPlus());
|
pin1 = newJunction(this, dto.getPlus(), JUNCTION_LEFT, HALF);
|
||||||
junctions.add(pin0);
|
|
||||||
junctions.add(pin1);
|
|
||||||
maxVoltage = dto.getMaxVoltage();
|
maxVoltage = dto.getMaxVoltage();
|
||||||
defect = dto.isDefect();
|
defect = dto.isDefect();
|
||||||
}
|
}
|
||||||
@ -80,18 +81,18 @@ public class PartLight extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
line(g, pin0, pin1, Color.BLACK, SYMBOL_STROKE);
|
line(g, pin0, pin1, Color.BLACK, SYMBOL_STROKE);
|
||||||
circle(g, HALF, HALF, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
|
circle(g, HALF, HALF, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color);
|
||||||
|
|
||||||
final double diag = 0.33;
|
final double diag = 0.33 * RASTER;
|
||||||
line(g, diag, diag, 1 - diag, 1 - diag, Color.BLACK, SYMBOL_STROKE);
|
line(g, diag, diag, RASTER - diag, RASTER - diag, Color.BLACK, SYMBOL_STROKE);
|
||||||
line(g, diag, 1 - diag, 1 - diag, diag, Color.BLACK, SYMBOL_STROKE);
|
line(g, diag, RASTER - diag, RASTER - diag, diag, Color.BLACK, SYMBOL_STROKE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartLight duplicate(final int x, final int y) {
|
public PartLight duplicate(final Point position) {
|
||||||
return new PartLight(name, x, y, maxVoltage);
|
return new PartLight(name, position, getOrientation(), maxVoltage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
|
|||||||
import de.ph87.electro.circuit.ShortCircuit;
|
import de.ph87.electro.circuit.ShortCircuit;
|
||||||
import de.ph87.electro.circuit.dto.PartSwitch1x1Dto;
|
import de.ph87.electro.circuit.dto.PartSwitch1x1Dto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -23,21 +24,21 @@ public class PartSwitch1x1 extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitch1x1(final String name, final int x, final int y, final boolean state) {
|
public PartSwitch1x1(final Point position) {
|
||||||
super(name, x, y);
|
this("Ausschalter", position, Orientation.R0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitch1x1(final String name, final Point position, final Orientation orientation, final boolean state) {
|
||||||
|
super(name, position, orientation);
|
||||||
common = newJunction(this, "", JUNCTION_LEFT, HALF);
|
common = newJunction(this, "", JUNCTION_LEFT, HALF);
|
||||||
output = newJunction(this, "", JUNCTION_RIGHT, HALF);
|
output = newJunction(this, "", JUNCTION_RIGHT, HALF);
|
||||||
junctions.add(common);
|
|
||||||
junctions.add(output);
|
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
|
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
|
||||||
super(dto.getName(), dto.getX(), dto.getY());
|
super(dto);
|
||||||
common = new Junction(this, dto.getCommon());
|
common = newJunction(this, dto.getCommon(), JUNCTION_LEFT, HALF);
|
||||||
output = new Junction(this, dto.getOutput());
|
output = newJunction(this, dto.getOutput(), JUNCTION_RIGHT, HALF);
|
||||||
junctions.add(common);
|
|
||||||
junctions.add(output);
|
|
||||||
state = dto.isState();
|
state = dto.isState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,9 +56,9 @@ public class PartSwitch1x1 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common.getX(), common.getY(), JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
|
line(g, common, JUNCTION_RIGHT, FOURTH1, common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
line(g, common, output, common.getColor(), SWITCH_STROKE);
|
line(g, common, output, common.getColor(), SWITCH_STROKE);
|
||||||
}
|
}
|
||||||
@ -69,8 +70,8 @@ public class PartSwitch1x1 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartSwitch1x1 duplicate(final int x, final int y) {
|
public PartSwitch1x1 duplicate(final Point position) {
|
||||||
return new PartSwitch1x1(name, x, y, state);
|
return new PartSwitch1x1(name, position, getOrientation(), state);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
|
|||||||
import de.ph87.electro.circuit.ShortCircuit;
|
import de.ph87.electro.circuit.ShortCircuit;
|
||||||
import de.ph87.electro.circuit.dto.PartSwitch1x2Dto;
|
import de.ph87.electro.circuit.dto.PartSwitch1x2Dto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -25,25 +26,23 @@ public class PartSwitch1x2 extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitch1x2(final String name, final int x, final int y, final boolean state) {
|
public PartSwitch1x2(final Point position) {
|
||||||
super(name, x, y);
|
this("Wechselschalter", position, Orientation.R0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitch1x2(final String name, final Point position, final Orientation orientation, final boolean state) {
|
||||||
|
super(name, position, orientation);
|
||||||
common = newJunction(this, "", JUNCTION_LEFT, HALF);
|
common = newJunction(this, "", JUNCTION_LEFT, HALF);
|
||||||
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
|
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
|
||||||
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
|
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
|
||||||
junctions.add(common);
|
|
||||||
junctions.add(output0);
|
|
||||||
junctions.add(output1);
|
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
|
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
|
||||||
super(dto.getName(), dto.getX(), dto.getY());
|
super(dto);
|
||||||
common = new Junction(this, dto.getCommon());
|
common = newJunction(this, dto.getCommon(), JUNCTION_LEFT, HALF);
|
||||||
output0 = new Junction(this, dto.getOutput0());
|
output0 = newJunction(this, dto.getOutput0(), JUNCTION_RIGHT, FOURTH1);
|
||||||
output1 = new Junction(this, dto.getOutput1());
|
output1 = newJunction(this, dto.getOutput1(), JUNCTION_RIGHT, FOURTH3);
|
||||||
junctions.add(common);
|
|
||||||
junctions.add(output0);
|
|
||||||
junctions.add(output1);
|
|
||||||
state = dto.isState();
|
state = dto.isState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +66,7 @@ public class PartSwitch1x2 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common, output0, common.getColor(), SWITCH_STROKE);
|
line(g, common, output0, common.getColor(), SWITCH_STROKE);
|
||||||
} else {
|
} else {
|
||||||
@ -81,8 +80,8 @@ public class PartSwitch1x2 extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartSwitch1x2 duplicate(final int x, final int y) {
|
public PartSwitch1x2 duplicate(final Point position) {
|
||||||
return new PartSwitch1x2(name, x, y, state);
|
return new PartSwitch1x2(name, position, getOrientation(), state);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.impl;
|
|||||||
import de.ph87.electro.circuit.ShortCircuit;
|
import de.ph87.electro.circuit.ShortCircuit;
|
||||||
import de.ph87.electro.circuit.dto.PartSwitchCrossDto;
|
import de.ph87.electro.circuit.dto.PartSwitchCrossDto;
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.PartOther;
|
import de.ph87.electro.circuit.part.PartOther;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -27,29 +28,25 @@ public class PartSwitchCross extends PartOther {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean state;
|
private boolean state;
|
||||||
|
|
||||||
public PartSwitchCross(final String name, final int x, final int y, final boolean state) {
|
public PartSwitchCross(final Point position) {
|
||||||
super(name, x, y);
|
this("Kreuzschalter", position, Orientation.R0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PartSwitchCross(final String name, final Point position, final Orientation orientation, final boolean state) {
|
||||||
|
super(name, position, orientation);
|
||||||
common0 = newJunction(this, "", JUNCTION_LEFT, FOURTH1);
|
common0 = newJunction(this, "", JUNCTION_LEFT, FOURTH1);
|
||||||
common1 = newJunction(this, "", JUNCTION_LEFT, FOURTH3);
|
common1 = newJunction(this, "", JUNCTION_LEFT, FOURTH3);
|
||||||
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
|
output0 = newJunction(this, "", JUNCTION_RIGHT, FOURTH1);
|
||||||
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
|
output1 = newJunction(this, "", JUNCTION_RIGHT, FOURTH3);
|
||||||
junctions.add(common0);
|
|
||||||
junctions.add(common1);
|
|
||||||
junctions.add(output0);
|
|
||||||
junctions.add(output1);
|
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartSwitchCross(final PartSwitchCrossDto dto) {
|
public PartSwitchCross(final PartSwitchCrossDto dto) {
|
||||||
super(dto.getName(), dto.getX(), dto.getY());
|
super(dto);
|
||||||
common0 = new Junction(this, dto.getCommon0());
|
common0 = newJunction(this, dto.getCommon0(), JUNCTION_LEFT, FOURTH1);
|
||||||
common1 = new Junction(this, dto.getCommon1());
|
common1 = newJunction(this, dto.getCommon1(), JUNCTION_LEFT, FOURTH3);
|
||||||
output0 = new Junction(this, dto.getOutput0());
|
output0 = newJunction(this, dto.getOutput0(), JUNCTION_RIGHT, FOURTH1);
|
||||||
output1 = new Junction(this, dto.getOutput1());
|
output1 = newJunction(this, dto.getOutput1(), JUNCTION_RIGHT, FOURTH3);
|
||||||
junctions.add(common0);
|
|
||||||
junctions.add(common1);
|
|
||||||
junctions.add(output0);
|
|
||||||
junctions.add(output1);
|
|
||||||
state = dto.isState();
|
state = dto.isState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +80,7 @@ public class PartSwitchCross extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void _paint(final Graphics2D g) {
|
protected void _render(final Graphics2D g) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
line(g, common0, output0, common0.getColor(), SWITCH_STROKE);
|
line(g, common0, output0, common0.getColor(), SWITCH_STROKE);
|
||||||
line(g, common1, output1, common1.getColor(), SWITCH_STROKE);
|
line(g, common1, output1, common1.getColor(), SWITCH_STROKE);
|
||||||
@ -99,8 +96,8 @@ public class PartSwitchCross extends PartOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartSwitchCross duplicate(final int x, final int y) {
|
public PartSwitchCross duplicate(final Point position) {
|
||||||
return new PartSwitchCross(name, x, y, state);
|
return new PartSwitchCross(name, position, getOrientation(), state);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,22 @@
|
|||||||
package de.ph87.electro.demo;
|
package de.ph87.electro.demo;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.Circuit;
|
import de.ph87.electro.circuit.Circuit;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.*;
|
import de.ph87.electro.circuit.part.impl.*;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.VOLTAGE;
|
||||||
|
|
||||||
public class DemoAll {
|
public class DemoAll {
|
||||||
|
|
||||||
public static Circuit create() {
|
public static Circuit create() {
|
||||||
final double voltage = 3.0;
|
|
||||||
|
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 2, voltage);
|
final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R2, VOLTAGE);
|
||||||
final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ausschalter", 2, 0, 0, false);
|
final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ausschalter", 2, 0, Orientation.R0, false);
|
||||||
final PartLight light = circuit.addLight("Licht", 4, 0, 0, voltage);
|
final PartLight light = circuit.addLight("Licht", 4, 0, Orientation.R0, VOLTAGE);
|
||||||
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 2, 0, false);
|
final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 2, Orientation.R0, false);
|
||||||
final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 2, 2, 0, false);
|
final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 2, 2, Orientation.R0, false);
|
||||||
final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 4, 2, 2, true);
|
final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 4, 2, Orientation.R2, true);
|
||||||
|
|
||||||
circuit.connect(battery.getMinus(), switcher.getCommon());
|
circuit.connect(battery.getMinus(), switcher.getCommon());
|
||||||
circuit.connect(switcher.getOutput(), light.getPin0());
|
circuit.connect(switcher.getOutput(), light.getPin0());
|
||||||
|
|||||||
@ -1,43 +1,32 @@
|
|||||||
package de.ph87.electro.sidebar;
|
package de.ph87.electro.sidebar;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.impl.*;
|
import de.ph87.electro.circuit.part.impl.*;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import static de.ph87.electro.CONFIG.VOLTAGE;
|
||||||
|
|
||||||
public class Sidebar extends JPanel {
|
public class Sidebar extends JPanel {
|
||||||
|
|
||||||
private final Consumer<Part> onAdd;
|
private final Consumer<Part> onAdd;
|
||||||
|
|
||||||
private final List<SidebarPart> parts = new ArrayList<>();
|
|
||||||
|
|
||||||
public Sidebar(final Consumer<Part> onAdd) {
|
public Sidebar(final Consumer<Part> onAdd) {
|
||||||
this.onAdd = onAdd;
|
this.onAdd = onAdd;
|
||||||
setLayout(new GridBagLayout());
|
setLayout(new FlowLayout());
|
||||||
add(new PartBattery("Batterie", 0, 0, 3.0));
|
add(new PartBattery("Batterie", new Point(0, 0), Orientation.R0, VOLTAGE));
|
||||||
add(new PartLight("Licht", 1, 0, 3.0));
|
add(new PartLight("Licht", new Point(0, 0), Orientation.R0, VOLTAGE));
|
||||||
add(new PartSwitch1x1("Ausschalter", 0, 1, false));
|
add(new PartSwitch1x1("Ausschalter", new Point(0, 0), Orientation.R0, false));
|
||||||
add(new PartSwitch1x2("Wechselschalter", 1, 1, false));
|
add(new PartSwitch1x2("Wechselschalter", new Point(0, 0), Orientation.R0, false));
|
||||||
add(new PartSwitchCross("Kreuzschalter", 0, 2, false));
|
add(new PartSwitchCross("Kreuzschalter", new Point(0, 0), Orientation.R0, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void add(final Part part) {
|
private void add(final Part part) {
|
||||||
final SidebarPart entry = new SidebarPart(part, onAdd);
|
final SidebarPart entry = new SidebarPart(part, onAdd);
|
||||||
parts.add(entry);
|
|
||||||
add(entry);
|
add(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void paint(final Graphics g) {
|
|
||||||
g.setColor(Color.gray);
|
|
||||||
g.fillRect(0, 0, getWidth(), getHeight());
|
|
||||||
for (final SidebarPart part : parts) {
|
|
||||||
part.getPart().paint((Graphics2D) g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,15 +2,18 @@ package de.ph87.electro.sidebar;
|
|||||||
|
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.datatransfer.StringSelection;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.dnd.DnDConstants;
|
||||||
|
import java.awt.dnd.DragSource;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static de.ph87.electro.CONFIG.RASTER;
|
import static de.ph87.electro.CONFIG.RASTER;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Getter
|
@Getter
|
||||||
public class SidebarPart extends JPanel {
|
public class SidebarPart extends JPanel {
|
||||||
|
|
||||||
@ -21,15 +24,26 @@ public class SidebarPart extends JPanel {
|
|||||||
public SidebarPart(final Part part, final Consumer<Part> add) {
|
public SidebarPart(final Part part, final Consumer<Part> add) {
|
||||||
this.part = part;
|
this.part = part;
|
||||||
this.add = add;
|
this.add = add;
|
||||||
|
setTransferHandler(new TransferHandler("text"));
|
||||||
setPreferredSize(new Dimension(RASTER, RASTER));
|
setPreferredSize(new Dimension(RASTER, RASTER));
|
||||||
addMouseListener(new MouseAdapter() {
|
createDragSource(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createDragSource(final Part part) {
|
||||||
|
final DragSource dragSource = new DragSource();
|
||||||
|
dragSource.createDefaultDragGestureRecognizer(
|
||||||
|
this,
|
||||||
|
DnDConstants.ACTION_COPY_OR_MOVE,
|
||||||
|
event -> dragSource.startDrag(event, DragSource.DefaultCopyDrop, new StringSelection(part.getClass().getSimpleName()), null)
|
||||||
|
);
|
||||||
|
part.render();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(final MouseEvent e) {
|
public void paint(final Graphics g) {
|
||||||
add.accept(part.duplicate(e.getPoint().x / RASTER, e.getPoint().y / RASTER));
|
part.paint((Graphics2D) g);
|
||||||
}
|
g.setColor(Color.black);
|
||||||
|
g.drawRect(0, 0, RASTER - 1, RASTER - 1);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
@ -14,9 +15,9 @@ public class BatteryLightTest {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
|
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x1;
|
import de.ph87.electro.circuit.part.impl.PartSwitch1x1;
|
||||||
@ -15,11 +16,11 @@ public class BatterySwitcher1x1Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, 0, false);
|
private static final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 0, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 1, 0, VOLTAGE);
|
private static final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||||
@ -15,13 +16,13 @@ public class BatterySwitcher1x2Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 0, VOLTAGE);
|
private static final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, 0, false);
|
private static final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 0, 2, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, 0, VOLTAGE);
|
private static final PartLight light0 = circuit.addLight("Licht 0", 1, 1, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, 0, VOLTAGE);
|
private static final PartLight light1 = circuit.addLight("Licht 1", 1, 3, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||||
@ -15,13 +16,13 @@ public class BatterySwitcher2x2Test {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
|
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
|
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, 0, false);
|
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 1, 0, 0, VOLTAGE);
|
private static final PartLight light = circuit.addLight("Licht", 1, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
import de.ph87.electro.circuit.part.impl.PartSwitch1x2;
|
||||||
@ -16,15 +17,15 @@ public class BatterySwitcherCrossTest {
|
|||||||
|
|
||||||
private static final Circuit circuit = new Circuit();
|
private static final Circuit circuit = new Circuit();
|
||||||
|
|
||||||
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, 0, VOLTAGE);
|
private static final PartBattery battery = circuit.addBattery("Batterie", 0, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, 0, false);
|
private static final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 0, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, 0, false);
|
private static final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, 0, false);
|
private static final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 2, 1, Orientation.R0, false);
|
||||||
|
|
||||||
private static final PartLight light = circuit.addLight("Licht", 2, 0, 0, VOLTAGE);
|
private static final PartLight light = circuit.addLight("Licht", 2, 0, Orientation.R0, VOLTAGE);
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package de.ph87.electro.circuit;
|
package de.ph87.electro.circuit;
|
||||||
|
|
||||||
import de.ph87.electro.circuit.part.Junction;
|
import de.ph87.electro.circuit.part.Junction;
|
||||||
|
import de.ph87.electro.circuit.part.Orientation;
|
||||||
import de.ph87.electro.circuit.part.Part;
|
import de.ph87.electro.circuit.part.Part;
|
||||||
import de.ph87.electro.circuit.part.impl.PartBattery;
|
import de.ph87.electro.circuit.part.impl.PartBattery;
|
||||||
import de.ph87.electro.circuit.part.impl.PartLight;
|
import de.ph87.electro.circuit.part.impl.PartLight;
|
||||||
@ -11,6 +12,7 @@ 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.VOLTAGE;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class CircuitServiceTest {
|
class CircuitServiceTest {
|
||||||
@ -18,8 +20,8 @@ class CircuitServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void serialization() throws IOException {
|
void serialization() throws IOException {
|
||||||
final Circuit circuit = new Circuit();
|
final Circuit circuit = new Circuit();
|
||||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 1, 3.0);
|
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, Orientation.R1, VOLTAGE);
|
||||||
final PartLight light = circuit.addLight("Licht", 1, 1, 0, 3.0);
|
final PartLight light = circuit.addLight("Licht", 1, 1, Orientation.R0, VOLTAGE);
|
||||||
circuit.connect(battery.getPlus(), light.getPin1());
|
circuit.connect(battery.getPlus(), light.getPin1());
|
||||||
circuit.connect(light.getPin0(), battery.getMinus());
|
circuit.connect(light.getPin0(), battery.getMinus());
|
||||||
check(circuit);
|
check(circuit);
|
||||||
@ -42,8 +44,7 @@ class CircuitServiceTest {
|
|||||||
final Part reloadedPart = reloaded.getParts().stream().filter(part -> part.getUuid().equals(originalPart.getUuid())).findFirst().orElseThrow();
|
final Part reloadedPart = reloaded.getParts().stream().filter(part -> part.getUuid().equals(originalPart.getUuid())).findFirst().orElseThrow();
|
||||||
assertEquals(originalPart.getUuid(), reloadedPart.getUuid());
|
assertEquals(originalPart.getUuid(), reloadedPart.getUuid());
|
||||||
assertEquals(originalPart.getName(), reloadedPart.getName());
|
assertEquals(originalPart.getName(), reloadedPart.getName());
|
||||||
assertEquals(originalPart.getX(), reloadedPart.getX());
|
assertEquals(originalPart.getPosition(), reloadedPart.getPosition());
|
||||||
assertEquals(originalPart.getY(), reloadedPart.getY());
|
|
||||||
assertEquals(originalPart.getOrientation(), reloadedPart.getOrientation());
|
assertEquals(originalPart.getOrientation(), reloadedPart.getOrientation());
|
||||||
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
|
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
|
||||||
for (final Junction originalJunction : originalPart.getJunctions()) {
|
for (final Junction originalJunction : originalPart.getJunctions()) {
|
||||||
@ -51,8 +52,7 @@ class CircuitServiceTest {
|
|||||||
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
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.getX(), reloadedJunction.getX());
|
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
||||||
assertEquals(originalJunction.getY(), reloadedJunction.getY());
|
|
||||||
assertEquals(originalJunction.getDestinations().size(), reloadedJunction.getDestinations().size());
|
assertEquals(originalJunction.getDestinations().size(), reloadedJunction.getDestinations().size());
|
||||||
for (final Junction originalDestination : originalJunction.getDestinations()) {
|
for (final Junction originalDestination : originalJunction.getDestinations()) {
|
||||||
System.out.printf(" - Destination: %s\n", originalDestination.getUuid());
|
System.out.printf(" - Destination: %s\n", originalDestination.getUuid());
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user