0

how can I return card cc from lambda mouse click?
I'm waiting from user's input from mouse. imgRuka0 is an imageView.
Is there better way to do it without lamba? This class is from Interface. I guess this isn't optimal way to do it, so please, teach me.
Thanks for any suggestion.

@Override
public card turn() {

    imgRuka0.setOnMouseClicked(e -> {
        String s =""+ e.getSource().toString(); //getting mouse event
        System.out.println(s); 
        s =""+s.substring(20,21);//taking number
        int i = Integer.parseInt(s); //parsing number
        System.out.println(s);
        card cc = karticky.get(i); //from arraylist<card> taking card
    });
    return null;
}

EDIT: Is this better? Will it wait for user's input?

@Override
    public card turn() {

        imgRuka0.setOnMouseClicked(e -> {
            String s =""+ e.getSource().toString();
            System.out.println(s);
            s =""+s.substring(20,21);
            vyhazujuKartu = Integer.parseInt(s);
            vyhazujuKartuC = karticky.get(vyhazujuKartu);
        });
        imgRuka1.setOnMouseClicked(e -> {
            String s =""+ e.getSource().toString();
            System.out.println(s);
            s =""+s.substring(20,21);
            vyhazujuKartu = Integer.parseInt(s);
            vyhazujuKartuC = karticky.get(vyhazujuKartu);
        });


        return vyhazujuKartuC;
    }
  • What do you mean by "without lambda"? – Hovercraft Full Of Eels Dec 15 '18 at 13:42
  • Also your code looks to be as if it were written for a linear console program and not a GUI as it appears to be working against the event-driven model of the GUI library. The solution is likely that you don't have a method `turn()` that returns a card object (which should be renamed "Card"), but rather base the program's response to events, here a mouse click on the *state* of the program, here the state being whose turn it is. – Hovercraft Full Of Eels Dec 15 '18 at 13:43
  • 1
    unrelated to your problem: please learn java naming conventions and stick to them – kleopatra Dec 15 '18 at 13:50
  • Basically i need to find out, what user clicked on (ImageView0-9), i just need return object card and if i find out, which ImageView he clicked, im able to return that exact object. I'm sorry for bad english and bad java experience. Is there any other way? I'm just asking how do i recognize what user clicked on. Thanks. – Zdeněk Krast Dec 15 '18 at 13:55
  • 3
    Again you need to re-structure your entire program as this is not a linear console program. Read up on event-driven programming concepts because this is what you need for this program to succeed. For instance, you don't add listeners within a turn method. And in fact all a `turn()` method should do is to change the state of the game so that it behaves correctly to a user's mouse press. – Hovercraft Full Of Eels Dec 15 '18 at 14:14
  • The question in the link in the comment above shows some Swing code, but the general concepts in the question, and more importantly in the answer, are the same. – Hovercraft Full Of Eels Dec 15 '18 at 14:26
  • @kleopatra: good to see you again. If possible, please lend your thoughts to the answer posted below. – Hovercraft Full Of Eels Dec 15 '18 at 14:56
  • 1
    @HovercraftFullOfEels :)) nothing special to fx .. the general design is the same – kleopatra Dec 15 '18 at 15:43
  • 1
    don't think "wait" - the gui does nearly nothing else but wait ;) Instead have something like a GameModel that has state which can be changed by the listeners (better: higher level ui elements like buttons that can be activated by either mouse, keyboard or whatever) - the model specifies which of the changes is allowed of any point in time, driving the enablement state of the ui elements. – kleopatra Dec 15 '18 at 15:52

0 Answers0