Results 1 to 5 of 5
  1. #1
    samiksha.goel is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default add image on panel

    actually m working on a project and facing problem in adding images on frame as background...as i want to add buttons,textboxes,labels,images and more panels on that frame

    pls reply as soon as possible...
    thanx...
    Last edited by samiksha.goel; 07-26-2008 at 11:04 AM.

  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.swing.*;
    
    public class ImageOnBackground {
        public static void main(String[] args) throws IOException {
            BufferedImage image =
                javax.imageio.ImageIO.read(new File("images/hawk.jpg"));
            ImageBackgroundPanel imp = new ImageBackgroundPanel(image);
            // Set layout and add components to imp as desired.
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(imp);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class ImageBackgroundPanel extends JPanel {
        BufferedImage image;
    
        ImageBackgroundPanel(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }

  3. #3
    samiksha.goel is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    thanx for the reply...it really helps me a lot...
    but i wont be able to add more panels or textboxes n buttons on that...can u pls guide me for that also

  4. #4
    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.swing.*;
    
    public class ImageOnBackground {
        public static void main(String[] args) throws IOException {
            BufferedImage image =
                javax.imageio.ImageIO.read(new File("images/hawk.jpg"));
            ImageBackgroundPanel imp = new ImageBackgroundPanel(image);
            // Set layout and add components to imp as desired.
            addComponents(imp);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(imp));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private static void addComponents(JPanel panel) {
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(20,5,20,5);
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            for(int i = 0; i < 10; i++) {
                gbc.gridwidth = ((i+1) % 2 == 0)
                        ? GridBagConstraints.REMAINDER
                        : GridBagConstraints.RELATIVE;
                panel.add(new JButton("button " + (i+1)), gbc);
            }
        }
    }
    
    class ImageBackgroundPanel extends JPanel {
        BufferedImage image;
    
        ImageBackgroundPanel(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }

  5. #5
    samiksha.goel is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Problem in getting table on panel
    By adeeb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 08:23 PM
  2. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  3. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 04:16 PM
  4. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 11:21 PM
  5. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM

Posting Permissions

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