简体   繁体   中英

Insert image in resources folder Java

I have a database WAMP in which, despite other fields, I can insert the name followed by the extension of an image, for instance, "imagen1.jpg" . The program I am doing is written in Java and SQL for the sentences and I'm using the IDE of Netbeans to do it. I have a type called "Imagenes.java" in which I pick the route of an image to, in this way, insert it in my Jpanel or Jlabel .

public class Imagenes extends javax.swing.JPanel {
    String path;
    ImageIcon imag;

    public Imagenes(int width,int height,String path)
    {    
        this.path=path;
        this.setSize(width,height);        
    }

    @Override
    public void paint(Graphics graph)
    {
        Dimension tam = getSize();
        if (imag!=null)
        graph.drawImage(imag.getImage(),0,0,tam.width, tam.height, null); 
        else
        {
            ImageIcon imagenFondo = new ImageIcon(getClass().getResource(path));        
            graph.drawImage(imagenFondo.getImage(),0,0,tam.width, tam.height, null);        
        }
        setOpaque(false);
        super.paintComponent(graph);
    }

}

All the images I've inserted are already saved in a folder called "resources" . Therefore, I want to add to the "resources" folder, images from the computer I pick from a "fileChooser" . I've tried it in a thousand ways, putting the complete route and there is no way for the folder to being recognized. However, it copies the images in the source folder. I've tried type of routes like '\\\\resources\\\\imagen1.jpg' , Java specific, but It doesn't pick it properly. But If I do a print of the string of the route, this is correct. What I'm doing wrong?

If you used ImageIcon(path) instead of ImageIcon(getClass().getResource(path)) . You may want to try "src/resources/imagen1.jpg" . Netbeans will search from the ProjectRoot. So just typing "resources/imagen1.jpg" , it wont be found. You need to add the src

ProjectRootDir
            src
               resources
                      imagen1.jpg

When you use ImageIcon(getClass().getResource(path)); The program will try and locate the Image from the classes dir in the build dir. If you place the resources dir in the classes dir, using ImageIcon(getClass().getResource(path)) should work

ProjectRootDir
            build
                classes
                     resources
                             imagen1.jpg

In the above scenario, your path would be "resources/imagen.jpg"

How could I use this to save in resources folder?

    // declare JFileChooser
JFileChooser fileChooser = new JFileChooser();

// let the user choose the destination file
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
    // indicates whether the user still wants to export the settings
    boolean doExport = true;

    // indicates whether to override an already existing file
    boolean overrideExistingFile = false;

    // get destination file
    File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());

    // check if file already exists
    while (doExport && destinationFile.exists() && !overrideExistingFile) {
        // let the user decide whether to override the existing file
        overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);

        // let the user choose another file if the existing file shall not be overridden
        if (!overrideExistingFile) {
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                // get new destination file
                destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
            } else {
                // seems like the user does not want to export the settings any longer
                doExport = false;
            }
        }
    }

    // perform the actual export
    if (doExport) {
        // the code to write the file goes here...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM