我目前正在使用Java進行高級紙牌遊戲。在按鈕上更新ArrayList中顯示的圖像按
我有一個帶有播放,交易和退出按鈕的JavaFX UI。
我創建4個玩家,記錄最高的數字並在用戶界面中顯示卡片。
現在每按下一次交易按鈕,就會發出4張牌,並顯示當前回合的贏家。
現在這是我的問題:如何將我的圖像鏈接到ArrayList,以便在按下按鈕時更新所顯示的卡片?我知道我必須向按鈕添加事件處理程序,但我不知道如何將圖像鏈接到ArrayList,以便每次按下按鈕時都會更改它們。
的主界面:
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
import javafx.scene.shape.Rectangle;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
import javafx.scene.layout.*;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
public class Main extends Application {
Stage window;
Scene scene1, scene2, scene3;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
// Root node
Group root = new Group();
//Button 1
Label label1 = new Label("Welcome to the JavaFx Card Game!");
label1.setAlignment(Pos.TOP_CENTER);
label1.setFont(Font.font("Times New Roman",
FontWeight.EXTRA_BOLD, FontPosture.ITALIC, 200));
label1.setStyle("-fx-background-color: Blue;");
Button button1 = new Button("Click here to Start the Game");
button1.setStyle("-fx-border-color: blue;");
label1.setFont(Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.ITALIC, 20));
button1.setOnAction(e -> window.setScene(scene2));
//Layout 1 - children laid out in vertical column
HBox layout1 = new HBox(20);
layout1.getChildren().addAll(label1, button1);
// layout1.setStyle("-fx-border-color: red; -fx-background-color: Blue;");
scene1 = new Scene(layout1, 500, 500);
//Button 2
Button button2 = new Button("Go Back to Main page");
button2.setOnAction(e -> window.setScene(scene1));
button2.setStyle("-fx-border-color: red;");
//Button 3
Button button3 = new Button("Play");
button3.setStyle("-fx-border-color:orange;");
button3.setOnAction(e -> window.setScene(scene3));
//Button 4
Button button4 = new Button("Deal");
button4.setStyle("-fx-border-color:yellow;");
//Button 5
Button button5 = new Button("Exit");
button5.setStyle("-fx-border-color:pink");
// creating cards box
Rectangle rectangle = new Rectangle(10,50,200, 300);
rectangle.setArcHeight(15);
rectangle.setArcWidth(15);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.AQUA);
Rectangle rectangle2 = new Rectangle(20,200,200, 300);
rectangle2.setArcHeight(15);
rectangle2.setArcWidth(15);
rectangle2.setStroke(Color.BLACK);
rectangle2.setFill(Color.AQUA);
Rectangle rectangle3 = new Rectangle(10,50,200, 300);
rectangle3.setArcHeight(15);
rectangle3.setArcWidth(15);
rectangle3.setStroke(Color.BLACK);
rectangle3.setFill(Color.AQUA);
Rectangle rectangle4 = new Rectangle(20,200,200, 300);
rectangle4.setArcHeight(15);
rectangle4.setArcWidth(15);
rectangle4.setStroke(Color.BLACK);
rectangle4.setFill(Color.AQUA);
//Layout 2
HBox layout2 = new HBox();
layout2.setPadding(new Insets(100, 100, 100, 100));
layout2.getChildren().addAll(button2, button3, button4, button5);
layout2.setSpacing(25);
layout2.setLayoutX(200);
layout2.setLayoutY(350);
// Layout 3
HBox layout3 = new HBox();
layout3.setPadding(new Insets(100, 100, 100, 100));
layout3.setSpacing(25);
layout3.getChildren().addAll(rectangle, rectangle2, rectangle3, rectangle4);
// adding layout 3 and layout 2
root.getChildren().addAll(layout3, layout2);
scene2 = new Scene(root, 600, 300);
//Display scene 1 at first
window.setScene(scene1);
window.setTitle("Card Game");
window.show();
// Max the window
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
primaryStage.setX(primaryScreenBounds.getMinX());
primaryStage.setY(primaryScreenBounds.getMinY());
primaryStage.setWidth(primaryScreenBounds.getWidth());
primaryStage.setHeight(primaryScreenBounds.getHeight());
primaryStage.show();
// Back ground Image for Layer 1
BackgroundImage myBI= new BackgroundImage(new Image("http://i.imgur.com/LAXk1jw.png",1000,1000,false,true),
BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
//then you set to your node
layout1.setBackground(new Background(myBI));
}
}
卡類:
import java.util.ArrayList;
import java.util.Random;
public class Card {
String finalCard = "";
int suit, number;
static String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits
static String [] numbers = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" , "Ace" }; //card values
String card = "";
public Card() {
}
public Card(int suits, int numbers)
{
suit = suits;
number = numbers;
}
public int getSuit()
{
return suit;
}
public String toString()
{
String finalCard = numbers[number] + " of " + suits[suit];
return finalCard;
}
}
和甲板類:
import java.util.ArrayList;
import java.util.Random;
public class FullDeck {
private ArrayList<Card> cards = new ArrayList<Card>();//card array list
public FullDeck()
{
for(int a =0; a<=3; a++) //loops through suits
{
for(int b =0; b<=12;b++) //loops through values
{
cards.add(new Card(a,b)); //creates adds cards to list
}
}
}
public Card drawRandomCard()
{
Random generator = new Random(); //picks random card
int index = generator.nextInt(cards.size());
return cards.remove(index); //removes card from list
}
public String toString()
{
String result = "Cards remaining in deck: " + cards; //not currently used
return result;
}
}
任何幫助表示讚賞。
那不是你怎麼JavaFX中做一個紙牌遊戲。您可以使用像ImageView這樣的節點來顯示卡片。您不直接在畫布上繪製。 – Roland
好吧,更新我的答案。 – Siguza