Two buffered images onto one component

Posted on 2006-05-27
Last Modified: 2012-08-13
Does Java allow one to draw | paint two bufferedImages onto one JLabel?

Say I have two buffered images, each 200 x 100, and a JLabel or canvas 200 x 200.

How would i draw | paint  image A into the top half of the label | canvas, and image B into the bottom half?

Example code or pointers to closely relevant tutorials greatly appreciated

Many thanks in advance
Question by:javaQQ
    LVL 92

    Expert Comment

    No. What you'd is use two labels and add them to a JPanel using a 2x1 GridLayout
    LVL 86

    Expert Comment

    Use two labels.

    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(new JLabel(new ImageIcon("top.jpg"), BorderLayout.NORTH);
    p.add(new JLabel(new ImageIcon("bottom.jpg"), BorderLayout.SOUTH);
    LVL 86

    Expert Comment


    p.add(new JLabel(new ImageIcon("top.jpg"), BorderLayout.NORTH));
    p.add(new JLabel(new ImageIcon("bottom.jpg"), BorderLayout.SOUTH));


    Author Comment

    Hello  CEHJ;

    BufferedImages. Does your stuff apply to BufferedImages?
    LVL 92

    Expert Comment

    > BufferedImages. Does your stuff apply to BufferedImages?

    What I suggested will work with a BufferedImage
    LVL 86

    Expert Comment

    >>Does your stuff apply to BufferedImages?

    Yes. Try

    p.add(new JLabel(new ImageIcon(bufImage1), BorderLayout.NORTH));
    p.add(new JLabel(new ImageIcon(bufImage2), BorderLayout.SOUTH));
    LVL 1

    Accepted Solution

    I would just combine the 2 images into one and then use the new image for the label.

    Here is a sample of how to make an image by painting your 2 images one above the other.

    // you know the size of all images so you can take short cuts by.
    // hard coding your dimensions but your code won't be flexible.
    BufferedImage lCombinedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = lCombinedImage.createGraphics();
    g.drawImage(image1, 0, 0, null); // draws starting at 0,0 (top left)
    g.drawImage(image2, 0, 100, null); // draws starting at 0,100 (middle left)

    JLabel lYourLabel = new JLabel(new ImageIcon(lCombinedImage));

    // --- the following is commented out because it is not needed, it is only a suggestion ---
    // if you don't know the size of the images you could use something
    // like the following 2 lines to figure out what size lCombinedImage you should use
    // int width = Math.max(image1.getWidth(), image2.getWidth());
    // int height = image1.getHeight() + image2.getHeight();
    LVL 1

    Expert Comment

    sorry,  I gave you the wrong order of the drawImage args.
    use the following instead.
    g.drawImage(image1, null, 0, 0); // draws starting at 0,0 (top left)
    g.drawImage(image2, null, 0, 100); // draws starting at 0,100 (middle left)

    See,%20java.awt.image.BufferedImageOp,%20int,%20int) for info on how this all works.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java JDK Download 7 49
    count8 challlenge 13 67
    array220 challenge 8 31
    strDist challenge 35 69
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now