-1

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kemenesendre
  • 165
  • 1
  • 1
  • 11
  • 1
    *"cause I have read some ideas"* Prove it by linking to them, and explaining what it is you do not understand! This just seems like the typical statement of someone who has done 0 research and expects us to spoon-feed the information to them, for the millionth time.. :( – Andrew Thompson Jan 02 '14 at 10:15
  • BTW - Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 02 '14 at 10:16
  • Is this question about colors or images? So far two people have presumed 'color' though the title states 'image'. – Andrew Thompson Jan 02 '14 at 10:33
  • About images. With colors is obvious. There is put a color, just to see if setBackground is working, but instead of that I would like an image. – kemenesendre Jan 02 '14 at 10:47
  • Tried it adding a try-catch statement and with BufferedImage, added the path, it throws a NullPointerException. Tried with inner classes as some answers were here on this site, does not happen anything. Tried it with labels: nothing happening. – kemenesendre Jan 02 '14 at 10:55
  • @rafanadal I actually thought that it was pretty obvious you meant 'image'. I cannot account for two people presuming 'color' except that the code did set a color. Maybe you should remove that line for the moment. – Andrew Thompson Jan 02 '14 at 10:56
  • *"Tried it adding.."* That is good and useful information that should be [edited into the question](http://stackoverflow.com/posts/20880587/edit). – Andrew Thompson Jan 02 '14 at 10:57
  • Edited with my last bit of code I am just trying. – kemenesendre Jan 02 '14 at 11:08

1 Answers1

1

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);
}
Marenthyu
  • 165
  • 1
  • 12
  • 1
    *"you have to use URL not File."* +1 This BG image is apparently an embedded resource (it will be in a Jar), and will not be available as a `File`. Or a longer way to say that is: By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Jan 02 '14 at 11:14
  • True, I didn't consider that. Changed to URL, but still throws my NullPointerException at this line: private JLabel picLabel = new JLabel(new ImageIcon(myPicture)); – kemenesendre Jan 02 '14 at 11:27
  • that is because myPicture is not initialised yet. you have to call that line after the try-catch Block, then it should work as intended. - or add default image as a placeholder and then simply call private BufferedImage myPicture = new BufferedImage(placeholder); instead of only private BufferedImage myPicture; – Marenthyu Jan 02 '14 at 11:30
  • 1
    Thanks for your help for both of you. Finally this site will teach me some programming, instead of my professors. :) – kemenesendre Jan 02 '14 at 11:48