如何绘制坐标的ArrayList到画布? 【JAVA]

时间:2016-02-20 14:24:23

标签: java canvas arraylist

我正在努力制造游戏炮兵。为了产生地图,我的讲师已经告诉我制作一个相对随机生成的Y坐标列表。现在我有了这个列表(GameMap),但我似乎无法找到适当的Canvas方法来绘制它们。 Canvas不是最好的工作方式,还是有方法可以做到这一点?这个ArrayList的长度是1000+大。

public class MapView extends StackPane {

private Canvas canvas;
private GraphicsContext graphicsContext;
private GameMap map;

    public MapView(){
        initialiseNodes();
        layoutNodes();
    }

    public void initialiseNodes(){
        canvas = new Canvas(250, 250);
        graphicsContext = canvas.getGraphicsContext2D();
    }

    public void layoutNodes(){
        graphicsContext.setFill(Color.ALICEBLUE);
        graphicsContext.fillRect(75, 75, 100, 100);
        this.getChildren().add(canvas);
        for(Integer i : map){
            graphicsContext.
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我不确定你的问题如何显示坐标,但这是你要找的那种例子吗?

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.stage.Stage;

public class DrawPoints extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        int width = 200, height = 200;
        Canvas c = new Canvas(width, height);

        /*
         * Some randomized list of points.
         */
        List<Point2D> points = new ArrayList<>();
        {
            Random rnd = new Random();
            for (int i = 0; i < 100; i++) {
                points.add(new Point2D(rnd.nextInt(width), rnd.nextInt(height)));
            }
        }

        /*
         * Drawing them as points (1x1 filled rectangles) on a Canvas
         */
        for (Point2D p : points) {
            c.getGraphicsContext2D().fillRect(p.getX(), p.getY(), 1, 1);
        }

        Group root = new Group();
        root.getChildren().add(c);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}