Jump to content

Load Pictures from Resources to GUI


Recommended Posts

Posted

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();
      }

}

 

Posted

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
Posted
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
Posted

@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;
    }
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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