Tino APCS

Graphics in Java

Origin

In JavaFX, the origin is the upper left corner of the Scene with positive-x going to the right and positive-y going down.

Note that this coordinate system also means that rotations will be clockwise for positive angles and counter clockwise for negative angles.

Drawing Shapes

  • You can add subclasses of the Shape class to your root node.

  • Useful Shape methods include:

    • setFill(Paint value)

      • If you click on the Paint API, you'll notice that Paint is an abstract class and therefore you must use one of its subclasses to use a particular kind of Paint.
      • Color is a subclass of Paint and is specific to JavaFX
      • Do not use the java.awt.Color class with JavaFX. It's not the same as JavaFX's Color class.

    • setStroke(Paint value)

      • This sets the border color of the shape

  • Shape objects also inherit many, many methods from Node. See the API for a particular Shape such as Circle or Rectangle.

  • Each Shape subclass has a different way of setting position and other measurements. For example, to place a circle, you would use setCenterX and setCenterY. See the API for the shape you are adding.

  • Placing Shapes in a Group node will respect positions, but Region nodes may use layout rules that ignore them. If your goal is to draw a freeform picture of Shapes, you should place your shapes in a Group node.

  • Sample code is given on the Java FX HW 1 lab page.

Dark Mode

Outline