How to get the x and y of a program window in KNIME?

@takbb, Good evening. Once again, we need help. I have a Java code to determine the X and Y points. The code is working. I would like to run this script on KNIME via the Java Edit Variable node, I know it works. But I don’t have enough knowledge to do it. Could you help. With this question?

import java.awt.;
import java.awt.event.
;
import java.awt.image.;
import javax.swing.
;

/** Getting a point of interest on the screen.
Requires the MotivatedEndUser API - sold separately. */
class GetScreenPoint {

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    final Dimension screenSize = Toolkit.getDefaultToolkit().
        getScreenSize();
    final BufferedImage screen = robot.createScreenCapture(
        new Rectangle(screenSize));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JLabel screenLabel = new JLabel(new ImageIcon(screen));
            JScrollPane screenScroll = new JScrollPane(screenLabel);
            screenScroll.setPreferredSize(new Dimension(
                (int)(screenSize.getWidth()/2),
                (int)(screenSize.getHeight()/2)));

            final Point pointOfInterest = new Point();

            JPanel panel = new JPanel(new BorderLayout());
            panel.add(screenScroll, BorderLayout.CENTER);

            final JLabel pointLabel = new JLabel(
                "Click on any point in the screen shot!");
            panel.add(pointLabel, BorderLayout.SOUTH);

            screenLabel.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent me) {
                    pointOfInterest.setLocation(me.getPoint());
                    pointLabel.setText(
                        "Point: " +
                        pointOfInterest.getX() +
                        "x" +
                        pointOfInterest.getY());
                }
            });

            JOptionPane.showMessageDialog(null, panel);

            System.out.println("Point of interest: " + pointOfInterest);
        }
    });
}

}

Hi @Nuke_Attokurov,

attached a workflow example
KNIME_project2.knwf (5.2 KB)

I think

SwingUtilities.invokeLater(new Runnable() {
is counter productive as it will cause the node to finish before the user picked the point.
So it was removed from the example

4 Likes

Oh, that’s exactly what I need. Thanks a lot

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.