I am newer to programming and am trying to get my image viewer program to display BMP images. The program works for JPEG and PNG images but not for BMP. I have commented out attempts I have made to make BMP work which I have found online. The methods I have found online all seem to convert the BMP to PNG. I would preferr for the image to stay a BMP but if that is not possible would still like to get it working where it converts to PNG. Aditionally, I know how to change the image window size but don't know what determines the size the image displays in or if there is a way to change that? I have included what I have so far below:
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class ImageViewer {
public static void main(String[] args) {
JFrame frame = new ImageViewerFrame();
frame.setTitle("Image Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ImageViewerFrame extends JFrame {
JLabel label;
JFileChooser chooser;
JMenuBar menubar;
JMenu menu;
JMenuItem menuitem;
public ImageViewerFrame() {
setSize(500,500); // set size of window
label = new JLabel();
add(label);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu("File");
menubar.add(menu);
menuitem = new JMenuItem("Open");
menu.add(menuitem);
menuitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
//Image image = ImageIO.read
String name = chooser.getSelectedFile().getPath();
//Image image = ImageIO.read(name);
//ImageIcon icon = new ImageIcon(name);
//JLabel test = new JLabel(icon);
label.setIcon(new ImageIcon(name)); //opens file
// ImageIcon icon = new ImageIcon(name);
// label.setIcon(new ImageIcon(icon));
}
}
});
}
}