Jump to content

Loading any Tradeable item image onto a GUI


Recommended Posts

Posted (edited)

I made this for fun and I really don't plan on using it. I'm gonna release it. Feel free to use it or don't. I don't care doge.png

 

Also the code isn't that clean.

 

What it does:

 

https://gyazo.com/442cf002eb683ed0343f6f62f79e2d15

 

 

Code (Note* the first item search may lag a tiny bit due to loading the text document from online. An easy fix is to load the text document locally):

 

package org.empathy.combat.equipment.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Gui extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private int itemId;
    String path = "http://cdn.rsbuddy.com/items/" + itemId + ".png";
    JLabel lblNewLabel;
    BufferedImage image;
    private JLabel lblTypeAnItem;
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Gui frame = new Gui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Gui() {
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 340, 382);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        textField = new JTextField();
        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                checkText();                
                
            }
            
        });
        textField.setBounds(118, 49, 86, 20);
        contentPane.add(textField);
        textField.setColumns(10);
        
        try {

        image = ImageIO.read(new URL(path));
        lblNewLabel = new JLabel();
        lblNewLabel.setBounds(118, 92, 86, 73);
        contentPane.add(lblNewLabel);
        
        lblTypeAnItem = new JLabel("Type an item name and hit Enter");
        lblTypeAnItem.setBounds(80, 28, 208, 14);
        contentPane.add(lblTypeAnItem);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void checkText() {
        if (textField.getText() != null) {
            try {
                URL url = new URL("https://gist.githubusercontent.com/anonymous/a1aa9894c1acc0c1c201/raw/b8bd35ed57a9cedfb1dc2379212ac79faa979136/IDs.txt");
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    if (line.toLowerCase().contains(textField.getText().toLowerCase())) {
                        String[] x = line.toLowerCase().split(textField.getText().toLowerCase() + " - ");
                        int i = Integer.parseInt(x[1]);
                        image = ImageIO.read(new URL("http://cdn.rsbuddy.com/items/" + i + ".png"));
                        lblNewLabel.setIcon(new ImageIcon(image));
                        break;
                        }
                    }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Edited by empathy
  • Like 4

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