我已经使Minimal Code base成为可行的,希望它有助于作为示例你可以在构造函数中使用随机PNG图像文件在Class 2中BufferedImage.getSubImage从只有1个文件获取所有需要的图像并添加每个BufferedImage转换为ImageView,将它提供给每个最多32个数组:)
第1课:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.effect.Glow;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Game extends Application{
private GameMenu gameMenu;
@Override
public void start(Stage primaryStage) throws Exception {
// nastavenie pozadia
Pane root = new Pane();
root.setPrefSize(1600,900);
primaryStage.setResizable(false); // resize okna = false
primaryStage.show(); // kebyze to nemame mame hnusne biele okraje xD
this.gameMenu = new GameMenu();
this.gameMenu.setVisible(true);
root.getChildren().addAll(this.gameMenu);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} // End start
private class GameMenu extends Parent {
public GameMenu() throws Exception {
BalikKariet m = new BalikKariet();
VBox menu0 = new VBox(15);
menu0.setTranslateX(650);
menu0.setTranslateY(300);
GameMenuCSS btn1 = new GameMenuCSS("Random Text");
btn1.setOnMouseClicked(event -> {
getChildren().addAll(m.zoberKartu().getObrazok());
});
menu0.getChildren().addAll(btn1);
getChildren().addAll(menu0);
} // End Const.
} // End GameMenu
public static class GameMenuCSS extends StackPane {
private Text text;
public GameMenuCSS(String text) {
this.text = new Text(text);
this.text.setFont(this.text.getFont().font(20));
this.text.setFill(Color.WHITE);
Rectangle bg = new Rectangle(250,30);
bg.setOpacity(0.6);
bg.setFill(Color.BLACK);
GaussianBlur blur = new GaussianBlur(3.5);
bg.setEffect(blur);
setAlignment(Pos.CENTER);
setRotate(-0.5);
getChildren().addAll(bg, this.text);
setOnMouseEntered(event -> {
bg.setTranslateX(10);
this.text.setTranslateX(10);
bg.setFill(Color.WHITE);
this.text.setFill(Color.BLACK);
});
setOnMouseExited(event -> {
bg.setTranslateX(0);
this.text.setTranslateX(0);
bg.setFill(Color.BLACK);
this.text.setFill(Color.WHITE);
});
DropShadow drop = new DropShadow(50, Color.WHITE);
drop.setInput(new Glow());
setOnMousePressed(event -> setEffect(drop));
setOnMouseReleased(event -> setEffect(null));
} // koniec konstruktora
} // koniec Triedy MenuPolosky
public static void main(String[] args) {
launch(args);
}
}
第2课:
package com.sayky.test;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BalikKariet {
private Karta[] balik;
private int dalsiaKarta; // index nasledujucej karty
private ImageView imgViwVsetkyKarty;
private ImageView tempImageViewObrazokKarty;
public BalikKariet() throws IOException {
int[] hodnota = {7, 8, 9, 10, 11, 12, 13, 14};
String[] typKariet = {"7","8","9","10","Dolek","Horek","Kral","Eso"};
String[] farbay = {"Zelen","Cervem","Gula","Zalud"};
balik = new Karta[32];
dalsiaKarta = 0;
final int sirka = 74;
final int vyska = 120;
final int riadky = 4;
final int stlpec = 8;
BufferedImage vssetkyKartyObrazok = ImageIO.read(new File("res/img/vsetkyKarty.png"));
Image imgVsetkyKarty = SwingFXUtils.toFXImage(vssetkyKartyObrazok, null);
imgViwVsetkyKarty = new ImageView(imgVsetkyKarty);
BufferedImage tempObrazokKarty;
for (int farba = 0; farba < 4 ; farba++){
for (int typKarty = 0; typKarty < 8; typKarty++){
//ukaz obrazok
tempObrazokKarty = vssetkyKartyObrazok.getSubimage(
typKarty*sirka, // zaciatok x suradnic
farba*vyska, // zaciatok y sura dnic
sirka, // sirka
vyska); // vyska
balik[(typKarty + (farba*8))] = new Karta(
farbay[farba], // zavola Farby Array aby nadobudla meno Farba
typKariet[typKarty],
typKarty+7, // pozicia 0+7 je 7 zacina na hodnote prvej karty
tempImageViewObrazokKarty);
Image tempImgObrazokKarty = SwingFXUtils.toFXImage(tempObrazokKarty, null);
tempImageViewObrazokKarty = new ImageView(tempImgObrazokKarty);
}
}
} // koniec konstruktora
public Karta zoberKartu(){
if ( dalsiaKarta< balik.length) {
return balik[dalsiaKarta++];
} else {
return null;
}
}
} // koniec classy
第3课:
package com.sayky.test;
import javafx.scene.image.ImageView;
public class Karta {
private String typkarty; // cislo karty
private String farba; // telo
private int hodnota; // hodnota karty
private ImageView kartaObrazok;
/**
* Konstruktor pre PexesovaKarta triedu bude obsahovat toto ( pre lepsiu orientaciu )
* @param farba -suit- "Zelen" "Cerven" "Gula" "Zalud"
* @param typkarty -face- = 7, 8, 9, 10, Dolek, Horek, Kral, Eso;
* @param hodnota = 7, 8, 9, 10, 11-dolek, 12-horek, 13-kral, 14-eso;
*/
public Karta(String farba, String typkarty, int hodnota, ImageView kartaObrazok) {
// nastavenie hodnot lokalnich premien do globalnich
this.farba = farba;
this.typkarty = typkarty;
this.hodnota = hodnota;
}
public String toString(){
return typkarty + " " + farba;
}
public int getHodnotaKarty(){
return hodnota;
}
public Karta getKarta() {
return this;
}
public ImageView getObrazok() {
return kartaObrazok;
}
}
答案 0 :(得分:1)
Karta
类从不初始化kartaObrazok
字段,因此getObrazok()
返回null。