0

I'm new to programming in Java, but I managed to learn the basics at this moment. At this moment I try to do image analysis with the program ImageJ.

In this code I've got an picture (which is refered as an ImagePlus object). In this script the first part consist of some processing. Afterwards I would like to select some specific spots by hand. After this selection I would like to run another processing script.

To do this I managed to get the first processing part. Then I would like to have a code which results in a pause of the script. When I press a button (like spacebar) I would like to continue the script and do the last part of the processing.

At this moment this is my draft;

class Help{

import static java.awt.event.KeyEvent.VK_E;

public class Help extends JComponent{
    private final static int onmask_one = InputEvent.ALT_GRAPH_DOWN_MASK;

    public static void main(String[] args) {
        //some post processing
        ImagePlus imp = IJ.getImage();//makes a object of the Image
        Help a = new Help();
        a.keyBindingExample();

    }

    void keyBindingExample() {
        Action spacebarAction= new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                // do something when spacebar button pressed;
            }
        };

        InputMap inputMap = ?.InputMap(JPanel.WHEN_FOCUSED);
        ActionMap actionMap = ?.ActionMap();

        inputMap.put(KeyStroke.getKeyStroke("SPACEBAR"), "spacebarAction");
        actionMap.put("spacebarAction", spacebAraction);
}

On the internet I could find this draft, but I don't really get what kind of instance I could use in Inputmap inputMap = ?.Inputmap(JPanel.WHEN_IN_FOCUSED_WINDOW);

Is this a Jcomponent? Does this mean that I have to make a Jcomponent of my ImagePlus object?

I know there is also some issues about the focus of the right window. I'm not sure what the settings are in this concept. I would like to continue the script without having a specific window focused. __solved; I should have to use JPanel.WHEN_FOCUSED to have a focus on the keyboard without any problems on window focus.

Hope you can help me out.

EDIT: I edited some of the code to clarify things. which weren't so clear in the original post.

  • Please clarify your question. What don't you understand specifically? What are you asking? Do you understand Key Bindings? Because that's what this code is using. If not, better to first check the [Key Bindings Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). Have you gone to the API to look up `JPanel.WHEN_IN_FOCUSED_WINDOW` to see what it refers to (it will refer you to the super class by the way, JComponent). – Hovercraft Full Of Eels Jan 02 '17 at 22:21
  • And does that code even compile? What is `j` for instance? And why are you calling `InputMap(JPanel.WHEN_IN_FOCUSED_WINDOW)` and not `getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW)`? Same for `ActionMap()` and not `getActionMap()`? Are you blindly and sloppily copying code but not understanding it? Please be honest. – Hovercraft Full Of Eels Jan 02 '17 at 22:25
  • @HovercraftFullOfEels I'm honest that I used internet to copy some code. But I tried to rewrite it for my own purpose. I rewrite the code so my question should me more clear. The question is; what kind of instance do I need for the inputmap and actionmap? I don't use getInputMap/getActionMap because I want to specify this all myself. This means that I want to press the spacebar and then do some processing. – NoUsername Jan 02 '17 at 22:43
  • @HovercraftFullOfEels – NoUsername Jan 02 '17 at 22:43
  • You will want to check out the Key Bindings tutorial, which I've linked to above. In the future, please avoid posting sort-of kind-of code, and instead post code that actually compiles. That will save all of us grief. Or if you're confused about a compiler error, then post the full error with details about it in your question text. – Hovercraft Full Of Eels Jan 02 '17 at 22:45
  • I've also written a lot about key bindings on this site, including [these links](http://stackoverflow.com/search?q=user%3A522444+%5Bkey-bindings%5D) – Hovercraft Full Of Eels Jan 02 '17 at 22:47
  • `"The question is; what kind of instance do I need for the inputmap and actionmap?"` -- as the tutorial explains, it has to be a JComponent or a class derived from that (JPanel). `"I don't use getInputMap/getActionMap because I want to specify this all myself.`" -- Sorry but that statement makes absolutely no sense at all, and I consider myself to be a Key Bindings expert. – Hovercraft Full Of Eels Jan 02 '17 at 22:48
  • By no sense: You must get the appropriate InputMap and ActionMap, and the **only** way to do this is to call the getter methods. Period. If you want "to specify this all myself" and not use the getter methods, then your code won't work. – Hovercraft Full Of Eels Jan 02 '17 at 22:49
  • `"Does this mean that I have to make a Jcomponent of my ImagePlus object?"` -- no. Your GUI code if its a Swing program should have plenty of JComponents to choose from. – Hovercraft Full Of Eels Jan 02 '17 at 22:50
  • Hmm well... Than I think I don't got the principles at all. I'm going to check the internet. I didn't get the answer from that post you've added. Maybe it is just to soon for me to do this kind of things and get back to the java books :) Thanks for you help! – NoUsername Jan 02 '17 at 22:51
  • In fact, your class above extends JCompnent, and so you could call the methods on `this`, but **only if you will add `this` to the GUI**. – Hovercraft Full Of Eels Jan 02 '17 at 22:59
  • There is an ImageJ forum with an active community: http://forum.imagej.net/ I am sure you will find some help there. As for your specific problem, I would refrain from implementing a new solution for something that has probably been solved. In Fiji, http://fiji.sc/ , press Alt + L and type PointPicker. Use this tool to select points and write them to a text file that you can read from within your code. There might be a better solution but this should be a good start. – hanslovsky Feb 22 '17 at 22:48

0 Answers0