[GUI Image] attach an image as a background picture

Discussion in 'Java' started by Raydog, Apr 8, 2004.

  1. Raydog

    Raydog Guest

    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
     
    Raydog, Apr 8, 2004
    #1
    1. Advertising

  2. 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...
     
    Knute Johnson, Apr 8, 2004
    #2
    1. Advertising

  3. Raydog

    Raydog Guest

    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" <> 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...
     
    Raydog, Apr 9, 2004
    #3
  4. Raydog

    Raydog Guest

    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" <> wrote in message
    news:c56n8g$ors$...
    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" <> 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...
     
    Raydog, Apr 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric

    picture - background

    Eric, Nov 1, 2004, in forum: HTML
    Replies:
    19
    Views:
    776
    brucie
    Nov 2, 2004
  2. Replies:
    0
    Views:
    689
  3. jc
    Replies:
    3
    Views:
    1,785
  4. Warren Tang
    Replies:
    1
    Views:
    574
    Warren Tang
    Sep 17, 2008
  5. Replies:
    8
    Views:
    1,345
    Mick White
    Apr 19, 2005
Loading...

Share This Page