65 lines
1.5 KiB
Java
65 lines
1.5 KiB
Java
package de.ph87.electro.circuit;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
import static de.ph87.electro.CONFIG.*;
|
|
|
|
@Slf4j
|
|
public class CircuitPanel extends JPanel {
|
|
|
|
private final Circuit circuit = new Circuit();
|
|
|
|
private final CircuitPanelMouseAdapter mouseAdapter = new CircuitPanelMouseAdapter(this, circuit);
|
|
|
|
public CircuitPanel() {
|
|
new CircuitPanelDropTarget(this, circuit);
|
|
}
|
|
|
|
@Override
|
|
public void paint(final Graphics _g) {
|
|
final Graphics2D g = (Graphics2D) _g;
|
|
final int w = getWidth();
|
|
final int h = getHeight();
|
|
drawBack(g, w, h);
|
|
drawParts(g);
|
|
drawRaster(g, w, h);
|
|
drawWires(g);
|
|
mouseAdapter.drawDrag(g);
|
|
}
|
|
|
|
private void drawBack(final Graphics2D g, final int w, final int h) {
|
|
g.setColor(Color.white);
|
|
g.fillRect(0, 0, w, h);
|
|
}
|
|
|
|
private void drawParts(final Graphics2D g) {
|
|
circuit.streamParts().forEach(part -> part.paint(g));
|
|
}
|
|
|
|
private void drawRaster(final Graphics2D g, final int w, final int h) {
|
|
g.setStroke(RASTER_STROKE);
|
|
g.setColor(RASTER_COLOR);
|
|
for (int x = 0; x < w; x += RASTER) {
|
|
g.drawLine(x, 0, x, h);
|
|
}
|
|
for (int y = 0; y < h; y += RASTER) {
|
|
g.drawLine(0, y, w, y);
|
|
}
|
|
}
|
|
|
|
private void drawWires(final Graphics2D g) {
|
|
circuit.streamWires().forEach(wire -> {
|
|
final Point a = wire.getA().getAbsolute();
|
|
final Point b = wire.getB().getAbsolute();
|
|
|
|
g.setColor(wire.getA().getColor());
|
|
g.setStroke(WIRE_STROKE);
|
|
g.drawLine(a.x, a.y, b.x, b.y);
|
|
});
|
|
}
|
|
|
|
}
|