GrowPath.java

Click mouse to start path or to add a new point. Double click to add last point. The save button writes the path file to growpath.txt


GrowPath.java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
import java.lang.Math;
import java.util.ArrayList;
import java.io.*;

public class GrowPath extends JPanel implements MouseMotionListener, ActionListener {
    GeneralPath path = null;
    //LineTo to = null;
    boolean in_progress = false;
    boolean starting = false;
    Color colors[] = {Color.BLACK, Color.BLUE, Color.YELLOW, Color.RED, Color.CYAN, Color.MAGENTA};
    int nc = 0;
    ArrayList<GeneralPath> list = new ArrayList<GeneralPath>();
    Point base, current;
    JButton save = new JButton("Save");

    GrowPath() {
	save.addActionListener(this);
	addMouseListener(new MouseHandler());
	addMouseMotionListener(this);

   }

   public void paintComponent(Graphics g)
    {
	super.paintComponent(g);

	Graphics2D g2 = (Graphics2D) g;
	g2.setStroke(new BasicStroke(2.5f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
	for (GeneralPath p: list) g2.draw(p);
	if (path!=null) g2.draw(path);
    }



   private class MouseHandler extends MouseAdapter
    {

	public void mousePressed(MouseEvent e)
	{
	    if (in_progress) {
		if (e.getClickCount()>1) {
			in_progress = false;
			list.add(path);
			path = null;
		}
		else {
		     path.lineTo(current.getX(),current.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 GeneralPath();
		path.moveTo(base.getX(),base.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);
	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 actionPerformed(ActionEvent e) {
	showPath("growpath.txt");
    }


    public void showPath(String filename) {
	double x, y;
	double xr, yr;
        try (
         BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
        ) {
	 for (GeneralPath p: list) {
		bw.write("W ");
		PathIterator iter = p.getPathIterator(null);
		while (!iter.isDone()) {
			double [] coords = new double[6];
			int type = iter.currentSegment(coords);
			if (type ==  PathIterator.SEG_LINETO) bw.write(" L ");
			else if (type ==  PathIterator.SEG_MOVETO) bw.write(" M ");
			else bw.write(" Other ");
			bw.write(" " + coords[0]+ " " + coords[1]);
			iter.next();
		}
		bw.newLine();
	 }
	 bw.close();
      } catch (IOException e) {
         System.err.println("I/O Exception: " + e);
      }

    }

   public static void main(String[] args)
   {
       EventQueue.invokeLater(new Runnable() {
	   public void run()
	   {
	       GrowPath panel = new GrowPath();
	       JFrame frame = new JFrame("Grow Path");
	       frame.add(panel);
	       JPanel buttons = new JPanel();
	       buttons.add(panel.save);
	       frame.add(buttons,BorderLayout.NORTH);
	       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       frame.setSize(400,400);
	       frame.setVisible(true);
	   }
       });
   }

}


Maintained by John Loomis, updated Sun Mar 01 13:18:53 2020