Graphics Context will only draw in one thread

南笙酒味 提交于 2019-12-25 08:05:38

问题


I am making a game of snake, but whenever I try to update my canvas in the draw() method, the new snake won't draw. It draws in the run thread. I have tried a bunch of different things, but I can't seem to get it working.

Imports:

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
import java.io.*;
import java.util.*;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;

Actual class:

public class Snake extends Application implements Runnable
{
    //Snake parts and locations
    boolean dead;
    int headX=0;
    int headY=0;
    // for the game
    Group root = new Group();
    Scene snakeG = new Scene(root, 550, 550,Color.BLACK);
    final Canvas canvas = new Canvas(550,550);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    //Start Game
    VBox newGame = new VBox(3);
    Scene startC = new Scene(newGame, 200, 200);
    Label info = new Label("Snake Game \nCreated by: Austin");
    Label rules = new Label("Rules\n1.) If you hit the edge you die\n2.) If you touch your snake you die\n3.)Collect fruit to increase your snakes size");
    Button startBut = new Button("Start Game");
    Stage startS;

    public static void main ( String[] args )
    {
        launch(args);
    }

    @Override
    public void start ( Stage primaryStage )throws Exception
    {
        startS=primaryStage;
        startS.setTitle("Snake");
        newGame.getChildren().addAll(info,rules,startBut);
        newGame.setAlignment(Pos.CENTER);
        startS.setScene(startC);
        startS.show();

        startBut.setOnAction(e ->
        {
            startGame();
        });
    }

    public void startGame()
    {
        System.out.println("Success");
        headX=275;
        headY=275;
        dead = false;
        gc.clearRect(0,0,800,800);
        startS.setScene(snakeG);
        gc.setFill(Color.GREEN);
        gc.fillRect(headX,headY,10,10);
        root.getChildren().add(canvas);
        (new Thread ( new Snake())).start();    
    }

    public void run()
    {
        draw(); 
    }

    // draws the snake
    public void draw()
    {
        System.out.println("DRAW STARTED");
        gc.setFill(Color.GREEN);
        gc.fillRect(50,50,10,10);   
    }
}

If you know of a better way to draw graphics in JavaFX, please tell me. This is the only way I could find for what I am doing.


回答1:


There are some problems with your approach.

  1. You don't need your own thread.
  2. You can only modify the active scene graph (including a canvas) using the JavaFX Thread. Read the JavaFX concurrency documentation:

    The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.

  3. Your main application class should not implement Runnable.

Some suggestions:

  1. You may find it easier to use SceneGraph nodes for your game objects rather than a Canvas, but either should work.
  2. You can implement your game loop using an AnimationTimer as demoed here.
  3. Here is a sample of using an AnimationTimer for display.


来源:https://stackoverflow.com/questions/41274078/graphics-context-will-only-draw-in-one-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!