0

there i have made Minimal Code base as passible, hope it helps as example u can use random PNG image files as you want in Class 2 in constructor BufferedImage.getSubImage is getting all needed images from only 1 file and adding every single BufferedImage converted to ImageView giving it to every array up to 32 :)

Class 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);

}
}

Class 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

Class 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;
}
}
7
  • When you say it crashes, do you get an error with it? If so, please share. Commented Nov 19, 2016 at 15:50
  • Also, a MCVE what be greatly helpful. Commented Nov 19, 2016 at 15:51
  • @Jonah Error :'Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Children: child node is null: parent = GameMenuDemo$GameMenu@66820790' at com.sayky.sedma.GameMenuDemo$GameMenu.lambda$new$15(GameMenuDemo.java:333) this line is 'getChildren().addAll(m.bk.getKartaBalik(0).getObrazok()); ' it doesnt not crash it just show this error and nothing happends Commented Nov 19, 2016 at 16:03
  • So you're never instantiating the node you are trying to add. Commented Nov 19, 2016 at 16:12
  • 1
    What @Jonah is saying is that getObrazok() is returning null. No one can tell you why that is unless you create and post a minimal reproducible example (though you don't seem to initialize the field in the constructor; is that what you are missing?) Commented Nov 19, 2016 at 16:24

1 Answer 1

1

The Karta class never initializes the kartaObrazok field, so getObrazok() returns null.

Sign up to request clarification or add additional context in comments.

5 Comments

it is in class 2 in for where i make new karta 32 times and to every single karta[0] , karta[1] ..... it get kartaObrazok = tempImageViewObrazokKarty also adding this.kartaObrazok = kartaObrazok; to class 3 doesnt fix it =\
But that is not the value returned by getObrazok(). The value returned by that method is the field kartaObrazok defined in the Karta class. That field is never assigned a value.
Also, you only initialize the temporary variable after you create the Karta instance. So on the first iteration of the loop you will be passing null as the parameter to the Karta constructor.
THX fixed it adding before Balik[] this 2 lines Image tempImgObrazokKarty = SwingFXUtils.toFXImage(tempObrazokKarty, null); tempImageViewObrazokKarty = new ImageView(tempImgObrazokKarty);
and is passible biuld .jar file with biuld artifacts ? cause its always close .jar file when i try open it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.