Jump to content
Sign in to follow this  
Ace99

Load Pictures from Resources to GUI

Recommended Posts

I know the first post on this page is pinned and says how to use the Resource folder... but  I am still stuck.  I also know there is a Data folder but I don't want that unless I have to.  I can make my GUI run as a stand alone JAR without OSBot and images load from resources, but once the client tries to do it the pics are null.  It is about 14 pics so I do not want to pull from imgur.  Checking the JAR in the OSBot/Scripts folder I see the resources are built into the JAR.

Folder structure:

Script.jar

-->resources/

      -->images.png

-->src/

      -->Main

      -->GUI

Code in GUI:

public ImageIcon pic;

public class GUI{

  public void setImages(){
      this.pic = new ImageIcon(getClass().getResource("images.png")); **** error on this line in GUI *****
  }

}

   Code in Main:

...

public void onStart() {
	GUI gui = new GUI();
      try {
      gui.setImages(); ***** error on this line in main *****
      } catch (IOException e) {
      e.printStackTrace();
      }
      try {
      gui.GUI();
      } catch (IOException e) {
      e.printStackTrace();
      }

}

 

Share this post


Link to post
Share on other sites

Script getScriptResourceAsStream - This loads resources relative to the entry of the script
Script getDataResourceAsStream - This loads resources relative to the entry of the OSBot data directory

Here are two examples so you get an idea:

 

ztfC9pq.png

 

x5gt9d6.png

 

Of course please ensure you are still following the existing SDN resources folder rules. 

https://osbot.org/forum/topic/140109-osbot-254-resource-loading/

  • Heart 1

Share this post


Link to post
Share on other sites
7 minutes ago, Chris said:

this is how I read in my plugin

 


this.image = ImageIO.read(script.getScriptResourceAsStream("resources/" + imgName));

 

But the JLabel requires an ImageIcon not just BufferedImage or Image?

Share this post


Link to post
Share on other sites
24 minutes ago, Chris said:

My other comment just shows the loading of resources. you can read how you want

Yeah thanks I saw that after I replied.  I had my folder structure off for sure.  I don't get null error anymore so I think it found the image and I have JLabel issues to work out.

Much appreciated!

  • Like 1

Share this post


Link to post
Share on other sites

@Chris I am still getting errors and have been stuck for hours! Shame its a GUI error of all things to be stuck on.  Ill put my file structure, code, and error if you have time to review (or anyone)! I have been tunnel visioning this so hard I may be missing something obvious.

 

image.png.9e69336e2d69c24bbb4761dd1eb0e2d2.pngimage.png.314956b263d609d23fe302872feae2c0.png

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;

import java.io.IOException;


@ScriptManifest(author = "Ace99", name = "Test GUI", logo = "", version = 1.0 , info = "")

public class Main extends Script {

    Image guam;

    @Override
    public void onStart() throws InterruptedException {

        try {
            this.guam = ImageIO.read(getScriptResourceAsStream("resources/Guam_potion_(unf).png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JFrame frame = new JFrame("Does this work?" );
        ImageIcon guamPic = new ImageIcon(guam);
        JLabel picture = new JLabel();
        picture.setIcon(guamPic);
        picture.setBounds(10,10,200,200);
        frame.add(picture);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setSize(430, 300);
        frame.setVisible(true);

         /*
        if (guam != null) {
            log("found it");
        } else {
            log("waiting around");
            sleep(15000);
        }
         */

        super.onStart();
    }

    @Override
    public int onLoop() throws InterruptedException {


        return 500;
    }
}

 

Share this post


Link to post
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.

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...