2017-02-05 64 views
1

創建外形上,我需要放置在畫布位置整形JavaFX中畫布

Class Square繪製一個正方形,並插入到canvas位置

public class Square{ 
    //calculate the position of the rand column to 
    //draw and insert in the position of the canvas 
    public void drawSquare(int posX, int posY, GraphicsContext gc) { 
     //Square Shadow 
     //gc.rect(posX, posY, w, h); 
     gc.rect(posX + 1, posY + 53, 50, 50); 
     gc.fill(); 
     gc.beginPath(); 
     //Square 
     gc.beginPath(); 
     gc.setFill(Color.WHITE); 
     gc.setStroke(Color.BLACK); 
     gc.setLineWidth(2); 
     //gc.rect(posX, posY, w, h); 
     gc.rect(posX + 1, posY + 53, 48, 48); 
     gc.fill(); 
     gc.stroke(); 

    } 
} 

Canvas instanceheight = 450width = 600

Canvas canvas = new Canvas(); 
canvas.setHeight(450); 
canvas.setWidth(600); 

and GraphicsContext to draw square

GraphicsContext gc = canvas.getGraphicsContext2D(); 

這個循環中,canvas, 畫4 rows和6 columnssquare和我的懷疑是怎麼linecalculate the positioncolumndraw square,並在畫布position插入當我打電話pieces.drawSquare(i, j, gc);和方法drawSquare創建形狀,但無疑是如何position他們如果是多於一個的形狀

for (int i = 0; i < 4; i++) { //4 rows 
     for (int j = 0; j < 6; i++) { //6 columns 
     Piece pieces = new Piece(); 
     pieces.drawSquare(i, j, gc); 
     } 

這個圖像是例如,

和物鏡是填補4行和6列

enter image description here

我已經想到了將畫布的大小和寬度與形狀的大小和寬度,但它不工作,也許可以有另一種解決方案

+0

我無法讀到的一切,但我的理解是,你要填充矩形畫布,除了第一行? –

回答

2

我認爲這可以讓你跳起步

enter image description here

public class Main extends Application { 

private SimpleIntegerProperty rowProperty = new SimpleIntegerProperty(4); //default 
private SimpleIntegerProperty columnProperty = new SimpleIntegerProperty(6);//default 


@Override 
public void start(Stage primaryStage) { 
    try { 
     BorderPane root = new BorderPane(); 
     root.setPadding(new Insets(5)); 
     HBox top; 
     TextField rowField = new TextField(); 
     rowField.setMaxWidth(60); 
     rowField.textProperty().addListener(new ChangeListener<String>() { 
      @Override 
      public void changed(ObservableValue<? extends String> observable, 
        String oldValue, String newValue) { 
       try{ rowProperty.setValue(Integer.valueOf(newValue));}catch(NumberFormatException e){} 
      } 
     }); 
     TextField colField = new TextField(); 
     colField.setMaxWidth(60); 
     colField.textProperty().addListener(new ChangeListener<String>() { 
      @Override 
      public void changed(ObservableValue<? extends String> observable, 
        String oldValue, String newValue) { 
       try{ columnProperty.setValue(Integer.valueOf(newValue));}catch(NumberFormatException e){} 
      } 
     }); 
     top = new HBox(10,new Label("ROW FIELD"),rowField, new Label("COLUMN FIELD"),colField); 
     top.setAlignment(Pos.CENTER); 
     root.setStyle("-fx-background-color: white;"); 
     root.setTop(top); 
     //////////////////////////////////////////////////////////////////////////////////// 
     Canvas canvas = new Canvas(500,400); 
     canvas.getGraphicsContext2D().setFill(Color.BLACK); 
     canvas.getGraphicsContext2D().setStroke(Color.GOLD); 

     ChangeListener<Number> chan = new ChangeListener<Number>() { 
      int space = 2; 
      @Override 
      public void changed(ObservableValue<? extends Number> observable, 
        Number oldValue, Number newValue) { 
       ///i will draw here 
       canvas.getGraphicsContext2D().clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); 
       int rectW = (int) canvas.getWidth(); 
       rectW = rectW/columnProperty.intValue(); 
       int rectH = (int) canvas.getHeight(); 
       rectH = rectH/rowProperty.intValue(); 

       System.out.println(rectW); 
       System.out.println(rectH); 

       for(int k = 0; k < canvas.getHeight()/rectH; k++){ 
        for(int i =0; i< canvas.getWidth()/rectW; i++){ 
         canvas.getGraphicsContext2D().fillRect((i*rectW) + (i*space), 
           (k*rectH) + (k*space), 
           rectW, rectH); 
        } 
       } 

      } 
     }; 
     rowProperty.addListener(chan); 
     columnProperty.addListener(chan); 
     ////////////////////////////////////////////////////////////////////////////////////////// 

     root.setCenter(canvas); 
     Label l = new Label("ENTER NUMBERS TO FIELDS TO SEE IT"); 
     l.setStyle("-fx-background-color: blueviolet; -fx-text-fill: white;"); 
     l.setPrefWidth(Double.MAX_VALUE); 
     l.setAlignment(Pos.CENTER); 
     root.setBottom(l); 
     Scene scene = new Scene(root,500,500); 
     scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } catch(Exception e) { e.printStackTrace(); } 
} 



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

}

+0

感謝Elltz,就是這樣,我們的目標是進行這個操作'canvas.getGraphicsContext2D()。fillRect((i * rectW)+(i * space),(k * rectH)+(k * space),rectW ,rectH);'用'canvas.getHeight()/ rectH'和'canvas.getWidth()/ rectW' – DSanches