0

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

batman
  • 13
  • 1
  • 1
  • 5
  • 2
    The problem with that code is probably **best** fixed by using [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than `KeyListener`.. – Andrew Thompson Sep 24 '14 at 01:25
  • 2
    BTW - `new Component() { .. @Override public void paint(Graphics g) { super.paint(g); ..` should be `new Component() { .. @Override public void paintComponent(Graphics g) { super.paintComponent(g); ..` for any `JComponent` – Andrew Thompson Sep 24 '14 at 01:27
  • Thanks guys, this doesnt really help. The problem is, that I cant get my head around how to add the KeyListener and still be able to change the images in the frame. Any idea? – batman Sep 24 '14 at 15:45
  • *"Any idea?"* Yes I have some idea. But *first,* why not use key bindings? – Andrew Thompson Sep 24 '14 at 16:34
  • 1
    What is an `ArrowAction`? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). Also, where does this code load images? One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Oct 03 '14 at 08:04
  • ...And I still have no idea of how to get the next picture to show on screen. – batman Oct 03 '14 at 16:58
  • Doesn't it stand to reason a back/forward action has at least ***two*** images to deal with? But a tip `JLabel picLabel = new JLabel(new ImageIcon(myPicture)); panel.add(picLabel);` none of that should be in the action. Instead, create and add the label at start up, then in the action.. `picLabel.setIcon(..);` – Andrew Thompson Oct 04 '14 at 01:35

0 Answers0