Domanda

I need this for implementing the secret maze mini-game and I am unable to set a background image for a JPanel. It has to be, strictly, JPanel. In addition it would be good to be solved via an URL. If you can give me some, not just ideas I would appreciate it, cause I have read some ideas, but they weren't working or at least I didn't succeed in implementing them.

I have the following code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PanelLooks extends JFrame
{
    JPanel content = new JPanel();
    private JButton NewGameBtn = new JButton("New Game");
    private JButton TopTimesBtn = new JButton("Top Times");
    private JButton QuitGameBtn = new JButton("Quit Game");
    public PanelLooks() {
        setPreferredSize(new Dimension(600, 600));
        content.setLayout(new FlowLayout());
        content.add(NewGameBtn);
        content.add(TopTimesBtn);
        content.add(QuitGameBtn);
        NewGameBtn.setBounds(250, 160, 100, 30);
        TopTimesBtn.setBounds(250, 260, 100, 30);
        QuitGameBtn.setBounds(250, 360, 100, 30);
        this.setContentPane(content);
        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("Secret Maze");
   }

The latest version after editing contains an attempt that fails because of a NullPointerException:

private BufferedImage myPicture;
    private JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    public void backgroundImage()
    {
        try
        {
            BufferedImage myPicture = ImageIO.read(new File("D:/Dokumentumok/Egyetem/Object oriented programming/Java project/Project/Background.jpg"));
        }
        catch(IOException ex)
        {
            System.err.println("File not found!");
        }
    }

and this picLabel is called in the constructor as the others were by:

content.add(picLabel);
È stato utile?

Soluzione

I had something to get a local file and lookmed around a little bit and this is working for me:

    public static void changeIcon2(URL adress,JLabel imageLabel) throws JavaLayerException, IOException, InterruptedException {
    imageLabel.setVisible(false);
    BufferedImage temp = ImageIO.read(adress);

    imageLabel.setIcon(new ImageIcon(temp));

    imageLabel.setVisible(true);
}

Simply call it in you Code where you need it.

So - your problem (i ran into it, too) you have to use URL not File.

But what i am using in my Program to read local files (it is a bit specific for my project):

    public static void changeIcon(String championname,JLabel imageLabel) throws JavaLayerException, IOException, InterruptedException {
    imageLabel.setVisible(false);
    ImageIcon temp = new ImageIcon(getLolPath()+"\\League of Legends\\rads\\projects\\lol_air_client\\releases\\"+currentVersion+"\\deploy\\assets\\images\\champions\\"+championname+"_Square_0.png");

    imageLabel.setIcon(temp);

    imageLabel.setVisible(true);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top