Results 1 to 5 of 5
  1. #1
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Add an image to JFrame

    Hi all,

    I want to add an image to a JFrame as the background. On top of that I want to working on more controls.

    How can I do that, I work on Netbeans for this.

    Eranga.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImagePanel extends JPanel {
        BufferedImage image;
    
        public ImagePanel(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Draw image centered.
            int x = (getWidth() - image.getWidth())/2;
            int y = (getHeight() - image.getHeight())/2;
            g.drawImage(image, x, y, this);
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/reindeer.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            ImagePanel contentPane = new ImagePanel(image);
            // You'll want to be sure this component is opaque
            // since it is required for contentPanes. Some
            // LAFs may use non-opaque components.
            contentPane.setOpaque(true);
            contentPane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            // Add components.
            for(int j = 0; j < 8; j++) {
                gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER
                                               : GridBagConstraints.RELATIVE;
                contentPane.add(new JButton("button " + (j+1)), gbc);
            }
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Thanks a lot pal. It's really nice work.

    I've done interesting thing here pal. Just add a JPanel on top of the JFrame. Then drag all the components onto it. On JPanel add a image icon in form load event.

    Seems it's fine.

    Eranga

  4. #4
    fatih_58 is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    hi ı have a problem
    ı must do a project..
    I want to add all images in a folder to a JFrame as the background in slide with threads ..
    how can ı do ???
    plaese help me !!!
    emergency

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

Similar Threads

  1. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 08:02 PM
  2. Replies: 2
    Last Post: 03-24-2008, 06:29 AM
  3. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 03:53 AM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •