I created a program to drag & drop images, and I want to make the image black & white after right-clicking.
What I have tried:
What I did is to make the image black & white directly after dnd but what I want to do is to make the right-click (
JPopupMenu
and
JMenuItem
) do that instead.
I have everything:
1. Create drag & drop. (done)
2. Create a right-click with
JPopupMenu
and
JMenuItem
. (done)
3. Make the image black and white. (done)
But I don't know how to do what I want (I am new to Java and Swing. Any help is welcome.)
Here is my code:
public class Tests {
public static StretchIcon getPhotoSI(String imageStr) {
return new StretchIcon(imageStr);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame f = new JFrame("Testing");
JLabel l = new JLabel("Drop here");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.add(l);
l.setHorizontalAlignment(JLabel.CENTER);
l.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 1L;
public static final DataFlavor[] SUPPORTED_DATA_FLAVORS =
new DataFlavor[] { DataFlavor.javaFileListFlavor,
DataFlavor.imageFlavor };
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
System.out.println("1");
boolean canImport = false;
for (DataFlavor flavor : SUPPORTED_DATA_FLAVORS) {
if (support.isDataFlavorSupported(flavor)) {
canImport = true;
break;
}
}
return canImport;
}
public boolean importData(TransferHandler.TransferSupport support) {
System.out.println("2");
JDesktopPane jdp = new JDesktopPane();
JInternalFrame mb = new JInternalFrame
("Frame title4", true, true, true, true);
JLabel la = new JLabel();
jdp.add(mb);
mb.setSize(400, 300);
l.add(mb);
mb.add(la);
mb.setVisible(true);
if (canImport(support)) {
try {
Transferable t = support.getTransferable();
Component component = support.getComponent();
if (component instanceof JLabel) {
Image image = null;
if (support.isDataFlavorSupported
(DataFlavor.imageFlavor))
{
image = (Image)
t.getTransferData(DataFlavor.imageFlavor);
} else if
(support.isDataFlavorSupported
(DataFlavor.javaFileListFlavor)) {
@SuppressWarnings("rawtypes")
List files = (List)
t.getTransferData(DataFlavor.javaFileListFlavor);
if (files.size() > 0) {
System.out.println(files);
image = ImageIO.read((File) files.get(0));
}
}
StretchIcon icon = null;
if (image != null) {
icon = new StretchIcon(fitimage
(image, l.getWidth(), l.getHeight()));
}
class PopUpDemo extends JPopupMenu {
JMenuItem anItem;
public PopUpDemo() {
anItem = new JMenuItem("Gray Scale");
add(anItem);
System.out.println("Right click Creadted");
anItem.addActionListener(new ActionListener() {
public void actionPerformed
(ActionEvent evt)
{
jMenuItem1ActionPerformed(evt);
}
private void
jMenuItem1ActionPerformed
(ActionEvent evt)
{
}
});
}
}
class PopClickListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
doPop(e);
mb.requestFocusInWindow();
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
doPop(e);
mb.requestFocusInWindow();
}
private void doPop(MouseEvent e) {
PopUpDemo menu = new PopUpDemo();
menu.show(e.getComponent(),
e.getX(), e.getY());
}
}
mb.addMouseListener(new PopClickListener());
la.setIcon(icon);
return true;
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
return true;
}
public Image fitimage(Image img, int w, int h) {
BufferedImage resizedimage = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedimage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedimage;
}
});
}
}