Hello
I have a small problem with Drag and Drop images.
Class GridImage creates small thumbnails of images to 100 x 100px
Image drag and drop is defined by mouseListener and mousePressed event.
I know that actually I am copying component JLabel to class DrawImage with these settings.
What should I do in transferHandler to copy image without this resize ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
public class GridImages extends JPanel {
public JLabel[] images = new JLabel[9];
private JButton button1 = new JButton("Backgrounds");
private JButton button2 = new JButton("More");
private JButton button3 = new JButton("Titles");
private JButton button4 = new JButton ("Next");
private JButton button5 = new JButton("Previous");
private JLabel test = new JLabel();
private File imageDir;
private String setDir;
private int n=0;
private int p=0;
private String[] filenames;
private MouseListener listener = new DragMouseAdapter();
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public GridImages(){
super();
setLayout(new GridLayout(5,3,10,10));
button1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
imageDir = new File("backgrounds");
setDir = new String("backgrounds/");
filenames = imageDir.list();
if(n<filenames.length){
LoadImages(setDir,filenames.length, filenames,n);
}
}
});
button2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
n=0;
imageDir = new File("more");
filenames = imageDir.list();
LoadImages("more/", filenames.length, filenames,n);
}
});
button3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
}
});
button4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(n<filenames.length){
n+=3;
LoadImages(setDir,filenames.length, filenames,n);
}
}
});
button5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
n-=3;
LoadImages(setDir, filenames.length,filenames,n);
}
});
for(int i=0; i<9; i++){
images[i] = new JLabel();
}
add(button1);
add(button2);
add(button3);
for(int i=0; i<9; i++){
add(images[i]);
}
add(button5);
add(test);
add(button4);
}
public void LoadImages(String dir, int length, String[] filenames, int n){
Image[] image = new Image[length];
Image[] scaledImages = new Image[length];
ImageIcon[] icons = new ImageIcon[length];
for(int i=0; i<9; i++){
icons[i] = new ImageIcon();
}
for(int i=n; i<length; i++){
image[i-n] = toolkit.getImage(dir+filenames[i]);
scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);
icons[i-n].setImage(scaledImages[i-n]);
images[i-n].setIcon(icons[i-n]);
images[i-n].addMouseListener(listener);
images[i-n].setTransferHandler(new TransferHandler("icon"));
}
}
}