I am looking to build a program to show pictures to friends and family. With the regular Windows Image Viewer there is a lot of unused screen space and it does not react on the USB presenter that I use for PPT presentations.
My idea is to have the user select a folder (easy), all images in that folder will be selected and the JFrame will display the first image in full screen mode (easy). On right arrow (easy) the presentation will jump to the next picture from the selection (difficult). Sounds simple, but I can't solve it.
Here is my code so far after Andrews suggestion:
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
class ArrowAction extends AbstractAction {
private String cmd;
public ArrowAction(String cmd) {
this.cmd = cmd;
}
@Override
public void actionPerformed(ActionEvent e) {
if (cmd.equalsIgnoreCase("Backward")) {
System.out.println("Previous picture!");
} else if (cmd.equalsIgnoreCase("Forward")) {
System.out.println("Next Picture!");
}
}
}
class TestKeyBinding {
public TestKeyBinding() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
InputMap im = panel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = panel.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Forward");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Backward");
am.put("Forward", new ArrowAction("Forward"));
am.put("Backward", new ArrowAction("Backward"));
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(frame);
BufferedImage myPicture;
try {
myPicture = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/1/13/Vancouver_ib.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
panel.add(picLabel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.add(panel);
frame.validate();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestKeyBinding kb = new TestKeyBinding();
}
}
Any idea how to do the onKeyEvent and have the next image displayed? And any other best practices that I should get used to?
Thanks, Florian