简体   繁体   中英

(JavaFX) How can I change a picture in an ImageView thats coded in an fxml file with a basic java class?

I am currently making a program, I want to have a button get clicked and it runs another class and depending on the output of that class, I want an image view on the same scene as the button that was clicked to be changed. I have everything coded up to the point where the output changes the image. How can I go about doing this?

Java Class

public static void USPSCase() {
        printUSPS();
    }

    public static void printUSPS(){
        Random rand = new Random();
        int gunSelect = rand.nextInt(99)+1;

        if(gunSelect<=30){ //Calculates which gun you will get
            LeadConduitUSPS(CaseSpinners.USPSCaseSpinController.setUSPImage);
        }
        else if(gunSelect>=31 && gunSelect<=60){
            NightOpsUSPS();
        }
        else if(gunSelect>=61 && gunSelect<=90){
            TorqueUSPS();
        }
        else if(gunSelect>=91 && gunSelect<=93.5){
            GuardianUSPS();
        }
        else if(gunSelect>=94.5 && gunSelect<=97){
            CyrexUSPS();
        }
        else if(gunSelect>=98 && gunSelect<=99){
            CaimanUSPS();
        }
        else if(gunSelect==100){
            KillConfirmedUSPS();
        }


    }

//some code... these two methods are connected
                    public static void LeadConduitUSPS(ImageView setUSPImage){
        System.out.println("Lead Conduit");

        Random rand = new Random();
        int wareSelect = rand.nextInt(99)+1;

if(wareSelect<=10){ //Calculates the ware the gun has
            setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
        }
        else if(wareSelect>=11 && wareSelect<=25){
            setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
        }
        else if(wareSelect>=26 && wareSelect<=60){
            setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
        }
        else if(wareSelect>=61 && wareSelect<=80){
            setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
        }
        else if(wareSelect>=81){
            setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
        }
    }

fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CaseSpinners.USPSCaseSpinController">
   <children>
      <ImageView fitHeight="400.0" fitWidth="600.0" pickOnBounds="true">
         <image>
            <Image url="@../csgocaseopener/back.png" />
         </image>
      </ImageView>
      <ImageView fx:id="spinmechback" fitHeight="45.0" fitWidth="45.0" onMouseClicked="#handleSpinMechBack" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="-1.0">
         <image>
            <Image url="@../csgocaseopener/backbtn.png" />
         </image>
      </ImageView>
      <Button fx:id="SpinUSPS" layoutX="235.0" layoutY="301.0" mnemonicParsing="false" onAction="#SpinUSPSCase" text="SPIN">
         <font>
            <Font name="System Bold" size="36.0" />
         </font>
      </Button>
      <ImageView fx:id="setAWPImage" fitHeight="200.0" fitWidth="200.0" layoutX="201.0" layoutY="100.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../csgocaseopener/bprof.png" />
         </image></ImageView>
   </children>
</AnchorPane>

fxml controller

public class USPSCaseSpinController implements Initializable {

        @FXML
        public static ImageView setUSPImage;

        @FXML
        private void handleSpinMechBack(MouseEvent event) throws IOException{
        Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
        Scene OPBackScene = new Scene(handleInventoryBackParent);
        Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        handleInventoryBackStage.setScene(OPBackScene);
        handleInventoryBackStage.show();
        }

        @FXML
        private void SpinUSPSCase(ActionEvent event) throws IOException{
            test test = new test();
            test.LeadConduitUSPS(setUSPImage);
        }
        @FXML
        public void SetUSPImage(){
            setUSPImage.setImage(new Image("AWPCase.png"));
        }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }   
}

First make parameterized method for LeadConduitUSPS() in your supporter class. Like this,

public static void LeadConduitUSPS(ImageView image){
    System.out.println("Lead Conduit");

    Random rand = new Random();
    int wareSelect = ((int)(rand.nextDouble() * 99)) +1;

    if(wareSelect<=10){ //Calculates the ware the gun has
        System.out.println("Factory New");
    }
    else if(wareSelect>=11 && wareSelect<=25){
        //Changes Image here!!
        image.setImage(new Image("new_image1.png");
    }
    else if(wareSelect>=26 && wareSelect<=60){
        //Changes Image here!!
        image.setImage(new Image("new_image2.png");
    }
    else if(wareSelect>=61 && wareSelect<=80){
        //Changes Image here!!
        image.setImage(new Image("new_image3.png");
    }
    else if(wareSelect>=81){
        //Changes Image here!!
        image.setImage(new Image("new_image4.png");
    }
}

Then call LeadConduitUSPS() method from the Controller class's SpinUSPSCase() ,

public class USPSCaseSpinController implements Initializable {

    @FXML
    private ImageView setUSPImage;

    @FXML
    private void handleSpinMechBack(MouseEvent event) throws IOException{
    Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
    Scene OPBackScene = new Scene(handleInventoryBackParent);
    Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    handleInventoryBackStage.setScene(OPBackScene);
    handleInventoryBackStage.show();
    }

    @FXML
    private void SpinUSPSCase(ActionEvent event) throws IOException{

        //call from here
        YourAnotherClass test = new YourAnotherClass();
        test.LeadConduitUSPS(USPImage);
    }

    @FXML
    public void SetUSPImage(){
        setUSPImage.setImage(new Image("AWPCase.png"));
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    } 
}

You can pass the ImageView instance (USPImage) which provided from FXML to your LeadConduitUSPS(ImageView imgView) method. Then you can set an Image to this imgView in your related class. Hope it is useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM