日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

javafx游戏_JavaFX游戏(四连环)

發(fā)布時(shí)間:2023/12/3 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javafx游戏_JavaFX游戏(四连环) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

javafx游戲

這是我的第一個(gè)JavaFX游戲教程,也是我關(guān)于JavaFX面板的第一篇博客文章。 我僅用200幾行代碼就完成了這四款連接游戲,足以應(yīng)付一個(gè)簡(jiǎn)單的游戲。 我在這里使用GridPane面板對(duì)磁盤(pán)進(jìn)行布局,GridPane是JavaFX布局窗格之一,但它與另一個(gè)窗格不同,因?yàn)樗谛泻土械撵`活網(wǎng)格中布局了其子項(xiàng)。

這是有關(guān)如何設(shè)置GridPanes列和行約束的代碼片段:

gridpane.getColumnConstraints().addAll(new ColumnConstraints(100,100,Double.MAX_VALUE), new ColumnConstraints(100,100,Double.MAX_VALUE),new ColumnConstraints(100,100,Double.MAX_VALUE),new ColumnConstraints(100,100,Double.MAX_VALUE));gridpane.getRowConstraints().addAll(new RowConstraints(100,100,Double.MAX_VALUE), new RowConstraints(100,100,Double.MAX_VALUE),new RowConstraints(100,100,Double.MAX_VALUE),new RowConstraints(100,100,Double.MAX_VALUE));

GridPane將具有4行4列100寬的正方形網(wǎng)格。

您可以享受下面的其余代碼。

import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.effect.DropShadow; import javafx.scene.effect.Reflection; import javafx.scene.input.MouseEvent; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Path; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.stage.Stage; import javafx.util.Duration;/**** @author mark_anro*/ public class Main extends Application {/*** @param args the command line arguments*/private SimpleObjectProperty<Color> playerColorProperty = new SimpleObjectProperty<Color>(Color.RED);private int r;private int c;public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) {final BorderPane root = new BorderPane();final GridPane gridpane = new GridPane();primaryStage.setTitle('JavaFX Connect Four');primaryStage.setResizable(true);final Button addCellButton = new Button('Add Grids');Scene scene = new Scene(root, 750, 680, true);scene.setFill(Color.BLACK);scene.getStylesheets().add('net/glyphsoft/styles.css');gridpane.setTranslateY(20);gridpane.setAlignment(Pos.CENTER);gridpane.getColumnConstraints().addAll(new ColumnConstraints(100,100,Double.MAX_VALUE), new ColumnConstraints(100,100,Double.MAX_VALUE),new ColumnConstraints(100,100,Double.MAX_VALUE),new ColumnConstraints(100,100,Double.MAX_VALUE));gridpane.getRowConstraints().addAll(new RowConstraints(100,100,Double.MAX_VALUE), new RowConstraints(100,100,Double.MAX_VALUE),new RowConstraints(100,100,Double.MAX_VALUE),new RowConstraints(100,100,Double.MAX_VALUE));createGrids(gridpane);root.setCenter(gridpane);DropShadow effect = new DropShadow();effect.setColor(Color.BLUE);addCellButton.setEffect(effect);addCellButton.setTranslateY(10);addCellButton.setTranslateX(10);root.setTop(addCellButton);addCellButton.setOnMouseClicked(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {addGrid(gridpane);}});primaryStage.setScene(scene);primaryStage.setResizable(false);primaryStage.show();}//Add Column and Rowprivate void addGrid(final GridPane gridpane){gridpane.getColumnConstraints().addAll(new ColumnConstraints(100,100,Double.MAX_VALUE));gridpane.getRowConstraints().addAll(new RowConstraints(100,100,Double.MAX_VALUE));createGrids(gridpane);}//Create Gridsprivate void createGrids(final GridPane gridpane){gridpane.getChildren().clear();for(r=0;r<gridpane.getColumnConstraints().size(); r++){for(c=0; c<gridpane.getColumnConstraints().size(); c++){Rectangle rect = new Rectangle(100,100);Circle circ = new Circle(47);circ.centerXProperty().set(50);circ.centerYProperty().set(50);Shape cell = Path.subtract(rect, circ);cell.setFill(Color.BLUE);cell.setStroke(Color.BLUE);cell.setOpacity(.8);DropShadow effect = new DropShadow();effect.setSpread(.2);effect.setRadius(25);effect.setColor(Color.BLUE);cell.setEffect(effect);final Circle diskPreview = new Circle(40);diskPreview.setOpacity(.5);diskPreview.setFill(Color.TRANSPARENT);diskPreview.setOnMouseEntered(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {diskPreview.setFill(Color.WHITE);if(playerColorProperty.get()==Color.RED){diskPreview.setFill(Color.RED);}else{diskPreview.setFill(Color.YELLOW);}}});diskPreview.setOnMouseExited(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {diskPreview.setFill(Color.TRANSPARENT);}});final Circle disk = new Circle(40);disk.fillProperty().bind(playerColorProperty);disk.setOpacity(.5);disk.setTranslateY(-(100*(r+1)));final TranslateTransition translateTranstion = new TranslateTransition(Duration.millis(300), disk);disk.setOnMouseEntered(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {diskPreview.setFill(Color.WHITE);if(playerColorProperty.get()==Color.RED){diskPreview.setFill(Color.RED);}else{diskPreview.setFill(Color.YELLOW);}}});disk.setOnMouseExited(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {diskPreview.setFill(Color.TRANSPARENT);}});disk.setOnMouseClicked(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {if(disk.getTranslateY()!=0){translateTranstion.setToY(0);translateTranstion.play();if(playerColorProperty.get()==Color.RED){playerColorProperty.set(Color.YELLOW);disk.fillProperty().bind(new SimpleObjectProperty<Color>(Color.RED));}else{playerColorProperty.set(Color.RED);disk.fillProperty().bind(new SimpleObjectProperty<Color>(Color.YELLOW));}}}});diskPreview.setOnMouseClicked(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {if(disk.getTranslateY()!=0){translateTranstion.setToY(0);translateTranstion.play();if(playerColorProperty.get()==Color.RED){playerColorProperty.set(Color.YELLOW);disk.fillProperty().bind(new SimpleObjectProperty<Color>(Color.RED));}else{playerColorProperty.set(Color.RED);disk.fillProperty().bind(new SimpleObjectProperty<Color>(Color.YELLOW));}}}});StackPane stack = new StackPane();stack.getChildren().addAll(cell, diskPreview, disk);gridpane.add(stack, c, r); if(r==gridpane.getColumnConstraints().size()-1){stack.setEffect(new Reflection());}}}} }

祝您編程愉快,別忘了分享!

參考:來(lái)自GlyphSoft博客的JCG合作伙伴 Mark Anro Silva的JavaFX Game(四人連接) 。


翻譯自: https://www.javacodegeeks.com/2012/09/javafx-game-connect-four.html

javafx游戲

總結(jié)

以上是生活随笔為你收集整理的javafx游戏_JavaFX游戏(四连环)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。