立ち往生しています。矢印キーをクリックしても写真が読み込まれません。最初の画像のみが表示されます。そして、それ以上何も起こりません。矢印キーをクリックしてフォルダ内の画像を変更できるようにしたいです。これがコードです。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPanel extends JPanel implements KeyListener {
//public Image [] walkable;
public Image walkable;
int i = 0;
public char c = 'e';
public String p = "no";
public ImageIcon icon;
//for (int s=0;s<3;s++){
// ImageIcon p[s] = ImageIcon("002.jpg");
// }
//ImageIcon icon = new ImageIcon(p);
//ImageIcon icon = ImageIcon("001.jpg");
//ImageIcon icon = new ImageIcon();
public MyPanel() {
this.setPreferredSize(new Dimension(500, 500));
String path = ".";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
if (i < listOfFiles.length) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".jpg") || files.endsWith(".JPG") || files.endsWith(".png") || files.endsWith(".PNG")) {
ImageIcon icon = new ImageIcon(files);
walkable = icon.getImage();
// return icon;
}
}
}
addKeyListener(this);
}
public void addNotify() {
super.addNotify();
requestFocus();
}
public void paintComponent(Graphics g) {
//g.clearRect(0, 0, getWidth(), getHeight());
//g.drawString("the key that pressed is " + c, 250, 250);
//g.drawString("the key that pressed is " + p, 400, 250);
g.drawImage(walkable, 0, 0, this);
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
c = e.getKeyChar();
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
// handle up
break;
case KeyEvent.VK_DOWN:
// handle down
break;
case KeyEvent.VK_LEFT:
i = i - 1;
//walkable = icon.getImage();
break;
case KeyEvent.VK_RIGHT:
i = i + 1;
//walkable = icon.getImage();
break;
}
repaint();
}
public static void main(String[] s) {
JFrame f = new JFrame();
f.getContentPane().add(new MyPanel());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}