Pressing the mouse outside of wire bounding boxes produces results as follows
Pressing the mouse inside wire bounding boxes produces results shown below.
Pressing the mouse near the wire shows the point of intersection.
Finally the figure below shows the bounding boxes for wires from an actual circuit.
WireDemo
extends GridPanel
and uses Wire
objects.
Download source from src.zip
WireDemo.java
import java.io.*; import java.util.ArrayList; import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; public class WireDemo extends GridPanel { ArrayList<Wire> wlist = new ArrayList<Wire>(); JLabel status = new JLabel(" "); Point2D loc = null; Point2D pt = null; WireDemo() { status.setBackground(Color.WHITE); status.setOpaque(true); status.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); addMouseListener(new MouseHandler()); } public void read(File file) { String delims = "[\\s]+"; try (BufferedReader br = new BufferedReader(new FileReader(file));) { String CurrentLine; while ((CurrentLine = br.readLine()) != null) { // debug: System.out.println(CurrentLine); //Split each line of text file String[] tokens = (CurrentLine.trim().split(delims)); if (tokens[0].equals("W")) { Wire w = new Wire(tokens); wlist.add(w); } } br.close(); } catch (IOException e) { JOptionPane.showMessageDialog(null, e); System.err.println(e); } } public void drawContent(Graphics g) { for (Wire w: wlist) w.draw(g); Graphics2D g2 = (Graphics2D) g; if (loc!=null) { g.setColor(Color.BLUE); Ellipse2D circ = new Ellipse2D.Double(loc.getX()-0.05,-loc.getY()-0.05,0.1,0.1); g2.fill(circ); } else if (pt!=null) { g.setColor(Color.BLACK); Ellipse2D circ = new Ellipse2D.Double(pt.getX()-0.05,-pt.getY()-0.05,0.1,0.1); g2.draw(circ); } } private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { Point2D src = new Point2D.Double(e.getX(),e.getY()); Point2D grid = new Point2D.Double(); Point2D dst=null; tf.transform(src,grid); pt = grid; grid.setLocation(grid.getX(),-grid.getY()); String str = String.format("grid %.2g %.2g",grid.getX(), grid.getY()); for (Wire w: wlist) { dst = w.intersects(grid); if (dst!=null) { str = String.format("intersects wire: [%.2g %.2g]", dst.getX(),dst.getY()); break; } if (w.contains(grid)) { str = "contains wire "; break; } } loc = dst; status.setText(str); repaint(); } } static public void main(String[] args) { String filename = args.length>0? args[0]: "wire.txt"; File file = new File(filename); WireDemo panel = new WireDemo(); panel.read(file); //for (Wire w: app.wlist) System.out.println(w); EventQueue.invokeLater(() -> { JFrame application = new JFrame("WireDemo"); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.add(panel.status,BorderLayout.SOUTH); application.setSize(496,400); application.setVisible(true); }); } }
Maintained by John Loomis, updated Sun Mar 08 11:55:13 2020