• 0

[JAVA] JPanel Background


Question

I've been working on this for an hour, with no success at all. I have this image which is 900x600, the same with as my window. I'm simply trying to set it as the background for my JPanel window.

http://faq.javaranch.com/java/BackgroundImageOnJPanel

I've tried that site with no success at all. It's been awhile since I've worked with Java, so this small project is a way for me to jump back into it. I've tried copying the code into the main code, but nothing happened when I ran. (However, it did compile without any issues). Looking at the code, and seeing it call the Super Paint Component, I then tried to put it in as a subclass, but that won't work either, so I reverted back into inserting all the code into the same file.

Here's my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class Play extends JPanel implements ActionListener
{
	private String display = "";
	Frame playArea;
	public Image backgroundImg;

	public Play()
	{
		playArea = new Frame("Euchre");
		try
		{
			backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("background.gif"), "background.gif"));
		}
		catch (Exception e) { /*handled in paintComponent()*/ }
	}

	public void launch()
	{
		playArea.setLayout(new GridLayout (6, 1) );
		playArea.setSize(900,600); //sets size WxH
		playArea.setVisible(true); //lets see it
		playArea.requestFocus();
	}

	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g); 
		if (g != null)
			g.drawImage(backgroundImg, 0,0,this.getWidth(),this.getHeight(),this);
	}

	public void actionPerformed(ActionEvent event)
	{
		//do nothing
	}

	public static void main (String[] args)
	{
		Play go = new Play();
		go.launch();

	}
}

I've changed the PaintComponent method to this:

protected void paintComponent(Graphics g)
	{
		super.paintComponent(g); 
		if (g != null)
			System.out.println("Image is Valid");
		else
			System.out.println("Image is invalid");
	}

Neither will print out, so for whatever reason, I believe the paintComponent method isn't being called. Any idea why?

Both files are in the same folder too, I've checked that (Play.java as well as background.gif)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

sialivi- Doesn't seem to do it, but I should of had that anyways. Good call.

public void launch()
	{
		playArea.setLayout(new GridLayout (6, 1) );
		playArea.setSize(900,600); //sets size WxH
		playArea.setVisible(true); //lets see it
		playArea.requestFocus();
		playArea.repaint();
	}

Antaris- Doesn't seem to work. Heres the new code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Play extends JPanel implements ActionListener
{
	private String display = "";
	Frame playArea;
	public Image backgroundImg;

	public Play()
	{
		playArea = new Frame("Euchre");
	}

	public void launch()
	{
		ImagePanel panel = new ImagePanel(new ImageIcon("background.gif").getImage());
		playArea.getContentPane().add(panel);
		playArea.pack();

		playArea.setLayout(new GridLayout (6, 1) );
		playArea.setSize(900,600); //sets size WxH
		playArea.setVisible(true); //lets see it
		playArea.requestFocus();
		playArea.repaint();
	}

	public ImagePanel(String img)
	{
		this(new ImageIcon(img).getImage());
	}

	public ImagePanel(Image img)
	{
		this.img = img;
		Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
	}

	public void paintComponent(Graphics g)
	{
		g.drawImage(img, 0, 0, null);
	}

	public void actionPerformed(ActionEvent event)
	{
		//do nothing
	}

	public static void main (String[] args)
	{
		Play go = new Play();
		go.launch();

	}
}

I get a compile error:

Play.java:40: invalid method declaration; return type required

public ImagePanel(String img)

^

Play.java:45: invalid method declaration; return type required

public ImagePanel(Image img)

^

2 errors

So, when I change to void, as that's the only thing I can assume it is, without a return statement, I get these errors:

Play.java:29: cannot find symbol

symbol : class ImagePanel

location: class Play

ImagePanel panel = new ImagePanel(new ImageIcon("background.gif").getImage());

^

Play.java:29: cannot find symbol

symbol : class ImagePanel

location: class Play

ImagePanel panel = new ImagePanel(new ImageIcon("background.gif").getImage());

^

Play.java:30: cannot find symbol

symbol : method getContentPane()

location: class java.awt.Frame

playArea.getContentPane().add(panel);

^

Play.java:42: call to this must be first statement in constructor

this(new ImageIcon(img).getImage());

^

Play.java:47: cannot find symbol

symbol : variable img

location: class Play

this.img = img;

^

Play.java:58: cannot find symbol

symbol : variable img

location: class Play

g.drawImage(img, 0, 0, null);

^

6 errors

As you can see, a whole bunch of errors.

Edited by Hendrick
Link to comment
Share on other sites

  • 0

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class Play extends JPanel implements ActionListener
{
	private String display = "";
	Frame playArea;
	public Image backgroundImg;

	public Play()
	{
		playArea = new Frame("Euchre");

		try
		{
			backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("threads.JPG"), "threads.JPG"));
		}
		catch (Exception e) { /*handled in paintComponent()*/ }
	}

	public void launch()
	{
	playArea.add(this);	
		playArea.setLayout(new GridLayout (6, 1) );
		playArea.setSize(900,600); //sets size WxH
		playArea.setVisible(true); //lets see it
		playArea.requestFocus();
	}

	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g); 
		if (g != null)
			g.drawImage(backgroundImg, 0,0,this.getWidth(),this.getHeight(),this);
	}

	public void actionPerformed(ActionEvent event)
	{
		//do nothing
	}

	public static void main (String[] args)
	{
		Play go = new Play();
		go.launch();

	}
}

Works?

Edit- I've changed the image url, replace it with your own.

Edited by Aquarian
Link to comment
Share on other sites

  • 0

I took a 2nd look at the code in your first post.

1) You never actually add the panel to the frame, so you're just displaying an empty frame.

2) You ignore printing the stacktrace when loading the image, so you don't actually see if the image fails to load and why

3) Another way of loading the image is backgroundImg = new ImageIcon(getClass().getResource("/background.gif")).getImage();

Link to comment
Share on other sites

  • 0

Thanks everyone! sialivi, I see what you were saying in your first point, and Aquarian just added it. There was an issue of it not showing all the way down, but then I realized I forgot to remove this line:

playArea.setLayout(new GridLayout (6, 1) );

Was leftover from a different code.

Thanks for all the help everyone!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.