• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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
0
javaQQ
Asked:
javaQQ
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
 
CEHJCommented:
Correction

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

      
            
      
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now