Results 1 to 5 of 5
  1. #1
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default Adding JPanels to JFrames based on x-y co-ordinates

    I have written a code for adding background image to a JFrame as given below.
    Java Code:
    JFrame frame = new JFrame();
    ImageIcon icon = new ImageIcon("c:\\final.jpg");
    JLabel background = new JLabel(icon);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    My requirement is that, I need to add other JComponents like JTextfields, JTextArea,JButton etc. on to this frame and it should be on top of this image.
    When I maximize or drag and enlarge the JFrame, the JComponents should automatically get aligned with the frame and background. Please help.
    Regards,
    Anees

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    One way could be to write your own Layout manager for the container that you are adding the components to.
    What happens with your current code?

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class ImageComponents extends JPanel {
        BufferedImage image;
        Rectangle r;
        JPanel parent;
    
        public ImageComponents(BufferedImage image) {
            this.image = image;
            r = new Rectangle(image.getWidth(), image.getHeight());
            parent = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weighty = 1.0;
            gbc.weightx = 1.0;
            gbc.gridwidth = 3;
            parent.add(new JTextArea(4, 16), gbc);
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            parent.add(new JTextField(12), gbc);
            parent.add(new JButton("button"), gbc);
            parent.add(new JButton("button"), gbc);
            // See through to image below.
            parent.setOpaque(false);
            // Position parent over image in sizer.
            setLayout(null);
            add(parent);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, r.x, r.y, this);
        }
    
        public static void main(String[] args) throws IOException {
            File file = new File("images/bison.jpg");
            BufferedImage image = javax.imageio.ImageIO.read(file);
            ImageComponents component = new ImageComponents(image);
            component.addComponentListener(component.sizer);
            JFrame f = new JFrame("drag frame edges");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(component);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private ComponentListener sizer = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                r.x = (getWidth() - r.width)/2;
                r.y = (getHeight() - r.height)/2;
                //System.out.printf("r.x = %d  r.y = %d%n", r.x, r.y);
                parent.setBounds(r);
            }
        };
    }

  4. #4
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    Dear hardwired,
    The image is not getting fit to the frame, when the frame is resized.
    Also, how should I move the components to a desired position(say, a little bit to the left/right or to top/bottom, I mean by small increments).

    I am much close to the most sought solution with your thread hardwired. Thanks a lot.

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

    Default

    The image is not getting fit to the frame, when the frame is resized.
    You didn't mention this before. Here's one way to dynamically fit/fill a background image into the contentPane.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class ImageComponents2 extends JPanel {
        BufferedImage image;
    
        public ImageComponents2(BufferedImage image) {
            this.image = image;
            // You can add your child components to this
            // component using the layout manager of your
            // choice.
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            int w = getWidth();
            int h = getHeight();
            int iw = image.getWidth();
            int ih = image.getHeight();
            double xScale = (double)w/iw;
            double yScale = (double)h/ih;
            // Scale-to-fit.
            //double scale = Math.min(xScale, yScale);
            // Scale-to-fill.
            double scale = Math.max(xScale, yScale);
            int width = (int)(scale*iw);
            int height = (int)(scale*ih);
            int x = (int)(w - scale*iw)/2;
            int y = (int)(h - scale*ih)/2;
            g2.drawImage(image, x, y, width, height, this);
        }
    
        public static void main(String[] args) throws IOException {
            File file = new File("images/bison.jpg");
            BufferedImage image = javax.imageio.ImageIO.read(file);
            JFrame f = new JFrame("drag frame edges");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new ImageComponents2(image));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    Also, how should I move the components to a desired position(say, a little bit to the left/right or to top/bottom, I mean by small increments).
    This really depends on what you want the layout to look like and what kind of resizing behavior you want. There are a lot of layout managers you can use/combine. I recommend either
    1 show/describe what you want the layout to look like with ascii art or image(s), or
    2 have a look here: Lesson: Laying Out Components Within a Container.

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 02:11 PM
  3. Adding Plugin Dependency for Feature-based Product
    By abiieez in forum SWT / JFace
    Replies: 2
    Last Post: 05-27-2008, 08:02 PM
  4. problem with getting new JFrames
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 12:46 PM
  5. Handling Two JFrames
    By hiranya in forum AWT / Swing
    Replies: 2
    Last Post: 11-05-2007, 07:23 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
  •