1

this is the code that i wrote, it didn't result in no image output the panel was just empty my goal is to have a frame with 8 panels with each panel containing a picture

      ImageIcon image1 = new ImageIcon("kenya.png");
       Image ken = image1.getImage();   
       Image kenya = ken.getScaledInstance(200,100,java.awt.Image.SCALE_FAST);
       image1 = new ImageIcon(kenya);
       
     JFrame frame = new JFrame("Ticket airline system");
    frame.setVisible(true);
    frame.setSize(1800,1000);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    panel.setBackground(new Color(200,250,255)); 
    panel.setSize(1800,1000);
    panel.setLayout(null);
    panel.setBounds (0,0,1800,1000); 
    JPanel panel1 = new JPanel();
    panel1.setBounds(0,140,300,280);
    panel1.setLayout(null);
    JLabel label1 = new JLabel();
    label1.setSize(200,180);
    
    label1.setIcon(image1); 
    panel1.add(label1);
    panel.add(panel1);
   frame.add(panel);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • `panel.setLayout(null);` is not helping your cause. – MadProgrammer Dec 13 '21 at 20:36
  • @MadProgrammer trust me it is haha, i tried to get rid of it and the output went mad – just_a_beginner Dec 13 '21 at 20:45
  • 2
    Trust me (said some stranger on the internet ), it's not you helping in the slightest, the fact is you simply don't understand how the layout manager system in Swing works. See [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details – MadProgrammer Dec 13 '21 at 20:59

1 Answers1

2

null or "pixel perfect" layouts are an illusion. There are so many factors which go into determining the best size/position and relationship of components, you'd spend a live time trying to make it work properly ever time on every platform.

Instead, make use of all the work which has already been done, see Laying Out Components Within a Container for more details.

ImageIcon(String) is expecting to find a file on the disk at the location you've specified. Since you're using a relative path, this will be offset from the "working" location of the app (that is, the directory is was launched from).

In cases where the image "doesn't appear to be loading", I'd consider using something like System.out.println(new File("kenya.png").exists());, this will give you a good indication whether the issue is with the image file itself or the image file can't be found within the current context.

If you want to know what the current working directory is, you can use something like System.out.println(System.getProperty("user.dir"));

ImageIcon will also attempt to load the image in a background thread, meaning that if it fails, it will do so silently. For this (and a few other) reasons, I prefer to use ImageIO.read, see Reading/Loading an Image

Now, one way to overcome this issue with external files, is to "embed" them within the application context directly. Generally this is done by having them included in the resulting Jar. Depending you build system and IDE, you should be able to add images to a "location" within the project, which will make them easily available during development and automatically include them in the Jar when it's exported.

In Netbeans (using an Ant based project), you can simply drop the resources into the src directory.

Once they are "embedded", you can no longer treat them as files, instead, you need to make use of Class#getResource or Class#getResourceAsStream depending on your needs

For example...

enter image description here

BYO own images (I shouldn't have to say this, but you'd be surprised).

I've also deliberately set the number of columns to 3 so that the "highlighting" shows up better.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new GridLayout(-1, 3));
            for (int index = 0; index < 8; index++) {
                BufferedImage img = ImageIO.read(getClass().getResource("/images/Cute" + (index + 1) + ".png"));
                JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 50, Image.SCALE_SMOOTH)));
                JPanel panel = new JPanel(new GridBagLayout());
                panel.add(label);
                if (index % 2 == 0) {
                    panel.setBackground(Color.DARK_GRAY);
                }
                add(panel);
            }
        }

    }
}

Now, if you want the images to be selectable, then you should consider using a JList instead, this can support columns and rows (it defaults to a single column)

If you prefer to have the images listed in a single column (for the above example), then change the number of columns the GridLayout is using, much simpler then trying to manually recalculate the positions yourself

Image#setScaledInstance is neither efficient or pretty, see

for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • the output of System.out.println(new File("kenya.png").exists()); is false . please tell me what this means and how to fix it – just_a_beginner Dec 13 '21 at 21:09
  • ps : the pictures are all pasted in my project src folder – just_a_beginner Dec 13 '21 at 21:10
  • *"please tell me what this means and how to fix it"* - I did, I explained about embedded resources. The "how" will come down to your IDE. In my test project, the images are located in `src/images`, obviously `src` won't exist at runtime, so the offset path become `/images` – MadProgrammer Dec 13 '21 at 21:12
  • Mad programmer thank you very much for your patience and help . I'll try to apply what you just told me – just_a_beginner Dec 13 '21 at 21:14
  • Embedded resources can take some time and effort to get your head around, but once you do, they will solve so many issues – MadProgrammer Dec 13 '21 at 21:25