Java system iconExtracting the
JavaThe following is a sample program that extracts icons from a specified file into a png format image file. The specified file can be an executable file with exe extension, dll, xls, doc, or any other file.
I have looked on the Internet for similar methods, but have not found any.
However, we have found a really easy way to get it, so we will introduce it here.
This method is also available on Chat & Messenger,Ability to attach files to stickiesWe are using this service in the following areas.
Sample program operation check
Run a sample
If a jar file is associated, it can be executed.
- Get the sample source, compile and run it.
- When launched, the main window will open. Drag and drop the icon you wish to extract directly into the window.
- A png-format image file is output to the same location as the original eincon.
Sample Program Explanation
The process of extracting icons in Java is realized in the following way.
-
Set Look & Feel to Windows.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - Drag-and-drop retrieved files. JFileChooser.getIcon() method. Icon object.
- ImageIO.write() method outputs the Icon object to a png image file.
sample program
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class IconExtractor extends JFrame
implements DropTargetListener {
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JFileChooser chooser;
public static void main(String[] args) {
IconExtractor extractor = new IconExtractor();
extractor.setVisible(true);
}
public IconExtractor() {
super();
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
chooser = new JFileChooser();
} catch (Exception e) {
e.printStackTrace();
}
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle(“Icon extraction”);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setText(“Drag the icon.”);
jLabel.setBackground(Color.white);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setOpaque(true);
new DropTarget(jLabel,
DnDConstants.ACTION_COPY_OR_MOVE, this);
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(jLabel, BorderLayout.CENTER);
}
return jContentPane;
}
// — implements DropTargetListener —————————-
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(DnDConstants.ACTION_MOVE);
try {
Transferable tran = dtde.getTransferable();
List fileList = (List) tran
.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext()) {
File file = (File) iterator.next();
ImageIcon icon = (ImageIcon) chooser.getIcon(file);
String ext = file.getAbsoluteFile() + “.png”;
// Save image file
ImageIO.write((BufferedImage) icon.getImage(), “png”,
new File(ext));
jLabel.setText(“<html>Extraction completed<br>” + ext);
setIconImage(icon.getImage());
}
dtde.dropComplete(true);
} catch (Exception e) {
e.printStackTrace();
dtde.dropComplete(false);
}
}
public void dragEnter(DropTargetDragEvent e) {
}
public void dragOver(DropTargetDragEvent e) {
}
public void dropActionChanged(DropTargetDragEvent e) {
}
public void dragExit(DropTargetEvent e) {
}
}