按下按钮,在ArrayList中更新显示的图像

时间:2015-04-26 16:15:51

标签: java arraylist javafx

我目前正在开发Java高卡片游戏。

我有一个带有播放,处理和退出按钮的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;
        }    
    }

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您不会将图像链接到ArrayList。您将其链接到卡。只需在Card类中创建对图像的引用即可。迭代卡并逐个加载图像。

关于你的代码,我建议你不要一直使用Random来调用卡片上的shuffle。除此之外,我还会为西装和排名创建枚举。如果您愿意,可以查看一些example code

答案 1 :(得分:0)

所以你想在按下按钮时更新显示的图像,对吗?

听起来你希望ArrayList做一些魔术 我甚至不确定ArrayList是你想要的,我也不知道为什么你会这么想。

但这是如何运作的:

你需要在某个地方以某种形式提供图像。 JavFX这样做的方式是ImageView的数组 您可以添加和删除任何Parent您喜欢的内容(请注意,您不能两次绘制相同的ImageView,但这种情况不应发生在您的纸牌游戏中)。
您需要在按钮上添加事件处理程序才能完成此操作:添加和删除图像(通过parent.getChildren().add() / parent.getChildren().remove())。

原始答案(也应该有用,但不是&#34; JavaFX方式&#34;):

你需要在某个地方以某种形式提供图像。最直接的方法可能是Image的数组 然后你需要一个地方在某处绘制你的图像。同样,最简单的方法可能是在HBox添加尽可能多的Canvas,因为您需要绘制卡片。
然后,您需要在按钮上添加事件处理程序以更新这些画布。当前卡片发生变化后,对于每张可见卡片,您需要从Image阵列中获取正确的图片,并使用canvas {{3}在右侧画布的GraphicsContext上绘制} .getGraphicsContext2D()