public void start( Stage primaryStage ) { final double width = 200.0, height = 200.0; double centerX = width/2, centerY=height/2; // center of ellipse and arc double radiusX = width*0.4, radiusY=width*0.4; // radii of ellipse and arc double startAngle = 20.0; // angle to start drawing the arc double length = 360.0 - 2*startAngle; // number of degrees to draw Arc arc = new Arc( centerX, centerY, radiusX, radiusY, startAngle, length ); arc.setFill( Color.YELLOW ); arc.setType( ArcType.ROUND ); Pane pane = new Pane( arc ); Scene scene = new Scene( pane, width, height, Color.BLACK ); primaryStage.setTitle("Pac Man"); primaryStage.setScene( scene ); primaryStage.show(); }
It would be OK to pick different values for startAngle
and length
,
but the mouth should be centered on the X axis.
JavaFX class Polygon
is used for polygons of any number of sides.
Here is a program that draws a triangle.
Of course, a triangle is a polygon with three sides.
import javafx.stage.*; import javafx.scene.Scene; import javafx.scene.shape.*; import javafx.scene.paint.*; import javafx.scene.layout.*; public class TriangleSimple extends Application { public void start( Stage primaryStage ) { double sceneWidth = 400.0, sceneHeight = 300.0; Polygon triangle = new Polygon( 0, 300.0, 400.0, 300.0, 200.0, 0 ); triangle.setFill( new Color( 0.8, 0.2, 0.1, 1.0) ); Pane pane = new Pane( triangle ); Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.FLORALWHITE ); primaryStage.setTitle("Simple Triangle Demo"); primaryStage.setScene( scene ); primaryStage.show(); } }
The constructor for Polygon
looks like this:
public Polygon(double... points)
The parameters are the vertices of the polygon as a comma-separated list, each vertex as two X Y values, with the vertices in clockwise or counter-clockwise order. The number of sides of the polygon is the number of vertices.
Polygon triangle = new Polygon( 0, 300.0, 400.0, 300.0, 200.0, 0 );
It does not matter which vertex you begin with, but the vertices should be in order. For a triangle, a list of three vertices will always be in order, but with more sides than 3 the order matters. The last point on the list will automatically be connected to the first to close the polygon.
Here is the constructor, again.
Polygon triangle = new Polygon( 0, 300.0, 400.0, 300.0, 200.0, 0 );
It is awkward and inflexible to use hard-coded literals like this. Change the constructor so that the X-Y values are based on the dimensions of the scene:
double sceneWidth = 400.0, sceneHeight = 300.0; ... Polygon triangle = new Polygon( 0, , , , , 0);