Hello i want to use java to draw lines with an refresh rate of 60fps:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.scene.paint.Color;
public class Example extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage theStage)
{
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 512, 512 );
root.getChildren().add( canvas );
GraphicsContext gc = canvas.getGraphicsContext2D();
Timeline gameLoop = new Timeline();
gameLoop.setCycleCount( Timeline.INDEFINITE );
final long timeStart = System.nanoTime() -10000000000000l;
KeyFrame kf = new KeyFrame(
Duration.seconds(0.017), // 60 FPS
new EventHandler<ActionEvent>()
{
public void handle(ActionEvent ae)
{
// Clear the canvas
gc.clearRect(0, 0, (int)canvas.getWidth(),(int)canvas.getHeight());
for(int i=0;i<(int)(canvas.getWidth()/10);i++) {
//gc.setS
for(int j=0;j<10;j++)
{
if(j==0){gc.setStroke(Color.web("#000000"));}
else{gc.setStroke(Color.web("#aaaaaa"));}
gc.moveTo(j+i*10, 110);
gc.lineTo(j+i*10, canvas.getHeight());
//using gc.stroke instead of moveTo and lineTo works with good performance,but i need 1px width
//gc.strokeLine(j+i*10, 110, j+i*10, canvas.getHeight());
}
}
gc.stroke();
}
});
gameLoop.getKeyFrames().add( kf );
gameLoop.play();
theStage.show();
theScene.widthProperty().addListener(observeable -> {
canvas.setWidth(theScene.getWidth());
});
}
}
however this is incredible slow and the program will crash. Using "strokeLine()" it runs ok,but i really need to draw lines width 1px width.
I suppose that i have to save the drawings in a buffer before i draw the full scene.But i read that javafx is keeping the low stuff from you.so is there another way to draw 1px lines?
You are not resetting the path to empty on each iteration. Thus the path strokes are accumulating: you have 500 lines on the first iteration, 1000 on the second, 1500 on the third, etc.
You need
gc.beginPath();
after you call gc.clearRect(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With