【问题标题】:I need to compare different image values after they've been clicked点击后我需要比较不同的图像值
【发布时间】:2018-08-26 03:26:42
【问题描述】:

我没有找到任何关于我的困境的好问题。我正在尝试计算/检查通过 actionListener 单击特定项目的次数,但不确定如何使用 actionListener/EventHandler 来记录单击。这是在 JavaFX 中完成的。请原谅这个可能很愚蠢的问题。

        correctUrl = getCorrectUrl(); //Correct image for set
        wrongUrl = getWrongUrl();
        initialPos(); //everything is placed into default.

       imageOne.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
           imageOne.setOnMouseClicked((MouseEvent e) -> {
               imageOne = flipToCard(imageOne, 1);
           });
            returnCardOne();
            System.out.println("Tile pressed ");
            event.consume();
        });

代码的最终目标是注意两个图像已被点击,然后检查它们的 URL 是否 == 彼此。

【问题讨论】:

  • 我认为您需要详细说明您的问题。例如,没有关于这些 URL 如何与图像相关联的信息。
  • 像这样注册一个监听器通常是个坏主意。第一次单击imageOne 时,会设置一个新的事件处理程序。从第二次单击开始,执行 both 事件处理程序。我看不出这会是预期效果的原因。
  • 网址是每个图像位置的字符串,我很抱歉我不小心添加了它们。

标签: java javafx imageview listener conditional-statements


【解决方案1】:

如果URLs 相等,则Images 应该相等。那么为什么不比较Images呢?这个小应用程序演示了您要实现的目标。代码中的注释。

主要:

import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication51 extends Application {

    @Override
    public void start(Stage primaryStage) {   
        List<ImageViewTile> imageViews = new ArrayList();
        List<ImageViewTile> cardsBeingMatched = new ArrayList();//Used to keep up with the current two cards that are pressed inorder to deteremine if they are a match
        Image initialImage = new Image(getClass().getResourceAsStream("cardBack_blue1.png"));
        Image ace = new Image(getClass().getResourceAsStream("cardSpadesA.png"));
        Image king = new Image(getClass().getResourceAsStream("cardSpadesK.png"));
        imageViews.add(new ImageViewTile(initialImage, ace));
        imageViews.add(new ImageViewTile(initialImage, king));
        imageViews.add(new ImageViewTile(initialImage, ace));
        imageViews.add(new ImageViewTile(initialImage, king));

        for(ImageViewTile view : imageViews)
        {
            //Set ImageViews' onMouseClicked handler
            view.setOnMouseClicked(event->{

                if(!view.isShowing())//If card face value is not showing 
                {
                    view.showFrontImage();//show it.
                    cardsBeingMatched.add(view);//Add card being clicked to list so it can be compared against the next card
                    if(cardsBeingMatched.size() == 2)//Once two cards are in the list, see if they are equal.
                    {
                        if(cardsBeingMatched.get(0).getImage().equals(cardsBeingMatched.get(1).getImage()))//If cards are equal a match is found
                        {
                            System.out.println("Match");
                            cardsBeingMatched.clear();//clear the list to prepare to compare the next two cards that are clicked
                        }
                        else//If cards are not equal
                        {
                            System.out.println("No match");
                            //wait half a second and flip cards back over
                            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.5), (event1) -> {
                                System.out.println(".5 seconds need to pass before another mouse click!");
                            }));
                            timeline.setCycleCount(1);
                            timeline.play();
                            timeline.setOnFinished(event1->{
                                cardsBeingMatched.get(0).showBackImage();
                                cardsBeingMatched.get(1).showBackImage(); 
                                cardsBeingMatched.clear();//clear the list to prepare to compare the next two cards that are clicked
                            });                      
                        }
                    }                        
                }                
            });
        }

        GridPane root = new GridPane();
        for(int i = 0; i < imageViews.size(); i++)
        {
            root.add(imageViews.get(i), i % 2, i  / 2);//Add ImageViews to the GridPane
        }


        Scene scene = new Scene(root, 280, 380);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

ImageViewTitle 类:

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author Sedrick
 */
public final class ImageViewTile extends  ImageView{
    private boolean showing;
    private final Image backImage;
    private final Image frontImage;

    public ImageViewTile(Image backImage, Image frontImage) {
        this.backImage = backImage;
        this.frontImage = frontImage;

        this.setImage(backImage);
        showing = false;
    }

    public void showBackImage()
    {
        this.setImage(backImage);
        showing = false;
    }

    public void showFrontImage()
    {
        this.setImage(frontImage);
        showing = true;
    }    

    public boolean isShowing()
    {
        return showing;
    }
}

我创建了这个类来帮助我跟上ImageView's 的不同状态。

【讨论】:

  • 很遗憾,您的代码在我尝试测试时无法正常工作。 :( 我仍然会剖析它并评估我如何自己实现它。
  • 使用 3 个不同的图像。将它们放在这两个 .java 文件所在的同一文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多