background image for JPanel

I was to come here and "search" for examples on this but i havnt found any so im jsut going to post it:

How do i set an image to be the background of a JPanel? setBackground(image); doesnt work... i was toal something about paintComponent() but i tried it with that too and it didnt work... anyhelp would be greatly appreciated

[340 byte] By [HDL_CinC_Dragona] at [2007-10-3 3:09:02]
# 1

in the 'search forums' box, I pasted your subject line

background image for JPanel

results 150

http://onesearch.sun.com/search/onesearch/index.jsp?qt=background+image+for+JPanel&subCat=siteforumid%3Ajava57&site=dev&dftab=siteforumid%3Ajava57&chooseCat=javaall&col=developer-forums

Michael_Dunna at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ive tried many of the things that search suggested but none of them pertained enough to what i am doing for it to work. Here is my code:package gameFunctions;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class loginScreen extends JFrame implements ActionListener

{

JTextField USERNAME, Username, PASSWORD, Password; // yeah i know, bad practice but there is some logic to the names relative to capitalization

String username, password;

JButton LOGIN;

Dimension res = Toolkit.getDefaultToolkit().getScreenSize();

ImageIcon background = new ImageIcon("C:\\FileTesting\\testImage.jpg");;

JPanel pane;

public static void main(String[] args)

{

loginScreen frame = new loginScreen();

frame.setVisible(true);

}

public loginScreen()

{

Container c = getContentPane();

setSize(400, 300);

setLocation((int)res.getWidth()/2-getWidth()/2, (int)res.getHeight()/2-getHeight()/2);

setResizable(false);

setLayout(null);

pane = new JPanel();

pane.setSize(getWidth(), getHeight());

pane.setLayout(null);

c.add(pane);

USERNAME = new JTextField("Username:");

USERNAME.setSize(68,25);

USERNAME.setLocation(getWidth()/2-USERNAME.getWidth()/2-80, getHeight()/2-USERNAME.getHeight()/2-50);

USERNAME.setEditable(false);

pane.add(USERNAME);

PASSWORD = new JTextField("Password:");

PASSWORD.setSize(68,25);

PASSWORD.setLocation(USERNAME.getX(), USERNAME.getY()+30);

PASSWORD.setEditable(false);

pane.add(PASSWORD);

Username = new JTextField();

Username.setSize(150,25);

Username.setLocation(USERNAME.getX()+USERNAME.getWidth()+5, USERNAME.getY());

Username.setEditable(true);

pane.add(Username);

Password = new JTextField();

Password.setSize(150,25);

Password.setLocation(Username.getX(), Username.getY()+30);

Password.setEditable(false); // because im not working with PW's yet

pane.add(Password);

LOGIN = new JButton("LOGIN");

LOGIN.setSize(80,30);

LOGIN.setLocation(getWidth()/2-LOGIN.getWidth()/2, PASSWORD.getY()+30);

pane.add(LOGIN);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent event)

{

}

}

i want "pane" to be the image of "background" imageIcon is the only thing that works like this because i dont know how to use straight up "Image". everything works if i set the ImageIcon "background" to be the background of JButton "LOGIN"

any help would be greatly appreciated

HDL_CinC_Dragona at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 3

import java.awt.*;

import javax.swing.*;

class loginScreen extends JFrame

{

public loginScreen()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

ImagePanel panel = new ImagePanel("test.gif");

panel.add(new JButton("OK"));

getContentPane().add(panel);

pack();

setLocationRelativeTo(null);

}

class ImagePanel extends JPanel

{

Image img;

public ImagePanel(String file)

{

setPreferredSize(new Dimension(600, 400));

try

{

img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));

}

catch(Exception e){}//do nothing

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if(img != null) g.drawImage(img,0,0,getWidth(),getHeight(),this);

}

}

public static void main(String[] args){new loginScreen().setVisible(true);}

}

Michael_Dunna at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

forgive my noobishness but it doesnt display any image... would you mind telling me what i need to change in order to have an image from the HDD be displayed in the JPanel?

i think all i have to do is change this line right?: img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));

and maybe this one too:ImagePanel panel = new ImagePanel("test.gif");

but i dont know what exactly i need to change it to...

please help me

HDL_CinC_Dragona at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 5
ok i got it working by importing an image directly into the folder instead of using a entire dir path... is there a way to get it working with a dir path?
HDL_CinC_Dragona at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 6

1)

copy this file into the same directory as the .java file you made from my prior post

testImage.jpg

2)

change

ImagePanel panel = new ImagePanel("test.gif");

to

ImagePanel panel = new ImagePanel("testImage.jpg");

3)

recompile/rerun, your image should appear as a background, with a JButton

near the top center

Michael_Dunna at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 7
> is there a way to get it working with a dir path?ImagePanel panel = new ImagePanel("file:c:\\FileTesting\\test.gif");
Michael_Dunna at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 8

Thanks! it works great! I thank you greatly for your help and patience...

one more question:

Ive done a forum search on this but nothing releated came up...

is it possible to set a background image to JTextArea similar to the "setIcon" method for JButtons? This will be my last question on the matter I promise.

HDL_CinC_Dragona at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 9

> is it possible to set a background image to JTextArea similar to the "setIcon" method for JButtons?

No, there is no set icon method so you need to draw the image yourself, similiar to the way you did it for the panel. The difference is

a) the text area needs to be non-opaque.

b) you draw the image first then use super.paintComponent.

These concepts are discussed in many postings on the forum. You need to improve your search skills.

camickra at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 10

> is it possible to set a background image to JTextArea

I don't know that it's a good idea mixing the graphics 'with' the text, seems

easier to have the panel showing an image, then adding a transparent text area

to the panel, effectively typing over the top of the image - if that is what you are trying to do.

here's the earlier code modified

import java.awt.*;

import javax.swing.*;

class loginScreen extends JFrame

{

public loginScreen()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

ImagePanel panel = new ImagePanel("file:c:\\FileTesting\\test.gif");

panel.setLayout(new GridLayout(1,1));

JTextArea ta = new JTextArea();

JScrollPane sp = new JScrollPane(ta);

ta.setOpaque(false);

sp.setOpaque(false);

sp.getViewport().setOpaque(false);

panel.add(sp);

getContentPane().add(panel);

pack();

setLocationRelativeTo(null);

}

class ImagePanel extends JPanel

{

Image img;

public ImagePanel(String file)

{

setPreferredSize(new Dimension(100, 100));

try

{

img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));

}

catch(Exception e){}//do nothing

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if(img != null) g.drawImage(img,0,0,getWidth(),getHeight(),this);

}

}

public static void main(String[] args){new loginScreen().setVisible(true);}

}

Michael_Dunna at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 11
Thanks! that is exactly what i wanted! Haha you rock man! Thanks again!
HDL_CinC_Dragona at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 12

This posting has a link to my ImagePanel, which allows you to do different things with the background image (tiling, scaling...). It automatically makes the components non-opaque as well so you don't have to worry about it.

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=316074

camickra at 2007-7-14 20:59:40 > top of Java-index,Desktop,Core GUI APIs...