4 November 2020

Last Time's Files


public class Point 
{
    private final double x;
    private final double y;
    public Point(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
}

import java.util.ArrayList;
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;


public class Curve extends ArrayList<Point>
{
    private Color color;
    private double width;  //width of pen that draws the curve.
    public Curve(Color color, double width)
    {
        this.color = color;
        this.width = width;
    }
    public void draw(GraphicsContext pen)
    {
        pen.setStroke(color);
        pen.setLineWidth(width);
        pen.setLineCap(StrokeLineCap.ROUND);
        pen.setLineJoin(StrokeLineJoin.ROUND);
        pen.beginPath();  //starts the path
        pen.moveTo(get(0).getX(), get(0).getY());
        //there is nothing for the last point to join to.
        for(int k = 0;k < size() - 1 ;k++)
        {
            pen.lineTo(get(k + 1).getX(), get(k + 1).getY());
        }    
        pen.stroke();
    }

}

/**************************************************
*   Author: Morrison
*   Date:  03 Nov 202020
**************************************************/

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import java.util.ArrayList;

public class Draw extends Application
{
    private final Canvas canvas;
    private final GraphicsContext pen;
    private Color bgColor;
    private Color currentColor;
    private ArrayList<Curve> drawing;
    private Curve curve;
    public Draw()
    {
        canvas = new Canvas(800, 600);
        pen = canvas.getGraphicsContext2D();
        bgColor = Color.WHITE;
        currentColor = Color.BLACK;
        drawing = new ArrayList<Curve>();
        curve = null;
    }
    
    @Override
    public void init()
    {
    }

    @Override
    public void start(Stage primary)
    {
        primary.setTitle("Draw Application");
        BorderPane bp = new BorderPane();
        bp.setCenter(canvas);
        bp.setTop(buildMenus());
        canvas.setOnMousePressed( e -> 
        {
            curve = new Curve(currentColor, 30);//TODO: WIDTH
            drawing.add(curve);
            curve.add(new Point(e.getX(), e.getY()));
        });
        canvas.setOnMouseDragged( e ->
        {
            curve.add(new Point(e.getX(), e.getY()));
            curve.draw(pen);
        });
        canvas.setOnMouseReleased( e -> 
        {
            curve.add(new Point(e.getX(), e.getY()));
            curve.draw(pen);
        });
        primary.setScene(new Scene(bp));
        primary.show();
    }

    private MenuBar buildMenus()
    {
        MenuBar mbar = new MenuBar();
        Menu fileMenu = new Menu("File");
        MenuItem quitItem = new MenuItem("Quit");
        fileMenu.setOnAction(e -> Platform.exit());
        fileMenu.getItems().addAll(quitItem);

        Menu colorMenu = new Menu("Color");
        //populate this color menu.
        colorMenu.getItems().addAll(
            new ColorMenuItem(Color.RED, "red"),
            new ColorMenuItem(Color.GREEN, "green"),
            new ColorMenuItem(Color.rgb(0, 0x1A, 0x57), "Dook"),
            new ColorMenuItem(Color.BLUE, "blue")
        );


        Menu bgMenu = new Menu("Background");
        Menu widthMenu = new Menu("Width");
        mbar.getMenus().addAll(fileMenu, colorMenu, bgMenu,
            widthMenu);
        return mbar;
    }
    public void refresh()
    {   
        //draw the background
        pen.setFill(bgColor);
        pen.fillRect(0, 0, getWidth(), getHeight());
        ///redraw curves
        for( Curve c: drawing)
        {
            c.draw(pen);
        }
        //
    }
    @Override
    public void stop()
    {
    }
    class ColorMenuItem extends MenuItem
    {
        private final Color color;
        public ColorMenuItem(Color color, String name)
        {
            super(name);
            this.color = color;
            setOnAction( e -> 
            {
                currentColor = color;
            });
        }

    }
    class BgMenuItem extends MenuItem
    {
        private final Color color;
        public BgMenuItem(Color color, String name)
        {
            super(name);
            this.color = color;
            setOnAction( e -> 
            {
                bgColor = color;
                //refresh();
            });
        }
    }
}