Jump to content

GUI Image problem


Sebastian

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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!

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