JavaFX Canvas getHeight()
The method getHeight() from Canvas is declared as:
public final double getHeight()
Return
The method getHeight() returns
The following code shows how to use JavaFX Canvas getHeight()
Example 1
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; public class Diagram { private double[] values; public final static String[] names = { "????", "??????", "????", "??????" }; public Diagram(double[] values) { this.values = values; }// w w w . d e m o 2s . co m public void draw(Canvas canvas) { if (values == null || values.length == 0) { return; } GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.GREEN); gc.fillOval(50, 50, canvas.getWidth() - 100, canvas.getHeight() - 100); } }
Example 2
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; /**// w w w . d e m o 2 s . c o m * Created by Hannes on 18/02/2017. */ public class Bird extends Mover { public Bird(float x, float y, float m) { super(x, y, m); bouncyness = 0; } public boolean checkFallen(Canvas canvas) { if (pos.y > canvas.getHeight()) return true; return false; } @Override public void newFrame(GraphicsContext gc, Canvas canvas) { FlappyBird.gameOver = checkFallen(canvas); super.newFrame(gc, canvas); } }
Example 3
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import java.util.ArrayList; import java.util.List; public class Simulation { private final List<SimulationObject> simulationObjects = new ArrayList<>(); public void draw(Canvas canvas) { GraphicsContext context = canvas.getGraphicsContext2D(); context.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); for (SimulationObject simulationObject : simulationObjects) { simulationObject.draw(context); }// w w w . d e m o 2 s . c o m } public void add(SimulationObject simulationObject) { simulationObjects.add(simulationObject); } public void step() { for (SimulationObject obj : simulationObjects) { obj.step(); } } }