如何调用图像数组

时间:2015-08-07 23:15:37

标签: java arrays javafx

所以,我被分配了一个问题,我必须使用JavaFX,4张随机卡和一个"刷新"按钮,随机播放并显示卡片。

问题源于我需要显示卡片时。 我不完全确定如何显示/显示图像。我查阅了教科书,我无法找到解决这个问题的另一条途径,也可以咨询同学。

我在Java项目中的另一个源文件(对等建议)中有卡片图像。

这是我到目前为止所做的:

package Images;

import java.util.Arrays;
import java.util.Collections;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CardRefreshButton extends Application{

    Image[] deck = new Image[51];

    public void add() {
        for(int i = 0; i < deck.length; i++){
            deck[i] = new Image(i + ".png");
        }
    }
    public void shuffle() {
        Collections.shuffle(Arrays.asList(deck));

    }

    @Override
    public void start(Stage primaryStage)  {

        primaryStage.setTitle("Shuffling Deck Program");

        BorderPane bPane1 = new BorderPane();
        HBox pane1 = new HBox(10);
        HBox pane2 = new HBox(10);

        Button refresh = new Button("Refresh");

        refresh.setOnAction((event) -> {
            add();
            shuffle();

            pane1.getChildren().clear();
            start(primaryStage);
        });

        pane1.setAlignment(Pos.CENTER);
        pane2.setAlignment(Pos.CENTER);

        Image card1 = deck[0];
        Image card2 = deck[1];
        Image card3 = deck[2];
        Image card4 = deck[3];
        ImageView view1 = new ImageView(card1);
        ImageView view2 = new ImageView(card2);
        ImageView view3 = new ImageView(card3);
        ImageView view4 = new ImageView(card4);

        pane1.getChildren().addAll(view1,view2,view3,view4);
        pane2.getChildren().add(refresh);

        bPane1.setCenter(pane1);
        bPane1.setBottom(pane2);


        Scene scene1 = new Scene(bPane1, 600,650);
        primaryStage.setScene(scene1);
        primaryStage.show();


    }
}

2 个答案:

答案 0 :(得分:0)

以下是您的代码的修改版本:

  • 首先加载图片
  • 然后您创建ImageView节点并将它们放入HBox
  • 在您的刷新代码中
  • 您将列表移动并将图像分配给现有的ImageView节点

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CardRefreshButton extends Application {

    List<Image> deck = new ArrayList<>();

    public void load() {
        for (int i = 0; i < 52; i++) {
            deck.add(new Image(getClass().getResource(i + ".png").toExternalForm()));
        }
    }

    @Override
    public void start(Stage primaryStage) {

        load();

        primaryStage.setTitle("Shuffling Deck Program");

        BorderPane bPane1 = new BorderPane();
        HBox pane1 = new HBox(10);
        HBox pane2 = new HBox(10);

        ImageView view0 = new ImageView(deck.get(0));
        ImageView view1 = new ImageView(deck.get(1));
        ImageView view2 = new ImageView(deck.get(2));
        ImageView view3 = new ImageView(deck.get(3));

        Button refresh = new Button("Refresh");

        refresh.setOnAction((event) -> {

            Collections.shuffle( deck);

            view0.setImage(deck.get(0));
            view1.setImage(deck.get(1));
            view2.setImage(deck.get(2));
            view3.setImage(deck.get(3));

        });

        pane1.setAlignment(Pos.CENTER);
        pane2.setAlignment(Pos.CENTER);

        pane1.getChildren().addAll(view0, view1, view2, view3);
        pane2.getChildren().add(refresh);

        bPane1.setCenter(pane1);
        bPane1.setBottom(pane2);

        Scene scene1 = new Scene(bPane1, 600, 650);
        primaryStage.setScene(scene1);
        primaryStage.show();

    }

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

答案 1 :(得分:0)

您也可以使用干净的编码:

Main.java文件:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Display Random Images");
    primaryStage.setScene(new Scene(root, 900, 500));
    primaryStage.show();
    }


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

Controller.java文件:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Random;

public class Controller {
@FXML
private Button btnRefresh
@FXML
private TextField txtShow;
@FXML
private ImageView imgAni;

@FXML
public void initialize(){
    btnRefresh.setText(" Refresh ");
}
@FXML
public void btnLoad(ActionEvent event){vImage();}

void vImage(){
    Image img1 = new Image("sample/res/tiger.jpg");
    Image img2= new Image("sample/res/horse.jpg");
    Image img3 = new Image("sample/res/lion.jpg");
    //imgAni.setImage(img1);*/
    Image[] img = {img1,img2,img3};
    Random rand = new Random();
    for (int i = 0; i < img.length; i++){
        imgAni.setImage(img[rand.nextInt(img.length)]);
    }
  }
}

sample.fxml文件:

<AnchorPane prefHeight="484.0" prefWidth="599.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
  <children>
    <Button fx:id="btnRefresh" layoutX="155.0" layoutY="187.0" mnemonicParsing="false" onAction="#btnLoad" prefHeight="50.0" prefWidth="90.0" text="" />
    <TextField fx:id="txtShow" layoutX="136.0" layoutY="74.0" prefWidth="200.0" />
    <ImageView fx:id="imgAni" fitHeight="400.0" fitWidth="221.3334615803972" layoutX="363.0" layoutY="21.0" pickOnBounds="true" preserveRatio="true" />
  </children>
</AnchorPane>

我支持将程序分成不同的文件,类和模块。 我的照片是在一个名为res inside sample的包中。有一个伟大的编码日