Jump to content

GUI Image problem


Recommended Posts

Posted

Hey everyone, 

 

It's me again with another question..

 

So i've given my GUI a new style. I like it but i want to add an image.

I have searched everywhere on the internetz but i cant find anything that works with OSBOT.

 

I'm pretty much clueless at the moment.

 

If someone can help me and give me a little hint. I don't need all the code but just a little so i can go on my way.

 

Thanks in advance,

 

Sebastian.

 

8Qy3lyd.png

Posted

Hey everyone, 

 

It's me again with another question..

 

So i've given my GUI a new style. I like it but i want to add an image.

I have searched everywhere on the internetz but i cant find anything that works with OSBOT.

 

I'm pretty much clueless at the moment.

 

If someone can help me and give me a little hint. I don't need all the code but just a little so i can go on my way.

 

Thanks in advance,

 

Sebastian.

 

8Qy3lyd.png

 

You could add a JLabel with an icon set like so:

JLabel jLabel = new JLabel();
try {
    Image image = ImageIO.read(YourClass.class.getResourceAsStream("/resources/image.png"));
    ImageIcon imageIcon = new ImageIcon(image);
    jLabel.setIcon(imageIcon);
} catch (IOException e) {
    e.printStackTrace();
}

Or you could add a custom JPanel with its paintComponent method overriden:

public final class ImagePanel extends JPanel {

    private final BufferedImage image;

    public ImagePanel(final String imagePath) {
        image = ImageIO.read(ImagePanel.class.getResourceAsStream(imagePath));
    }

    @[member=Override]
    public final void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
    }
}
  • Like 1
Posted

 

You could add a JLabel with an icon set like so:

JLabel jLabel = new JLabel();
try {
    Image image = ImageIO.read(YourClass.class.getResourceAsStream("/resources/image.png"));
    ImageIcon imageIcon = new ImageIcon(image);
    jLabel.setIcon(imageIcon);
} catch (IOException e) {
    e.printStackTrace();
}

Or you could add a custom JPanel with its paintComponent method overriden:

public final class ImagePanel extends JPanel {

    private final BufferedImage image;

    public ImagePanel(final String imagePath) {
        image = ImageIO.read(ImagePanel.class.getResourceAsStream(imagePath));
    }

    @[member='Override']
    public final void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
    }
}

 

Thank you, kind sir!

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