Image Help!

+1 Landon Luman · October 5, 2014
So I've been trying to be able to display images in Java, but I just can't seem to do it! I don't know if it is a library problem, or a code bug. But please help me if you can guys...

-Landon

Post a Reply

Replies

Oldest  Newest  Rating
+1 Alex Sweps · October 5, 2014
This seems like what you are looking for:

http://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel
+1 Landon Luman · October 5, 2014
Thanks John and Alex, you should get credits for helping so many people in Java
0 Alex Sweps · October 5, 2014
Post your code that you have tried so far. And do you mean images like graphics, quares and circles etc or actual jpgs ?
0 Landon Luman · October 5, 2014
I mean like, being able to put an image onto a JFrame or JPanel, if I could just have an example of an image code, please
0 Developer John · October 7, 2014
Sorry, I forgot to also state that once you use that code, you can use them for things like JComboBox. Here is the message again but more simplified for you to understand:
private static String[] filename = {"pic1.jpg", "pic2.jpg", "pic3.jpg"};
//Here I create an array that stores the picture names

    private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1])), new ImageIcon(getClass().getResource(filename[2]))};
//Here is where the pictures are stored
//That long line of code though ;D

box = new JComboBox(filename);
        box.addItemListener(
                new ItemListener(){
                    public void itemStateChanged(ItemEvent event){
                        if(event.getStateChange()==ItemEvent.SELECTED)
                            picture.setIcon(pics[box.getSelectedIndex()]);
                        
                    }
                }
        );
      
//This is my anonymous class to handle the picture events


        add(box);
        picture = new JLabel(pics[0]);
        add(picture);
//And lastly, this is where I add everything to the screen

The tutorial you really want to follow would be tutorial #68. When importing pictures, it really depends on where they will be used. I have deleted my old post, so hopefully you can see this new one. Remember, if your pictures don't show up when you add them to the Eclipse project folder of your choice, try to refresh the project folder in Eclipse.8-)

-Oh yea, and also remember this is not properly indented for Java use.
  • 1

Java / Android Development

106,989 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator