Two buffered images onto one component

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
javaQQAsked:
Who is Participating?
 
mark1348Connect With a Mentor Commented:
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();
0
 
objectsCommented:
No. What you'd is use two labels and add them to a JPanel using a 2x1 GridLayout
0
 
CEHJCommented:
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);
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CEHJCommented:
Correction

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

      
            
      
0
 
javaQQAuthor Commented:
Hello  CEHJ;

BufferedImages. Does your stuff apply to BufferedImages?
0
 
objectsCommented:
> BufferedImages. Does your stuff apply to BufferedImages?

What I suggested will work with a BufferedImage
0
 
CEHJCommented:
>>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));
0
 
mark1348Commented:
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 http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.image.BufferedImage,%20java.awt.image.BufferedImageOp,%20int,%20int) for info on how this all works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.