how to display two images on canvas using JFileChooser

hiii all.....how can i display two images selected from JFileChooser on a canvas ...is it possible?
[113 byte] By [gari_da] at [2007-11-27 0:49:28]
# 1

Don't use a canvas.

You can use custom painting for this, read the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

Or you can use a JPanel and place 2 JLabels into it.

Read the tutorial how use JLabel to display images:

http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html

Rodney_McKaya at 2007-7-11 23:19:01 > top of Java-index,Security,Cryptography...
# 2

The JLabel option is easier. Here's a stab at the drawing option.

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.swing.filechooser.FileFilter;

public class ImageLayout extends JPanel implements ActionListener {

BufferedImage[] images = new BufferedImage[0];

JFileChooser fileChooser;

Point[] locs = new Point[0];

final int PAD = 25;

public void actionPerformed(ActionEvent e) {

int choice = fileChooser.showOpenDialog(this);

if(choice == JFileChooser.APPROVE_OPTION) {

File[] files = fileChooser.getSelectedFiles();

images = loadImages(files);

layoutImages();

repaint();

}

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

for(int j = 0; j < images.length; j++)

g.drawImage(images[j], locs[j].x, locs[j].y, this);

}

private void layoutImages() {

locs = new Point[images.length];

int w = getWidth();

int x = PAD;

int y = PAD;

int maxWidth = 0;

int rowHeight = 0;

for(int j = 0; j < images.length; j++) {

int iw = images[j].getWidth();

int ih = images[j].getHeight();

if(x + iw + PAD > w) {

x = PAD;

y += rowHeight;

if(rowHeight > PAD)

y += PAD;

rowHeight = ih;

}

locs[j] = new Point(x, y);

if(ih > rowHeight)

rowHeight = ih;

x += iw + PAD;

if(x > maxWidth)

maxWidth = x;

}

setPreferredSize(new Dimension(maxWidth, y + rowHeight + PAD));

revalidate();

}

private BufferedImage[] loadImages(File[] files) {

BufferedImage[] images = new BufferedImage[files.length];

for(int j = 0; j < files.length; j++) {

try {

images[j] = ImageIO.read(files[j]);

} catch(IOException e) {

System.out.println("read error: " + e.getMessage());

return new BufferedImage[0];

}

}

return images;

}

private JPanel getUIPanel() {

fileChooser = new JFileChooser("images");

fileChooser.setAcceptAllFileFilterUsed(false);

fileChooser.setFileFilter(new ImageFilter());

fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

fileChooser.setMultiSelectionEnabled(true);

JButton button = new JButton("open");

button.addActionListener(this);

JPanel panel = new JPanel();

panel.add(button);

return panel;

}

public static void main(String[] args) {

ImageLayout test = new ImageLayout();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(test));

f.getContentPane().add(test.getUIPanel(), "Last");

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class ImageFilter extends FileFilter {

String GIF = "gif";

String PNG = "png";

String JPG = "jpg";

String BMP = "bmp";

public boolean accept(File file) {

if(file != null) {

if(file.isDirectory())

return true;

String extension = getExtension(file);

if(extension != null && isSupported(extension))

return true;

}

return false;

}

public String getDescription() {

return GIF + ", " + PNG + ", " + JPG + ", " + BMP + " images";

}

private String getExtension(File file) {

if(file != null) {

String filename = file.getName();

int dot = filename.lastIndexOf('.');

if(dot > 0 && dot < filename.length()-1)

return filename.substring(dot+1).toLowerCase();

}

return null;

}

private boolean isSupported(String ext) {

return ext.equals(GIF) || ext.equals(PNG) ||

ext.equals(JPG) || ext.equals(BMP) ||

ext.equals("jpeg");

}

}

crwooda at 2007-7-11 23:19:01 > top of Java-index,Security,Cryptography...
# 3
hiii...thanx for the code...but in this second image is coming on top of the first....actually i want that the two images should be displayed side by side......
gari_da at 2007-7-11 23:19:01 > top of Java-index,Security,Cryptography...
# 4

As I already tried to explain, just use a JPanel with 2 JLabels and BorderLayout.

JPanel panel = new JPanel(new BorderLayout());

JLabel label1 = new JLabel();

JLabel label2 = new JLabel();

panel.add(label1, BorderLayout.WEST);

panel.add(label2, BorderLayout.EAST);

Now when you select the first image do:label1.setIcon(new ImageIcon(image1));

And when you select the second image do:label2.setIcon(new ImageIcon(image2));

Rodney_McKaya at 2007-7-11 23:19:01 > top of Java-index,Security,Cryptography...