CANVAS
Canvas

Beschreibung Download
Dialogfenster mit einem Canvas-Element UIBspCanvas01.java
UIBspCanvas01.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

int MAX=700;
Canvas canvas = new Canvas(MAX, MAX);
root.setTop( canvas );

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();

gc.setLineWidth(2);
gc.setStroke(Color.RED); // Linienfarbe
gc.strokeLine(0,0, MAX, MAX);
Dialogfenster mit einem Canvas-Element
Barcode Muster
UIBspCanvas02.java
UIBspCanvas02.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

int MAX=700;
Canvas canvas = new Canvas(MAX, MAX);
root.setTop( canvas );

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();
int x = 80;
for (int i=0; i<60; i++) {
gc.strokeLine(x,40,x,100);
x += 1+ 10*Math.random();
}
gc.stroke();
Dialogfenster mit einem Canvas-Element
Zeichen des "Haus des Nikolaus"
UIBspCanvas03.java
UIBspCanvas03.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

private void draw1() {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();
gc.clearRect(0,0, MAX, MAX);
gc.setFill(Color.RED);
gc.setLineWidth(2);
gc.setStroke(Color.GREEN); // Linienfarbe

gc.strokeLine(100,100, 600,100); // waagerecht oben
gc.strokeLine(600,100, 600,400); // senkrecht rechts
gc.strokeLine(100,400, 600,400); // waagerecht unten
gc.strokeLine(100,100, 100,400); // senkrecht links

gc.strokeLine(100,100, 600,400); // links oben nach rechts unten
gc.strokeLine(100,400, 600,100); // links unten nach rechts oben

gc.strokeLine(100,100, 350, 50); // links oben nach rechts unten
gc.strokeLine(350, 50, 600,100); // links unten nach rechts oben
gc.stroke();
}// draw1
Dialogfenster mit einem Canvas-Element
Random Rechtecke
UIBspCanvas04.java
UIBspCanvas04.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

Canvas canvas = new Canvas(MAX, MAX);
root.setTop( canvas );

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();
gc.setLineWidth(2);
gc.setStroke(Color.RED); // Linienfarbe
int x, y; // Mittelpunkt
int w, h; // Breite Höhe

for (int i=0; i<10; i++) {
gc.setLineDashes(2+40*Math.random(), 2+10*Math.random());
//gc.setLineDashOffset(2+400*Math.random());

x = (int) ( 400*Math.random() ); // Links Oben
y = (int) ( 200*Math.random() ); // Links Oben
w = (int) ( 400*Math.random() ); // Width
h = (int) ( 200*Math.random() ); // Height
gc.strokeRect(x,y,w,h);
}
gc.stroke();
Dialogfenster mit einem Canvas-Element
Random Ellipsen
UIBspCanvas05.java
UIBspCanvas05.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

Canvas canvas = new Canvas(MAX, MAX);
root.setTop( canvas );

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();
gc.setLineWidth(2);
gc.setStroke(Color.RED); // Linienfarbe
int x, y; // Mittelpunkt
int w, h; // Breite Höhe

for (int i=0; i<10; i++) {
gc.setLineDashes(2+40*Math.random(), 2+10*Math.random());
//gc.setLineDashOffset(2+400*Math.random());

x = (int) ( 400*Math.random() ); // Links Oben
y = (int) ( 200*Math.random() ); // Links Oben
w = (int) ( 400*Math.random() ); // Width
h = (int) ( 200*Math.random() ); // Height
gc.strokeOval(x,y,w,h);
}



gc.stroke();
Dialogfenster mit einem Canvas-Element
Random PolyLines
UIBspCanvas06.java
UIBspCanvas06.png

import javafx.scene.paint.*;
import javafx.scene.canvas.*;

Canvas canvas = new Canvas(MAX, MAX);
root.setTop( canvas );

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.beginPath();
gc.setLineWidth(2);
gc.setStroke(Color.RED);

double[] xPoints=new double[10];
double[] yPoints=new double[10];

//void strokePolyline(double[] xPoints, double[] yPoints, int nPoints)

for (int i=0; i<3; i++) {
switch (i) {
case 0:
gc.setStroke(Color.RED);
break;
case 1:
gc.setStroke(Color.GREEN);
break;
case 2:
gc.setStroke(Color.BLUE);
break;
case 3:
gc.setStroke(Color.MAGENTA);
break;
}
gc.setLineDashes(2+40*Math.random(), 2+10*Math.random());
//gc.setLineDashOffset(2+400*Math.random());

for (int j=0; j<6; j++) {
xPoints[j] = 2+600*Math.random();
yPoints[j] = 5+470*Math.random();
}

gc.strokePolyline(xPoints,yPoints,4);
}

gc.stroke();
Dialogfenster mit einem Canvas-Element
Verschiedene Beispiele
UIBspCanvas07.java

UIBspCanvas07.png
UIBspCanvas07b.png
UIBspCanvas07c.png
UIBspCanvas07d.png
UIBspCanvas07e.png
UIBspCanvas07f.png
Dialogfenster mit einem Canvas-Element
XY-Grafik (ohne Kurven)
UIBspCanvas08.java
UIBspCanvas08.png

Das Haupelement ist ein Stackpanel.
Damit kann man dem Canvas die aktuelle Fenster "verpassen":

private Canvas canvas = null;
private Button bn = new Button("1");
private boolean start=true;


private Label editorstatus= new Label("-");

@Override
public void start(Stage stage) {
this.stage = stage;
StackPane root = new StackPane();
bn.setMaxWidth(Double.POSITIVE_INFINITY);
bn.setMaxHeight(Double.POSITIVE_INFINITY);
bn.setVisible(false);

root.getChildren().add( bn );
root.getChildren().add( setCenterElements() );

Scene scene= new Scene(root, 900, 900);
stage.setTitle("UICanvas08");
stage.widthProperty().addListener((obs, oldVal, newVal) -> {
resizeCanvas();
});

stage.heightProperty().addListener((obs, oldVal, newVal) -> {
resizeCanvas();
});
stage.setScene(scene);
stage.show();
start=false;
resizeCanvas();
}

private void resizeCanvas() {
if (!start) {
System.out.println("H: "+bn.getHeight()+" W: "+bn.getWidth());
canvas.setHeight(bn.getHeight());
canvas.setWidth(bn.getWidth());
draw();
}
}



BorderPane-Snippet
Canvas-Snippet