0

I have this method and I draw text. I would like to change this and if I call my method it should be draw not a text but it should display an image. How to code this? Below code for a firstimage doesn't work. It displays: "Cannot resolve symbol "firstimage". But it works fine if I code the same line below for example: public class SHOWYOURTEXT extends Application {

It works for text.

private void drawTEXT(){
           text.setText("TEXT");
           private firstimage = new Image("file:src/main/resources/img/TEXT.png");
              }

1 Answers1

0

Text class doesn't has graphic attribute you can change it to Label

private void drawTEXT(){
       label.setText("TEXT");
       //Passing FileInputStream object as a parameter 
       FileInputStream inputstream = new FileInputStream("C:\\images\\image.jpg"); 
       Image firstimage= new Image(inputstream); 
       label.setGraphic(new ImageView(firstimage));
          }

for platform independency you can use Path class

    Path path= Path.of("src","resources","Text.gif");
    InputStream inputstream = Files.newInputStream(path);

My project structure is

enter image description here

You can either directly specify x coordinate and y coordinate

For more references check this out https://www.tutorialspoint.com/javafx/javafx_images.htm https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html

vikalp rusia
  • 95
  • 1
  • 11