Jump to content

Java FX Help

Drackeo

Hey for a class that im in i have to create  a program that plays Blackjack or 21, whatever you call it. And i have no idea on how to initialize the deck on a button click and how to make it shuffle said deck on a button press. Lovely project concidering we have only been at this for like 3 classes.... 

Help pllease. Ill post what i have below! 

ProjectBlackJack.txt

Consume The Darkness

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure which part you're having trouble with so I'll cover two things

 

1. Initializing a class and calling a method

// Using the following classpublic class Example {    public Example() {}    public void someMethod() {}}// You initialize the class like soExample ex = new Example();// You call a method in the class like soex.someMethod();

2. Running code in a button click

public void start(Stage primaryStage) throws Exception{    // ...    Button btn1 = new Button("...");    btn1.setOnAction((clicked) -> {        // Code to run goes here between the curly brackets        Example ex = new Example();        ex.someMethod();     });    // ...}

Also note that you can define the object outside the click event and still use it. You'll need to do this if using the object outside of the button click event.

private Example ex;public void start(Stage primaryStage) throws Exception{    // ...    ex = new Example();    Button btn1 = new Button("...");    btn1.setOnAction((clicked) -> {        ex.someMethod()    });    // ...} 

With that you should be able to figure out how to get the button click to create and shuffle the deck.

Link to comment
Share on other sites

Link to post
Share on other sites

So using what you gave me i ended up getting this

 

btn1.setOnAction((clicked) -> {
       for(int i = 0; i<this.deck.length; i++){
            int j = (int)(Math.random()*this.deck.length);
            int temp = this.deck;
            this.deck = this.deck[j];
            this.deck[j] = temp;
       }
       this.next_card = 0;
});
 
and also able to intitalize a deck at the begining.
But when ever i try and use that button im getting a massive string of error messages. idk why
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at ProjectBlakcJack.lambda$2(ProjectBlakcJack.java:53)
at ProjectBlakcJack$$Lambda$72/1962811315.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Consume The Darkness

Link to comment
Share on other sites

Link to post
Share on other sites

- snip -

 

It's because you took the code outside of the DeckOfCards class. Since you've done that, this.deck and this.next_card no longer work. They were referring to the private members of the DeckOfCards class.

public class DeckOfCards {	    private int[] deck;    private int next_card = 0;    // ...}

Since these are private, they aren't available to you outside the DeckOfCards class. The button click event can't access them so it's throwing an error.

 

To run that code you don't take it out of the class, you call the shuffle method on an initialized DeckOfCards object. Just like I showed when I called the someMethod method on the initialized Example object.

Link to comment
Share on other sites

Link to post
Share on other sites

I have it working no! Thank you so much man!!!!!!

Consume The Darkness

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×