Center canvas inside another canvas in JavaFX

0

I managed to do so,

public class Graficos extends StackPane {

 private Canvas imagem;

    public void criarAreaDeDesenho(){ //foi criado dentro de uma StackPane

            GridPane grid = new GridPane();
            grid.setPadding(new Insets(5));
            grid.setHgap(10);
            grid.setVgap(10);

            imagem = new Canvas();
            imagem.setHeight(500);
            imagem.setWidth(700);
            GridPane.setHalignment(imagem, HPos.CENTER);
            grid.add(imagem, 0, 2);

            GraphicsContext gc = imagem.getGraphicsContext2D();
            gc.setFill(Color.BLACK);
            gc.fillRect(0, 0, imagem.getWidth(), imagem.getHeight());
            gc.setFill(Color.WHITE);
            gc.fillRect(1, 1, imagem.getWidth() - 2, imagem.getHeight() - 2);

            desenharEixoXY(gc);//Aqui chamo o metodo desenhar eixo(x,y)

           getChildren().addAll(grid);// será adicionado a gridpane ao stackpane
      }

   private void desenharEixoXY(GraphicsContext gc1) {

        int eixoX[] = new int[20];
        int eixoY[] = new int[20];

        gc1 = imagem.getGraphicsContext2D();
        gc1.setStroke(Color.BLACK);
        gc1.setLineWidth(1);

        // Eixo X  
        //gc1.strokeText("X", 620, 220);
        gc1.strokeLine(20, 287, 565, 287);
        // Eixo Y  
        //gc1.strokeText("Y", 100, 50);
        gc1.strokeLine(293, 80, 293, 540);

        int ptsX = 560;
        int ptsY = 540;
        for (int xcont = 0; xcont < 20; xcont++) {
            eixoX[xcont] = ptsX;
            eixoY[xcont] = ptsY;
            ptsX -= 28;
            ptsY -= 24;

            gc1.setStroke(Color.RED);
            gc1.setLineWidth(1);

            gc1.strokeLine(eixoX[xcont], 282, eixoX[xcont], 293);
            gc1.strokeLine(288, eixoY[xcont], 299, eixoY[xcont]);

        }
    }
}

AndIhavedoubtsincentralizingtheCartesianflatimagesothatitstaysthisway

I've tried to gc1.translate(x,y) but it's not working

    
asked by anonymous 25.05.2014 / 15:06

0 answers