Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [GUI Image] attach an image as a background picture

Reply
Thread Tools

[GUI Image] attach an image as a background picture

 
 
Raydog
Guest
Posts: n/a
 
      04-08-2004
Idea:

have a JPanel, it has some buttons and textfields....

I want to add a backgroud image to this JPanel so that all the components on
it will
overlay on top of the background.

is this easy or possible to do ?

Thanks so much

raydog


 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-08-2004
Raydog wrote:
> Idea:
>
> have a JPanel, it has some buttons and textfields....
>
> I want to add a backgroud image to this JPanel so that all the components on
> it will
> overlay on top of the background.
>
> is this easy or possible to do ?
>
> Thanks so much
>
> raydog
>
>


Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}


--

Knute Johnson
email s/nospam/knute/
Molon labe...
 
Reply With Quote
 
 
 
 
Raydog
Guest
Posts: n/a
 
      04-09-2004
Hi Knute:
I tried your code, but read() always gives me exception "cannot read file",
and my pic is a jpg format too.

did i miss out anything ?

THanks so much,
raydog

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:b2ee5eb8a1233459478bf3daaa505724@news.1usenet .com...
Raydog wrote:
> Idea:
>
> have a JPanel, it has some buttons and textfields....
>
> I want to add a backgroud image to this JPanel so that all the components

on
> it will
> overlay on top of the background.
>
> is this easy or possible to do ?
>
> Thanks so much
>
> raydog
>
>


Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}


--

Knute Johnson
email s/nospam/knute/
Molon labe...


 
Reply With Quote
 
Raydog
Guest
Posts: n/a
 
      04-09-2004
I think i got it fixed,
somehow jpg saved by some app does not have the first a few bytes
indicationg what image
format it is.
this caused a trouble when ImageIO reading the file in.

"Raydog" <(E-Mail Removed)> wrote in message
news:c56n8g$ors$(E-Mail Removed)...
Hi Knute:
I tried your code, but read() always gives me exception "cannot read file",
and my pic is a jpg format too.

did i miss out anything ?

THanks so much,
raydog

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:b2ee5eb8a1233459478bf3daaa505724@news.1usenet .com...
Raydog wrote:
> Idea:
>
> have a JPanel, it has some buttons and textfields....
>
> I want to add a backgroud image to this JPanel so that all the components

on
> it will
> overlay on top of the background.
>
> is this easy or possible to do ?
>
> Thanks so much
>
> raydog
>
>


Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}


--

Knute Johnson
email s/nospam/knute/
Molon labe...



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any shortcut for "Debug > Attach > Attach to aspnet_wp.exe"? Warren Tang ASP .Net 1 09-17-2008 03:46 PM
DIV background-repeat background-image? For shadow effect jc ASP .Net 3 03-19-2008 04:19 PM
DIV background-repeat background-image for shadowing effect jc HTML 1 03-19-2008 02:16 PM
attach agent to applet running in firefox (attach api) craiget@gmail.com Java 0 06-14-2007 12:28 AM
changing a background image to a background colour? Dj Frenzy Javascript 3 02-10-2004 08:08 PM



Advertisments