package de.ph87.electro.circuit; import de.ph87.electro.circuit.part.Part; import de.ph87.electro.demo.DemoAll; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Optional; import static de.ph87.electro.CONFIG.*; import static java.awt.event.MouseEvent.BUTTON1; import static java.awt.event.MouseEvent.BUTTON3; public class CircuitPanel extends JPanel { private Circuit circuit = new Circuit(); private Part dragPart = null; private Point dragPoint = null; private Point dragOffset = null; public CircuitPanel() { final MouseAdapter listener = new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { final int x = e.getX() / RASTER; final int y = e.getY() / RASTER; final Optional 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.rotate(); 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); addMouseMotionListener(listener); circuit = DemoAll.create(); } @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); 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 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.getParts().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) { for (final Wire wire : circuit.getWires()) { final int x0 = wire.getA().getAbsoluteX(); final int y0 = wire.getA().getAbsoluteY(); final int x1 = wire.getB().getAbsoluteX(); final int y1 = wire.getB().getAbsoluteY(); g.setColor(Color.BLACK); g.setStroke(WIRE_STROKE_BACK); g.drawLine(x0, y0, x1, y1); g.setColor(wire.getA().getColor()); g.setStroke(WIRE_STROKE); g.drawLine(x0, y0, x1, y1); } } }