Jump to content

BufferedImage returning null


Spills

Recommended Posts

For some reason the BufferedImage that I want to paint is constantly returning null even through it's not throwing any exceptions when I read it.

public class SpillsFisher extends Script {

   private BufferedImage background;

   public final void onStart() throws InterruptedException {

          try {
              background = ImageIO.read(getScriptResourceAsStream("resources/background.png"));
              log("No exceptions");
          } catch (IOException e) {
              log(e.fillInStackTrace());
          }
      }

   public void onPaint(Graphics2D g) {

          if(background != null){
              g.drawImage(background, null, 10, 10);
              log("Not null");
          } else {
              log("is null"); <-- it's always returning null here
          }
	}

Structure within IDE

F1UI8Zz.png

Structure within 7zip (It has the resources folder and the image inside)

GUjOzmt.png

I was using this post as a point of reference:

I've also tried:

background = ImageIO.read(SpillsFisher.class.getResourceAsStream("/resources/background.png"));

From

Having the same issue either way, any ideas?

Link to comment
Share on other sites

What I do is fetch the image from an url like imgur :)
Save it in the Osbot/data folder and load it in next time, if it's not there download it again.
 

    public static BufferedImage getImage(Script script, String url) {
        try {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            script.log("Failed to load image with url: " + url);
        }
        return null;
    }

 

  • Like 1
Link to comment
Share on other sites

3 hours ago, Khaleesi said:

What I do is fetch the image from an url like imgur :)
Save it in the Osbot/data folder and load it in next time, if it's not there download it again.
 

    public static BufferedImage getImage(Script script, String url) {
        try {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            script.log("Failed to load image with url: " + url);
        }
        return null;
    }

 

Ahah! I appreciate the response, funnily enough I was thinking about doing it this way, thank you!

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...