2

I am using eclipse and search around for tutorials but none of them helped me much because I would get errors if I tried those functions. I just wanna know what I am doing wrong or what should I use to get the code working. I Picture below is the final product, I just wanna add a picture and work towards adding text next. So for now All i want to know is adding an image using GUI features. Also, I tried the code below and doesn't show an image for some reason. Also I would like to know what i am doing wrong and how i can correct it.enter image description here

package Lab09Exercises;
import javax.swing.*;

public class Lab09Excercise extends JFrame {
JPanel jp = new JPanel();
JButton jb  = new JButton();


    public Lab09Excercise() {

        setTitle("Excercise2");
        setVisible(true);
        setSize(400,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        jb.setIcon(new ImageIcon("advertising.png"));
        jp.add(jb);
        add(jb);
        validate();
    }

    public static void main(String[] args) {


        Lab09Excercise a = new Lab09Excercise();
    }
}
[![the image is the result i want to get but i just wanana know how do add an image first before I can add rest of the components][1]][1]


  [1]: https://i.stack.imgur.com/QsvFp.png
geb
  • 43
  • 6

2 Answers2

0

Create your custom JPanel.

Look, create a class called ImagePanel and add this code, then just replace JPanel with ImagePanel

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
       }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);           
    }

}

Answered by Brendan Cashman How to add an image to a JPanel?

Also

private void DisplayImage(JPanel jp, String url) {
    JLabel jl=new JLabel();
    jl.setIcon(new javax.swing.ImageIcon(getClass().getResource(url)));
    jp.add(jl);
}

Answered by FiroKun How can I display an image in a JPanel

Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • can you explain why replace JPanel with ImagePanel and why I have to create custom class?, kinda new to programming so would like to know why you choose that method – geb Mar 23 '18 at 20:30
  • @geb I have given you two options, the first one creates a panel ready to show images when painting, but you can use the second, it is a method that is added to an image from a route to the JPanel – Héctor M. Mar 23 '18 at 20:38
  • The thing is that I already have an image and want to use that directly with JPanel, also if I use the second method I get an error which is "cannot make a static reference to a non-static class" , for that do i have to create a seperate class? – geb Mar 23 '18 at 20:42
  • @geb If your class is not static, then remove static keyword, I just edit my post, copy the code of the second option and try again – Héctor M. Mar 23 '18 at 20:49
  • I tried it and it works but the image wont show, how do i read from my eclipse package since my pictures are located there – geb Mar 23 '18 at 21:03
  • @geb Can you copy the route in the folder browser and show it to me? – Héctor M. Mar 23 '18 at 21:05
  • @geb Tell me in what folder your images are, so I can write a code that suits your project. – Héctor M. Mar 23 '18 at 21:10
  • Using a virtual machine to run eclipse, don't know where to find the director for it – geb Mar 23 '18 at 21:17
  • @geb Mmmmm, In that case, you must programmatically obtain the path of your project and concatenate it with the name of your image, see this post: [Get project path in Java](https://stackoverflow.com/questions/13011892/how-to-locate-the-path-of-the-current-project-in-java-eclipse) – Héctor M. Mar 23 '18 at 21:21
0

You can use JLabel to display images. It is probably more convenient to use because you can also display text with it and manipulate as any other component.

ImageIcon imageIcon = new ImageIcon("path/to/image.jpg");
JLabel label = new JLabel("Sample text", imageIcon, JLabel.CENTER);
SystemGlitch
  • 2,150
  • 12
  • 27
  • it works but i have no idea on reading the path for the image because of virtual machine – geb Mar 23 '18 at 22:48
  • If you want to get an image from inside your Jar, refer to [this question](https://stackoverflow.com/a/45580/9524541). Else, paths work the same inside a VM. Where is your image located? – SystemGlitch Mar 24 '18 at 08:57