import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class ImageApp extends Component implements KeyListener { BufferedImage img; File[] imageList; public void paint(Graphics g) { //g.drawImage(img, 0, 0, null); g.drawImage(img, 0, 0, 400, 600, null); g.setFont(new Font("Georgia", Font.ITALIC, 20)); g.drawString("Hello graphics", 20, 30); } public ImageApp() { try { img = ImageIO.read(new File("villa.jpg"));} catch (IOException e) { System.out.println("Image File not found"); } this.addKeyListener(this); } public void keyTyped(KeyEvent e) { char key = e.getKeyChar(); System.out.println("key typed:" + key); switch (key) { case 'h': this.setVisible(false); break; case 'l': JFileChooser c = new JFileChooser(); c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int result = c.showOpenDialog(this); java.io.File folder = c.getSelectedFile(); System.out.println("folder selected:" + folder ); this.imageList = folder.listFiles(); System.out.println("Files:" + java.util.Arrays.toString(this.imageList)); break; default: break; } } public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public Dimension getPreferredSize() { if (img == null) { return new Dimension(100,100); } else { //return new Dimension(img.getWidth(null), img.getHeight(null)); return new Dimension(400, 600); } } public static void main(String[] args) { JFrame f = new JFrame("Load Image Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); ImageApp li = new ImageApp(); f.add(li); f.pack(); li.requestFocusInWindow(); f.setVisible(true); } }