在JavaFX中放置和显示笛卡尔平面图形中的像素



我有这个方法在JavaFX中绘制笛卡尔平面,使用画布,想知道是否有可能放置或显示像素:

public class Grafics extends StackPane {
         private Canvas canvas;
            public void Grafics(){ 
                    GridPane grid = new GridPane();
                    grid.setPadding(new Insets(5));
                    grid.setHgap(10);
                    grid.setVgap(10);
                    canvas = new Canvas();
                    canvas.setHeight(500);
                    canvas.setWidth(700);
                    GridPane.setHalignment(canvas, HPos.CENTER);
                    grid.add(canvas, 0, 2);
                    GraphicsContext gc = canvas.getGraphicsContext2D();
                    gc.setFill(Color.BLACK);
                    gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
                    gc.setFill(Color.WHITE);
                    gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
                    drawAxesXY(gc); //call the method drawAxes
                   getChildren().addAll(grid);// add an gridpane in stackpane
              }

private void drawAxesXY(GraphicsContext gc1) {
    gc1 = canvas.getGraphicsContext2D().getPixelWriter();  
 PixelWriter pixelWriter = gc1.getPixelWriter();
                    gc1.setFill(Color.WHITE);
                    gc1.setStroke(Color.BLACK);
                    gc1.setLineWidth(1.5);
                    gc1.strokeText("Y", 350, 30);
                    gc1.scale(1, 1);  
                    gc1.translate((canvas.getHeight() / 50) - (canvas.getWidth() / 10), canvas.getHeight() / 50);
                    gc1.strokeLine(canvas.getWidth() - 300, canvas.getWidth() - 300,
                            canvas.getHeight() - 100, canvas.getHeight() / 30);
                    pixelWriter.setColor(300, 300, Color.RED);

                    gc1.strokeText("X", 620, 220);  
                    gc1.translate(canvas.getWidth() - (canvas.getHeight() / 10), -220);
                    gc1.rotate(90.0);
                    gc1.setFill(Color.WHITE);
                    gc1.setStroke(Color.RED);
                    gc1.setLineWidth(1.5);
                    gc1.strokeLine(canvas.getWidth() - 250, canvas.getWidth() - 250,
                            canvas.getHeight() - 50, canvas.getHeight() / 30);
                    pixelWriter.setColor(620, 220, Color.RED);
                        }
                    }

我用一行黑色,另一行红色http://postimg.org/image/tewjhco4z/

,我想知道是否可以在图中这样放置或显示像素http://postimg.org/image/98k9mvnb3/

使用PixelWriter在画布中写入像素

GraphicsContext gc          = canvas.getGraphicsContext2D();
PixelWriter     pixelWriter = gc.getPixelWriter();
pixelWriter.setColor(
    75, 100, Color.FORESTGREEN
);

最新更新