import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; public class MyCanvas extends Canvas { private double width, height; // Canvas has them, as private private GraphicsContext gc; // Canvas has it, as private private ArrayList strokes; public MyCanvas(double w, double h) { super(w, h); width = w; height = h; gc = super.getGraphicsContext2D(); strokes = new ArrayList<>(); } public void addStroke(Stroke s) { strokes.add(s); // must repaint paint(); } public void clearCanvas() { strokes.clear(); paint(); } public void paint() { // clear canvas gc.clearRect(0, 0, width, height); // draw all strokes in the arraylist for (Stroke s : strokes) { s.draw(gc); } } // allows user to save current drawing (canvas) into a text file // format: // #_of_strokes // C cx cy size filled r g b // S cx cy size filled r g b public void saveTextFile(File fh) { try { PrintWriter fileOut = new PrintWriter(fh); fileOut.println(strokes.size()); for (Stroke s : strokes) { fileOut.println(s.toString()); } fileOut.close(); } catch(IOException e) { System.err.println("Error writing to " + fh.getName()); e.printStackTrace(); } } }