GrowWire
modifies GrowPath
to operate within a GridPanel
environment and
construct Wire
objects.
Like GrowPath
the user clicks the mouse to start wire or to add a new point.
Double click to end the wire.
GrowWire.java
import java.io.*; import java.util.ArrayList; import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; public class GrowWire extends GridPanel implements MouseMotionListener { Wire path = null; ArrayList<Wire> wlist = new ArrayList<Wire>(); JLabel status = new JLabel(" "); JButton save = new JButton("Save"); //Point2D loc = null; int nc = 0; Point base, current; boolean in_progress = false; boolean starting = false; GrowWire() { status.setBackground(Color.WHITE); status.setOpaque(true); status.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); save.addActionListener(actionEvent -> showPath("wires.txt")); addMouseMotionListener(this); 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); if (path!=null) path.draw(g); } private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { Point2D src; Point2D grid = new Point2D.Double(); if (in_progress) { if (e.getClickCount()>1) { in_progress = false; wlist.add(path); path = null; } else { src = new Point2D.Double(current.getX(),current.getY()); tf.transform(src,grid); path.add(grid.getX(),-grid.getY()); base = current; repaint(); //System.out.println("LineTo " + x + " " + y); //to = new LineTo(x,y); //path.getElements().add(to); } } else { in_progress = true; starting = true; base = e.getPoint(); current = base; path = new Wire(); src = new Point2D.Double(base.getX(),base.getY()); tf.transform(src,grid); path.add(grid.getX(),-grid.getY()); } } public void mouseReleased(MouseEvent e) { } } public void mouseMoved(MouseEvent event) { if (!in_progress) return; Graphics g = getGraphics(); Graphics2D g2 = (Graphics2D) g; int bx = base.x; int by = base.y; g.setXORMode(Color.WHITE); g2.setStroke(new BasicStroke(2.5f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); // clear old line if (!starting) g.drawLine(bx,by,current.x,current.y); else starting = false; current = event.getPoint(); // xor new line g.drawLine(bx,by,current.x,current.y); g.setPaintMode(); g.setColor(Color.BLACK); } public void mouseDragged(MouseEvent event) { } public void showPath(String filename) { double x, y; double xr, yr; try ( BufferedWriter bw = new BufferedWriter(new FileWriter(filename)); ) { for (Wire w: wlist) { bw.write(w.toString()); bw.newLine(); } bw.close(); } catch (IOException e) { System.err.println("I/O Exception: " + e); } } static public void main(String[] args) { GrowWire panel = new GrowWire(); if (args.length>0) { File file = new File(args[0]); 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); JPanel buttons = new JPanel(); buttons.setBackground(new Color(245,245,245)); buttons.add(panel.save); application.add(buttons,BorderLayout.NORTH); application.setSize(496,480); application.setVisible(true); }); } }
Maintained by John Loomis, updated Sat Feb 22 22:39:58 2020