Last Time Our triangles didn't draw at the end of class because we forgot to insert a minus sign. It's shown here so you can fix yours.
public void draw(GraphicsContext pen)
{
//draw myself
pen.setFill(color);
pen.beginPath();
pen.moveTo(xCenter, yCenter - size/Math.sqrt(3));
//note - sign below.
pen.lineTo(xCenter - size/2, yCenter + size/(2*Math.sqrt(3)));
pen.lineTo(xCenter + size/2, yCenter + size/(2*Math.sqrt(3)));
pen.closePath();
pen.fill();
}
Yesterday's program is available in the zip archive linked in the navigation area. It also includes a .jar file, which you can run with this command.
unix> javafx -jar Coordinated.jar
You will want it because we will do some frankencoding from it.
A Draw Program
Get your starter code on the left. Compile and run it right away. Right at the beginning of class, download, compile, and run this. We have a canvas and menus inserted into the scene in this window. Also some basic state has been inserted and it has been initialized in the constructor.
Easy Assignment Create a size menu and allow the user to change the size of the shapes displayed on the screen.
/**************************************************
* 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;
public class Draw extends Application
{
private final Canvas canvas;
private final GraphicsContext pen;
private Color bgColor;
private Color currentColor;
public Draw()
{
canvas = new Canvas(800, 600);
pen = canvas.getGraphicsContext2D();
bgColor = Color.WHITE;
currentColor = Color.BLACK;
}
@Override
public void init()
{
}
@Override
public void start(Stage primary)
{
primary.setTitle("Draw Application");
BorderPane bp = new BorderPane();
bp.setCenter(canvas);
bp.setTop(buildMenus());
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");
Menu bgMenu = new Menu("Background");
Menu widthMenu = new Menu("Width");
mbar.getMenus().addAll(fileMenu, colorMenu, bgMenu,
widthMenu);
return mbar;
}
@Override
public void stop()
{
}
}